ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

实验4 继承

2021-11-24 20:01:08  阅读:115  来源: 互联网

标签:string 继承 na int 实验 MachinePets include public


实验任务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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有