实验4 继承
//Battery.hpp
1 #include<iostream> 2 using namespace std; 3 class Battery{ 4 private: 5 int capacity; 6 public: 7 Battery(int Capacity=70):capacity{Capacity}{} 8 Battery(const Battery &obj):capacity{obj.capacity}{} 9 ~Battery()=default; 10 int get_capacity()const; 11 }; 12 int Battery::get_capacity()const 13 { 14 return capacity; 15 }
//Car.hpp
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 class Car{ 5 private: 6 string maker,model; 7 int year,odometers; 8 public: 9 Car(string Maker,string Model,int Year,int Odometers=0):maker{Maker},model{Model},year{Year},odometers{Odometers}{}; 10 Car(const Car &obj):maker{obj.maker},model{obj.model},year{obj.year},odometers{obj.odometers}{} 11 ~Car()=default; 12 void info(); 13 void update_odometers(int ODO); 14 15 16 }; 17 void Car::info() 18 { 19 cout<<"maker: "<<maker<<endl; 20 cout<<"model: "<<model<<endl; 21 cout<<"year: "<<year<<endl; 22 cout<<"odometers: "<<odometers<<endl; 23 } 24 void Car::update_odometers(int ODO) 25 { 26 if(ODO>=odometers) 27 { 28 odometers=ODO; 29 } 30 else{ 31 cout<<"里程输入错误!!!"<<endl; 32 } 33 }
//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 { 8 private: 9 Battery battery; 10 public: 11 ElectricCar(string Maker,string Model,int Year,int Odometers=0,int Ecapacity=70): 12 Car{Maker,Model,Year,Odometers},battery{Ecapacity}{} 13 14 void info(){ 15 Car::info(); 16 cout<<"cacacity: "<<battery.get_capacity()<<"-kWh"<<endl; 17 } 18 };
//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("Ferrari", "SF90", 2020); 10 cout << "--------oldcar's info--------" << endl; 11 oldcar.update_odometers(2500); 12 oldcar.info(); 13 //oldcar.update_odometers(2000); 14 cout << endl; 15 16 //test class of ElectricCar 17 ElectricCar newcar("McLaren", "Artura", 2021); 18 newcar.update_odometers(2500); 19 cout << "\n--------newcar's info--------\n"; 20 newcar.info(); 21 }
对里程输入错误的截图:

替换数据的截图:

task4
//pets.hpp
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 class MachinePets{ 5 private: 6 string nickname; 7 public: 8 MachinePets(const string s) : nickname(s) {} 9 virtual string talk(); 10 string get_nickname(); 11 12 }; 13 string MachinePets::get_nickname(){ 14 return nickname; 15 } 16 class PetDogs: public MachinePets{ 17 private: 18 string miaomu; 19 public: 20 PetDogs(const string s):MachinePets(s){} 21 string talk(); 22 }; 23 class PetCats: public MachinePets{ 24 private: 25 string wang; 26 public: 27 PetCats(const string s):MachinePets(s){} 28 string talk(); 29 }; 30 string MachinePets::talk() 31 { 32 return "?!?!?!"; 33 } 34 string PetCats::talk() 35 { 36 return "I need more Lasagnas"; 37 } 38 string PetDogs::talk() 39 { 40 return "In your dreams"; 41 }
//task4
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("Odie"); 13 14 play(&cat); 15 play(&dog); 16 }
替换数据后的截图:

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 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 }

修改后:
1 #include <iostream> 2 #include <typeinfo> 3 4 // definitation of Graph 5 class Graph 6 { 7 public: 8 virtual 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 }

思考:
归纳总结:
1.同名覆盖原则:
若派生类中声明了与基类成员函数同名的新函数,即使函数的参数表不同,从基类继承的同名函数的所有重载形式也都会被隐藏。如果某派生类的多个基类拥有同名的成员,同时,派生类有新增这样的同名成员,这种情况下,派生类成员将隐藏所有基类的同名成员。
2.二元作用域分辨符:
当派生类新增成员隐藏了基类的同名成员,这时使用”对象名.成员名“的访问方式只能访问到派生类新增的成员。对基类同名成员的访问,只能通过基类名和作用域分辨符来实现。
3.类型兼容原则:
类型兼容规则是指在需要基类对象的任何地方,都可以使用公有派生类的对象来替代。通过公有继承,派生类得到了基类中除构造函数、析构函数之外的所有成员。公有派生类实际就具备了基类的所有功能,凡是基类能解决的问题,公有派生类都可以解决。
浙公网安备 33010602011771号