T1:
1 #include <iostream> 2 #include <typeinfo> 3 4 // definitation of Graph 5 class Graph 6 { 7 public: 8 void draw() { std::cout << "Graph::draw() : just as an interface\n"; } 9 }; 10 11 12 // definition of Rectangle, derived from Graph 13 class Rectangle : public Graph 14 { 15 public: 16 void draw() { std::cout << "Rectangle::draw(): programs of draw a rectangle\n"; } 17 }; 18 19 20 // definition of Circle, derived from Graph 21 class Circle : public Graph 22 { 23 public: 24 void draw() { std::cout << "Circle::draw(): programs of draw a circle\n"; } 25 }; 26 27 28 // definitaion of fun(): as a call interface 29 void fun(Graph *ptr) 30 { 31 std::cout << "pointer type: " << typeid(ptr).name() << "\n"; 32 std::cout << "RTTI type: " << typeid(*ptr).name() << "\n"; 33 ptr -> draw(); 34 } 35 36 // test 37 int main() 38 { 39 Graph g1; 40 Rectangle r1; 41 Circle c1; 42 43 // call by object name 44 g1.draw(); 45 r1.draw(); 46 c1.draw(); 47 48 std::cout << "\n"; 49 50 // call by object name, and using the scope resolution operator:: 51 r1.Graph::draw(); 52 c1.Graph::draw(); 53 54 std::cout << "\n"; 55 56 // call by pointer to Base class 57 fun(&g1); 58 fun(&r1); 59 fun(&c1); 60 }
微调后(加virtual)
1.对于同名的函数,继承类会将基类的函数覆盖掉。
2.对于继承的类的对象,可以通过 对象.基类::(同名)函数 的方式来访问被覆盖掉的同名函数。
3.公有派生类对象可以替代基类对象的位置。
4.添加了virtual后,fun函数输出不同。通过使函数变为虚拟函数,可以使派生类对象指针模拟为基类对象来使调用函数发生改变。
T2:
Car.hpp
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 class Car { 5 public: 6 Car(string mk,string md,double y,double dm = 0):maker(mk),model(md),year(y),odometers(0){} 7 void info(); 8 void update_odometers(const double u_om); 9 private: 10 string maker, model; 11 double year, odometers = 0; 12 }; 13 void Car::info() { 14 cout << "Manufacturer:\t\t" << maker << endl; 15 cout << "Model:\t\t\t" << model << endl; 16 cout << "Manuf.Year:\t\t" << year << endl; 17 cout << "Current odometers:\t" << odometers << endl; 18 19 } 20 void Car::update_odometers(const double u_om) { 21 if (u_om <= odometers) { 22 cout << "WARNING!The updated data is under current odometers!" << endl; 23 return; 24 } 25 else 26 odometers = u_om; 27 }
Battery.hpp
1 #include<iostream> 2 using namespace std; 3 class Battery { 4 public: 5 Battery(double cp=70):capacity(cp){} 6 double get_capacity() { return capacity; } 7 8 private: 9 double capacity; 10 };
electricCar.hpp
1 #include<iostream> 2 #include<string> 3 #include"Battery.hpp" 4 #include"Car.hpp" 5 using namespace std; 6 class ElectricCar :public Car { 7 public: 8 ElectricCar(string mk, string md, double y, double dm=0, double btr=70) :Car(mk,md,y,dm), battery(btr){} 9 void info(); 10 private: 11 Battery battery; 12 }; 13 void ElectricCar::info() { 14 Car::info(); 15 cout << "Current Capacity:\t" << battery.get_capacity() << " kWh " << endl; 16 17 }
task.cpp
1 #include <iostream> 2 #include "electricCar.hpp" 3 4 int main() 5 { 6 using namespace std; 7 8 // test class of Car 9 Car oldcar("Audi", "a4", 2016); 10 cout << "--------oldcar's info--------" << endl; 11 oldcar.update_odometers(18000); 12 oldcar.info(); 13 14 cout << endl; 15 16 // test class of ElectricCar 17 ElectricCar newcar("Tesla", "model s", 2016); 18 newcar.update_odometers(2800); 19 cout << "\n--------newcar's info--------\n"; 20 newcar.info(); 21 22 //test update_odometers error 23 cout << "\n--------error test--------\n"; 24 oldcar.update_odometers(10000); 25 oldcar.info(); 26 }
T3:
pets.hpp
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 class MachinePets { 6 public: 7 MachinePets(const string s):nickname(s){} 8 string get_nickname() const { return nickname; } 9 virtual string talk() { return " "; } 10 private: 11 string nickname; 12 }; 13 14 class PetCats :public MachinePets { 15 public: 16 PetCats(const string s):MachinePets(s){} 17 string talk() { return "Meow ~ Meow ~ ~\n"; } 18 }; 19 20 class PetDogs :public MachinePets { 21 public: 22 PetDogs(const string s):MachinePets(s){} 23 string talk() { return "Woof! Woof!!\n"; } 24 };
task.cpp
1 #include <iostream> 2 #include "pets.hpp" 3 4 void play(MachinePets *ptr) 5 { 6 std::cout << ptr->get_nickname() << " says " << ptr->talk() << std::endl; 7 } 8 9 int main() 10 { 11 PetCats cat("Garfield"); 12 PetDogs dog("Snoopy"); 13 14 play(&cat); 15 play(&dog); 16 }