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

对里程输入错误的截图:

实验4 继承

替换数据的截图:

 实验4 继承

 

 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 }

替换数据后的截图:

实验4 继承

 

 

上一篇:实验四 继承


下一篇:实验四 继承