其他分享
首页 > 其他分享> > 实验4 继承

实验4 继承

作者:互联网

实验任务3

"battery.hpp"

 1 #ifndef TEST01_BATTERY_HPP
 2 #define TEST01_BATTERY_HPP
 3 
 4 #include <iostream>
 5 using namespace std;
 6 
 7 class Battery {
 8 public:
 9     Battery (int cap = 70): capacity(cap) {};
10     int get_capacity() {
11         return capacity;
12     }
13 private:
14     int capacity;
15 };
16 
17 #endif

"car.hpp"

 1 #ifndef TEST01_CAR_HPP
 2 #define TEST01_CAR_HPP
 3 
 4 #include <iostream>
 5 #include <string>
 6 using namespace std;
 7 
 8 class Car {
 9 public:
10     Car (string ma, string mo, int ye, int odo = 0): maker(ma), model(mo), year(ye), odometers(odo) {};
11     void info() {
12         cout << "maker:             " << maker << endl;
13         cout << "model:             " << model << endl;
14         cout << "year:              " << year << endl;
15         cout << "odometers:         " << odometers << endl;
16     }
17     void update_odometers(int new_odo) {
18         if (new_odo >= odometers)
19             odometers = new_odo;
20         else
21             cout << "odometers updating error!" << endl;
22     }
23 protected:
24     string maker;
25     string model;
26     int year;
27     int odometers;
28 };
29 
30 #endif

"electricCar.hpp"

 1 #ifndef TEST01_ELECTRICCAR_HPP
 2 #define TEST01_ELECTRICCAR_HPP
 3 
 4 #include <iostream>
 5 #include "battery.hpp"
 6 #include "car.hpp"
 7 using namespace std;
 8 
 9 class ElectricCar: public Car {
10 public:
11     ElectricCar(string ma, string mo, int ye, int odo = 0);
12     void info();
13 private:
14     Battery battery;
15 };
16 
17 ElectricCar::ElectricCar(string ma, string mo, int ye, int odo): Car(ma, mo, ye), battery() {};
18 
19 void ElectricCar::info() {
20     cout << "maker:             " << maker << endl;
21     cout << "model:             " << model << endl;
22     cout << "year:              " << year << endl;
23     cout << "odometers:         " << odometers << endl;
24     cout << "capacity:          " << battery.get_capacity() << endl;
25 }
26 
27 #endif

"task3.cpp"

 1 #include <iostream>
 2 #include "electricCar.hpp"
 3 
 4 int main() {
 5     using namespace std;
 6 
 7     //test class of Car
 8     Car oldcar("Cadillac", "CT5", 2021);
 9     cout << "--------oldcar's info--------" << endl;
10     oldcar.update_odometers(21000);
11     oldcar.info();
12 
13     cout << endl;
14 
15     //test class of ElectricCar
16     ElectricCar newcar("Toyota", "bZ4X", 2022);
17     newcar.update_odometers(3700);
18     cout << "\n--------newcar's info--------\n";
19     newcar.info();
20 }

运行结果

 

 

 

 

实验任务4

"pets.hpp"

 

 1 #ifndef INC_4_PETS_HPP
 2 #define INC_4_PETS_HPP
 3 
 4 #include <iostream>
 5 #include <string>
 6 using namespace std;
 7 
 8 class MachinePets {
 9 public:
10     MachinePets(string nic_na);
11     string get_nickname() const;
12     virtual string talk();
13 protected:
14     string nickname;
15 };
16 
17 MachinePets::MachinePets(string nic_na): nickname(nic_na) {}
18 
19 string MachinePets::get_nickname() const {
20     return nickname;
21 }
22 
23 string MachinePets::talk() {
24     string sound = "";
25     return sound;
26 }
27 
28 class PetCats: public MachinePets {
29 public:
30     PetCats(string cats_na);
31     string talk();
32 };
33 
34 PetCats::PetCats(string cats_na): MachinePets(cats_na) {}
35 
36 string PetCats::talk() {
37     return "miao wu~";
38 }
39 
40 class PetDogs: public MachinePets {
41 public:
42     PetDogs(string dogs_na);
43     string talk();
44 };
45 
46 PetDogs::PetDogs(string dogs_na): MachinePets(dogs_na) {}
47 
48 string PetDogs::talk() {
49     return "wang wang~";
50 }
51 
52 #endif

"task4.cpp"

 1 #include <iostream>
 2 #include "pets.hpp"
 3 using namespace std;
 4 
 5 void play(MachinePets *ptr) {
 6     cout << ptr->get_nickname() << " says " << ptr->talk() << endl;
 7 }
 8 
 9 int main() {
10     PetCats cat("miku");
11     PetDogs dog("da huang");
12 
13     play(&cat);
14     play(&dog);
15 }

运行结果

 

标签:string,继承,na,int,实验,MachinePets,include,public
来源: https://www.cnblogs.com/wangJW0209/p/15599766.html