实验4 继承
实验任务2(验证性实验)
程序源码:
1 #include<iostream> 2 #include<typeinfo> 3 //definitation of Graph 4 class Graph 5 { 6 public: 7 void draw() { std::cout << "Graph::draw():just as an interface\n"; } 8 }; 9 10 //definition of Rectangle,derived from Graph 11 class Rectangle :public Graph 12 { 13 public: 14 void draw() { std::cout << "Rectangle::draw():programs of draw a retangle\n"; } 15 }; 16 //definition of Circle,derived from Graph 17 class Circle :public Graph 18 { 19 public: 20 void draw() { std::cout << "Circle::draw():programs of draw a circle\n"; } 21 22 }; 23 //definition of fun():as a call interface 24 void fun(Graph* ptr) 25 { 26 std::cout << "pointer type: " << typeid(ptr).name() << "\n"; 27 std::cout << "RTTI type: " << typeid(*ptr).name() << "\n"; 28 ptr->draw(); 29 } 30 //test 31 int main() 32 { 33 Graph g1; 34 Rectangle r1; 35 Circle c1; 36 //call by object name 37 g1.draw(); 38 r1.draw(); 39 c1.draw(); 40 std::cout << "\n"; 41 // call by object name, and using the scope resolution operator:: 42 r1.Graph::draw(); 43 c1.Graph::draw(); 44 std::cout << "\n"; 45 // call by pointer to Base class 46 fun(&g1); 47 fun(&r1); 48 fun(&c1); 49 }
运行结果截图:
归纳总结:
如果一个类有直接或间接的虚基类,则先执行虚基类的构造函数
如果该类有其他基类,则按照它们在继承声明列表中的出现的次序,分别执行它们的构造函数,但构造过程中,不再执行它们的虚基类的构造函数
对代码微调后,运行结果截图:
实验任务3
程序源码:
1 //task3.cpp 2 #include<iostream> 3 #include "electricCar.hpp" 4 int main() 5 { 6 using namespace std; 7 // test class of Car 8 Car oldcar("Audi", "a4", 2016); 9 cout << "--------oldcar's info--------" << endl; 10 oldcar.update_odometers(25000); 11 oldcar.info(); 12 cout << endl; 13 // test class of ElectricCar 14 ElectricCar newcar("Tesla", "model s", 2016); 15 newcar.update_odometers(2500); 16 cout << "\n--------newcar's info--------\n"; 17 newcar.info(); 18 }
1 //electricCar.hpp 2 #pragma once 3 #include<iostream> 4 #include"car.hpp" 5 #include"battery.hpp" 6 using namespace std; 7 class ElectricCar :public Car 8 { 9 public: 10 ElectricCar(string maker0, string model0, int year0) :Car(maker0, model0, year0), battery() {} 11 void info() 12 { 13 cout << left << setw(20) << "maker:" << maker << endl; 14 cout << left << setw(20) << "model:" << model << endl; 15 cout << left << setw(20) << "year:" << year << endl; 16 cout << left << setw(20) << "odometers:" << odometers << endl; 17 cout << left << setw(20) << "capacity:" << battery.get_capacity() << "-kwh" << endl; 18 } 19 private: 20 Battery battery; 21 };
1 //battery.hpp 2 #pragma once 3 #include<iostream> 4 class Battery 5 { 6 public: 7 Battery() :capacity(70) {} 8 int get_capacity() { return capacity; } 9 protected: 10 double capacity; 11 };
1 //car.hpp 2 #pragma once 3 #include<iostream> 4 #include<iomanip> 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 using std::string; 9 using std::setw; 10 using std::left; 11 class Car 12 { 13 public: 14 Car(string maker0,string model0,int year0):maker(maker0),model(model0),year(year0),odometers(0) {} 15 void info() 16 { 17 cout << left << setw(20) << "maker:" << maker << endl; 18 cout << left << setw(20) << "model:" << model << endl; 19 cout << left << setw(20) << "year:" << year << endl; 20 cout << left << setw(20) << "odometers:" << odometers << endl; 21 } 22 void update_odometers(int n) 23 { 24 int temp = odometers; 25 odometers = n; 26 if (odometers < temp) 27 cout << "warning,输入数据有误!" << endl; 28 } 29 string maker; 30 string model; 31 int year; 32 int odometers; 33 };
运行结果截图(更换数据后):
实验任务4
1 //task4.cpp 2 #include <iostream> 3 #include "pets.hpp" 4 void play(MachinePets* ptr) 5 { 6 std::cout << ptr->get_nickname() << " says " << ptr->talk() << 7 std::endl; 8 } 9 int main() 10 { 11 PetCats cat("miku"); 12 PetDogs dog("da huang"); 13 play(&cat); 14 play(&dog); 15 }
1 //pets.hpp 2 #pragma once 3 #include<iostream> 4 #include<string> 5 using namespace std; 6 class MachinePets 7 { 8 public: 9 MachinePets(const string s) :nickname(s) {} 10 string get_nickname()const 11 { 12 return nickname; 13 } 14 string virtual talk() { return "voice"; } 15 string nickname; 16 }; 17 class PetCats:public MachinePets 18 { 19 public: 20 PetCats(const string s) :MachinePets(s) {} 21 inline string talk() 22 { 23 return "miao wu~"; 24 } 25 }; 26 class PetDogs :public MachinePets 27 { 28 public: 29 PetDogs(const string s) :MachinePets(s) {} 30 inline string talk() 31 { 32 return "wang wang~"; 33 } 34 };
运行测试截图:(未更改)