实验四 继承
task2
改动后
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 // definition of Rectangle, derived from Graph 12 class Rectangle : public Graph 13 { 14 public: 15 void draw() { std::cout << "Rectangle::draw(): programs of draw a rectangle\n"; } 16 }; 17 18 // definition of Circle, derived from Graph 19 class Circle : public Graph 20 { 21 public: 22 void draw() { std::cout << "Circle::draw(): programs of draw a circle\n"; } 23 }; 24 25 // definitaion of fun(): as a call interface 26 void fun(Graph *ptr) 27 { 28 std::cout << "pointer type: "<< typeid(ptr).name() << "\n"; 29 std::cout << "RTTI type: " << typeid(*ptr).name() << "\n"; 30 ptr->draw(); 31 } 32 33 // test 34 int main() 35 { 36 Graph g1; 37 Rectangle r1; 38 Circle c1; 39 40 // call by object name 41 g1.draw(); 42 r1.draw(); 43 c1.draw(); 44 45 std::cout << "\n"; 46 47 // call by object name, and using the scope resolution operator:: 48 r1.Graph::draw(); 49 c1.Graph::draw(); 50 51 std::cout << "\n"; 52 53 // call by pointer to Base class 54 fun(&g1); 55 fun(&r1); 56 fun(&c1); 57 }
改动前运行结果图
改动后运行结果图
归纳总结:
同名覆盖原则:派生类与基类中有相同成员时,若未强行指名,则通过派生类对象使用的是派生类的同名成员,如果要通过派生类的对象访问基类被覆盖的同名成员,需要加对象名基类名::同名成员来限定。类型兼容原则:即可用派生类的对象去初始化基类的对象,可用派生类的对象去初始化基类的引用,可用派生类对象的地址去初始化基类对象指针。
task3
1 #include<iostream> 2 #include<string> 3 4 int main() 5 { 6 using namespace std; 7 8 class Battery{ 9 private: 10 int capacity; 11 public: 12 Battery(int c=0):capacity(c) {} 13 int get_capacity() const {return capacity;} 14 }; 15 16 class Car{ 17 protected: 18 string maker,model; 19 int year,odometers; 20 public: 21 Car(string mk,string md,int y,int o=0):maker(mk),model(md),year(y),odometers(o) {} 22 void info(){ 23 cout << "maker: " << maker << endl; 24 cout << "model: " << model << endl; 25 cout << "year: " << year << endl; 26 cout << "odometers: " << odometers << endl << endl; 27 } 28 void update_odometers(int new_odo) 29 { 30 if(new_odo<odometers) 31 cout << "[Warning] information error." << endl; 32 else 33 odometers = new_odo; 34 } 35 }; 36 37 class Electric:public Car{ 38 private: 39 Battery battery; 40 public: 41 Electric(string a,string b,int y,int o=0):Car(a,b,y),battery(70){} 42 void info(){ 43 cout << "maker: " << maker << endl; 44 cout << "model: " << model << endl; 45 cout << "year: " << year << endl; 46 cout << "odometers: " << odometers << endl; 47 cout << "capacity: " << battery.get_capacity() << "-kwh"<< endl; 48 cout << endl; 49 } 50 }; 51 52 Car oldcar("Aodi","a4",2016); 53 cout << "--------oldcar';s info--------" << endl; 54 oldcar.update_odometers(25000); 55 oldcar.info(); 56 57 cout << endl; 58 59 Electric newcar("Tesla","model s",2016); 60 newcar.update_odometers(2500); 61 cout<<"\n--------newcar'sinfo--------\n"; 62 newcar.info(); 63 }
运行结果图
pets.hpp
1 #ifndef MACHINEPETS_H 2 #define MACHINEPETS_H 3 4 #include<iostream> 5 #include<string> 6 7 using std::string; 8 9 class MachinePets{ 10 public: 11 MachinePets(const string n=";Cutie";):nickname(n) {} 12 virtual string talk() {return ";wu ao~";;} 13 string get_nickname() {return nickname;} 14 protected: 15 string nickname; 16 }; 17 18 class PetCats:public MachinePets{ 19 public: 20 PetCats(const string nick):MachinePets(nick) {} 21 string talk() {return ";miao wu~";;} 22 string get_nickname() {return nickname;} 23 }; 24 25 class PetDogs:public MachinePets{ 26 public: 27 PetDogs(const string nick):MachinePets(nick) {} 28 string talk() {return ";wang wang~";;} 29 string get_nickname() {return nickname;} 30 }; 31 32 #endif 33
task4.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(";miku";); 12 13 PetDogs dog(";da huang";); 14 15 play(&cat); 16 play(&dog); 17 } 18
运行结果图