任务二:
未加virtual
加virtual之后
同名覆盖原则:基类中的函数和派生类的函数重名时,若未强行指名,则通过派生类对象使用的是派生类的同名成员
二元作用域分辨符:当派生类与基类中有相同成员时,如果要通过派生类对象访问基类中被隐藏的同名成员,可以用基类名和作用域分辨符来限定
类型兼容原则:在需要基类对象的任何地方,都可以使用公有派生类的对象来替代。
派生类的对象可以隐含转换为基类对象
派生类的对象可以初始化基类的引用
派生类的指针可以隐含转换为基类的指针
通过基类对象名、指针只能使用从基类继承的成员。
任务三:
battery.hpp
1 #ifndef _BATTERY_HPP_ 2 #define _BATTERY_HPP_ 3 #include<iostream> 4 class Battery { 5 private: 6 int capacity; 7 public: 8 Battery(int c = 70): capacity(c){} 9 int get_capacity() const { 10 return capacity; 11 } 12 }; 13 14 #endif
car.hpp
1 #ifndef _CAR_HPP_ 2 #define _CAR_HPP_ 3 #include<iostream> 4 #include<string> 5 6 using namespace std; 7 8 class Car { 9 private: 10 string maker, model; 11 int year, odometers; 12 public: 13 Car(string maker1, string model1, int year1); 14 void info(); 15 void update_odometers(int m); 16 }; 17 18 Car::Car(string maker1, string model1, int year1) : 19 maker(maker1), model(model1), year(year1) { 20 odometers = 0; 21 } 22 23 void Car::info() { 24 cout << "maker: " << maker << endl; 25 cout << "model: " << model << endl; 26 cout << "year; " << year << endl; 27 cout << "odometers: " << odometers << endl; 28 } 29 void Car::update_odometers(int m) { 30 if (m < odometers) 31 cout << "里程数更新失败!" << endl; 32 else 33 odometers = m; 34 return; 35 } 36 37 #endif
electricCar.hpp
1 #ifndef _ELECTRICCAR_HPP_ 2 #define _ELECTRICCAR_HPP_ 3 #include<iostream> 4 #include"battery.hpp" 5 #include"car.hpp" 6 #include<string> 7 8 class ElectricCar : public Car, public Battery { 9 private: 10 Battery battery; 11 public: 12 ElectricCar(string maker1, string model1, int year1, int c = 70); 13 void info(); 14 }; 15 16 ElectricCar::ElectricCar(string maker1, string model1, int year1, int c) : 17 Car(maker1, model1, year1), Battery(c) {} 18 19 void ElectricCar::info() { 20 Car::info(); 21 cout << "Capacity: " << battery.get_capacity() << "-kWh" << endl; 22 } 23 24 #endif
task3,cpp
1 #include<iostream> 2 #include"electricCar.hpp" 3 int main() { 4 using namespace std; 5 6 Car oldcar("Audi", "a4", 2016); 7 cout << "---------oldcar's info---------" << endl; 8 oldcar.update_odometers(25000); 9 oldcar.info(); 10 11 cout << endl; 12 13 ElectricCar newcar("Tesla", "model s", 2016); 14 newcar.update_odometers(2500); 15 cout << "\n--------newcar's info---------\n"; 16 newcar.info(); 17 18 }
实验结果截图:
任务四:
pets.hpp
1 #ifndef _PETS_HPP_ 2 #define _PETS_HPP_ 3 #include<iostream> 4 #include<cstring> 5 using namespace std; 6 7 class MachinePets { 8 private: 9 string nickname; 10 public: 11 MachinePets(const string s):nickname(s){} 12 virtual string talk() { 13 return "la la la"; 14 } 15 string get_nickname() const {return nickname;} 16 }; 17 18 class PetCats : public MachinePets { 19 public: 20 PetCats(const string s):MachinePets(s){} 21 string talk() { 22 return "miao wu~"; 23 } 24 }; 25 26 class PetDogs : public MachinePets { 27 public: 28 PetDogs(const string s) :MachinePets(s) {} 29 string talk() { 30 return "wang wang~"; 31 } 32 }; 33 34 #endif
task4.cpp
1 #include<iostream> 2 #include"pets.hpp" 3 4 void play(MachinePets * ptr) { 5 std::cout << ptr->get_nickname() << " says " << ptr->talk() << std::endl; 6 } 7 8 int main() { 9 PetCats cat("miku"); 10 PetDogs dog("da huang"); 11 12 play(&cat); 13 play(&dog); 14 }
实验结果截图: