实验二
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
添加virtual
* 对于同名的函数,继承类将覆盖基类的函数 * 对于继承的类,可以通过继承类::函数的方式去访问被隐藏的函数 * 在需要基类对象的任何地方,都可以使用公有派生类的对象来替代。 * 在增添了virtual后,fun()函数输出发生了变化。通过重写虚拟函数来实现对基类虚拟函数的覆盖。
实验任务3
battery.hpp
1 #include<iostream> 2 using namespace std; 3 4 class Battery{ 5 public: 6 Battery(int ca = 70) : capacity(ca) {} ; 7 int get_capacity(){ 8 9 return capacity; 10 } 11 private: 12 int capacity; 13 };
car.hpp
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 class Car{ 6 public: 7 Car(string ma, string mo, int y, int od = 0) : maker(ma), model(mo), year(y), odometers(od) {} ; 8 void info(){ 9 cout<<"maker\t\t"<<maker<<endl; 10 cout<<"model\t\t"<<model<<endl; 11 cout<<"year:\t\t"<<year<<endl; 12 cout<<"odometerd:\t"<<odometers<<endl; 13 } 14 void update_odometers(int od){ 15 if(od < odometers) 16 cout<<"The update_odometers is wrong"<<endl; 17 else 18 odometers = od; 19 } 20 private: 21 string maker; 22 string model; 23 int year; 24 int odometers; 25 };
electricCar.hpp
1 #include<iostream> 2 #include"battery.hpp" 3 #include"car.hpp" 4 5 using namespace std; 6 7 class ElectricCar : public Car{ 8 public: 9 ElectricCar(string ma, string mo, int y, int od = 0, int ba = 70) : Car(ma, mo, y, od), battery(ba) {} ; 10 void info(){ 11 Car::info(); 12 cout<<"capacity:\t"<<battery.get_capacity()<<"-KWh"<<endl; 13 } 14 private: 15 Battery battery; 16 };
task3.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", 2019); 10 cout << "--------oldcar's info--------" << endl; 11 oldcar.update_odometers(2500); 12 oldcar.info(); 13 14 cout << endl; 15 16 // test class of ElectricCar 17 ElectricCar newcar("Tesla", "model s", 2021); 18 newcar.update_odometers(2900); 19 cout << "\n--------newcar's info--------\n"; 20 newcar.info(); 21 }
实验任务四
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{ 9 return nickname; 10 } 11 virtual string talk() {}; 12 private: 13 string nickname; 14 }; 15 16 class PetCats : public MachinePets{ 17 public: 18 PetCats(const string s) : MachinePets(s) {} ; 19 string talk(){ 20 return "miao wu~"; 21 } 22 }; 23 24 class PetDogs : public MachinePets{ 25 public: 26 PetDogs(const string s) : MachinePets(s) {} ; 27 string talk(){ 28 return "wang wang~"; 29 } 30 };
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 PetDogs dog("da huang"); 13 14 play(&cat); 15 play(&dog); 16 }