实验四 继承

1.实验任务2

验证性实验。

在C++编码环境中,输入task2.cpp。

task2.cpp:

 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 }

运行结果截图:

 

添加virtual后的运行结果截图:

 

 归纳总结:

a.同名覆盖原则:

派生类与基类中有相同成员时,通过派生类对象使用的是派生类的同名成员;

可以通过对象名.基类名::同名成员的方式,访问基类被覆盖的同名成员。

b.二元作用域分辨符:

可以告之编译器成员属于哪个类。

c.类型兼容原则:

1.派生类的对象可以隐含转换为基类的对象。
2.派生类的对象可以初始化基类的引用。
3.派生类的指针可以隐含转化为基类的指针。

2.实验任务3

程序包括Battery.hpp, Car.hpp, ElectricCar.hpp, task3.cpp。

Battery.hpp:

 1 #ifndef BATTERY_HPP
 2 #define BATTERY_HPP
 3 #include <iostream>
 4 using std::cout;
 5 using std::endl;
 6 class Battery {
 7 public:
 8     Battery(int n=70) :capacity(n) {};
 9     double get_capacity() const { return capacity; }
10     ~Battery() {};
11 private:
12     double capacity;
13 
14 };
15 
16 
17 #endif
View Code

Car.hpp:

 1 #ifndef CAR_HPP
 2 #define CAR_HPP
 3 #include <iostream>
 4 
 5 using namespace std;
 6 class Car {
 7 public:
 8     Car(string a, string b, int c, double d = 0) :maker(a), model(b), year(c), odometers(d) {};
 9     void info()
10     {
11         cout << "maker:\t\t" << maker << endl;
12         cout << "model\t\t" << model << endl;
13         cout << "year:\t\t" << year << endl;
14         cout << "odometers:\t" << odometers << endl;
15     }
16     void update_odometers(double n) ;
17     ~Car() {};
18 
19 private:
20     string maker, model;
21     int year;
22     double odometers;
23 
24 };
25 
26 void Car::update_odometers(double n)
27 {
28     if (n > odometers)
29         odometers = n;
30     else cout << "更新数值有误" << endl;
31 }
32 
33 #endif
View Code

ElectricCar.hpp:

 1 #ifndef ELECTRICCAR_HPP
 2 #define ELECTRICCAR_HPP
 3 #include "car.hpp"
 4 #include "battery.hpp"
 5 #include<iostream>
 6 using namespace std;
 7 class ElectricCar: public Car {
 8 public:
 9     ElectricCar(string a, string b, int c, int  d = 70) :Car(a, b, c), battery(d) {};
10     void info() {
11         Car::info();
12         cout << "capacity:\t" << battery.get_capacity() <<"-kwh" << endl;
13     }
14     ~ElectricCar() {};
15 private:
16     Battery battery;
17 
18 
19 
20 };
21 
22 #endif
View Code

 

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("Audi", "a4", 2016);
10     cout << "--------oldcar's info--------" << endl;
11     oldcar.update_odometers(25000);
12     oldcar.info();
13 
14     cout << endl;
15 
16     // test class of ElectricCar
17     ElectricCar newcar("legion", "b5", 2020);
18     newcar.update_odometers(4396);
19     cout << "\n--------newcar's info--------\n";
20     newcar.info();
21 }
View Code

运行截图:

 

 3.实验任务4

程序包括pets.hpp, task4.cpp。

pets.hpp:

 1 #ifndef PETS_HPP
 2 #define PETS_HPP
 3 #include <iostream>
 4 using namespace std;
 5 class MachinePets {
 6 public:
 7         MachinePets(const string s):nickname(s) {};
 8         virtual  string talk() { return "1"; }
 9         string get_nickname() { return nickname; }
10 private:
11     string nickname;
12 };
13 class PetCats:public MachinePets {
14 public:
15     PetCats(const string s) :MachinePets(s) {};
16     string talk() {
17         return "miao wu~";
18     }
19 };
20 class PetDogs :public MachinePets {
21 public:
22     PetDogs(const string s) :MachinePets(s) {};
23     string talk() {
24         return "wang wang~";
25     }
26 };
27 
28 
29 
30 #endif
View Code

 

task4.cpp:

 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("miku");
12     PetDogs dog("da huang");
13 
14     play(&cat);
15     play(&dog);
16 }
View Code

运行截图:

 

posted @ 2021-11-26 19:14  热橙汁  阅读(25)  评论(3编辑  收藏  举报