实验任务二
#include <iostream> #include <typeinfo> // definitation of Graph class Graph { public: virtual void draw() { std::cout << "Graph::draw() : just as an interface\n"; } }; // definition of Rectangle, derived from Graph class Rectangle : public Graph { public: void draw() { std::cout << "Rectangle::draw(): programs of draw a rectangle\n"; } }; // definition of Circle, derived from Graph class Circle : public Graph { public: void draw() { std::cout << "Circle::draw(): programs of draw a circle\n"; } }; // definitaion of fun(): as a call interface ////通过在Graph类接口draw(),不加/加关键字virtual,可以观察到运行时ptr实际指向的对象不同 void fun(Graph* ptr) { std::cout << "pointer type: " << typeid(ptr).name() << "\n"; std::cout << "RTTI type: " << typeid(*ptr).name() << "\n"; ptr->draw(); } // test int main() { Graph g1; Rectangle r1; Circle c1; // call by object name g1.draw(); r1.draw(); c1.draw(); std::cout << "\n"; // call by object name, and using the scope resolution operator:: r1.Graph::draw(); c1.Graph::draw(); std::cout << "\n"; // call by pointer to Base class fun(&g1); fun(&r1); fun(&c1); }
无关键词virtual
有关键词virtual
归纳总结:
(1)在多继承时,基类与派生类之间,或基类之间出现同名成员时,
将出现访问时的二义性(不确定性),需要采用同名覆盖原则来解决
(2)Graph::draw()中的::在这作一元作用域分辨符 它的作用是:当局部
变量和全局变量同名时 ,在局部变量所在作用域中访问全局变量
(3)在需要基类对象的任何地方,都可以使用共有派生类的对象来替
代,派生类对象的地址可以赋值给基类的指针
实验任务三
battery.hpp
#pragma once #include<iostream> using namespace std; class Battery { public: Battery(int capacity0 = 70) :capacity(capacity0) {}; int get_capacity() { return capacity; } private: int capacity; };
car.hpp
#include<iostream> #include<string> using namespace std; class Car { public: Car(string maker0, string model0, int year0, int odometers0 = 0) :maker(maker0), model(model0), year(year0), odometers(odometers0) {}; void info() { cout << "maker:\t\t" << maker << endl; cout << "model:\t\t" << model << endl; cout << "year:\t\t" << year << endl; cout << "odometers:\t" << odometers << endl; } void update_odometers(int odometers0) { if (odometers0 < odometers) { cout << "The update_odometers is wrong" << endl; } else { odometers = odometers0; } } private: string maker; string model; int year; int odometers; };
electricCar.hpp
#include"battery.hpp" #include"car.hpp" #include<iostream> using namespace std; class ElectricCar :public Car { public: ElectricCar(string maker0, string model0, int year0, int odometers0 = 0, int battrey0 = 70) :Car(maker0, model0, year0, odometers0), battery(battrey0) {}; void info() { Car::info(); cout << "capacity:\t" << battery.get_capacity() << "-KWh" << endl; } private: Battery battery; };
task3.cpp
#include <iostream> #include "electricCar.hpp" int main() { using namespace std; // test class of Car Car oldcar("Audi", "a4", 2021); cout << "--------oldcar's info--------" << endl; oldcar.update_odometers(17000); oldcar.info(); cout << endl; // test class of ElectricCar ElectricCar newcar("Tesla", "model s", 2020); newcar.update_odometers(1100); cout << "\n--------newcar's info--------\n"; newcar.info(); }
任务四
pets.hpp
#ifndef _PETS_HPP #define _PETS_HPP #include<iostream> #include<string> using namespace std; class MachinePets { public: MachinePets(const string s) : nickname(s) {}; virtual string talk() {}; string get_nickname() { return nickname; } private: string nickname; }; class PetCats :public MachinePets { public: PetCats(const string s) :MachinePets(s) {}; string talk() { return "miao wu~"; } }; class PetDogs :public MachinePets { public: PetDogs(const string s) :MachinePets(s) {}; string talk() { return "wang wang~"; } }; #endif // !_PETS_HPP
task4.cpp
#include <iostream> #include "pets.hpp" void play(MachinePets* ptr) { std::cout << ptr->get_nickname() << " says " << ptr->talk() << std::endl; } int main() { PetCats cat("miku"); PetDogs dog("da huang"); play(&cat); play(&dog); }