• 微信公众号：美女很有趣。 工作之余，放松一下，关注即送10G+美女照片！

# 实验4 继承

3小时前 2次浏览

``` 1 #include<iostream>
2 #include<typeinfo>
3 //definitation of Graph
4 class Graph
5 {
6 public:
7     void draw() { std::cout << "Graph::draw():just as an interfacen"; }
8 };
9
10 //definition of Rectangle,derived from Graph
11 class Rectangle :public Graph
12 {
13 public:
14     void draw() { std::cout << "Rectangle::draw():programs of draw a retanglen"; }
15 };
16 //definition of Circle,derived from Graph
17 class Circle :public Graph
18 {
19 public:
20     void draw() { std::cout << "Circle::draw():programs of draw a circlen"; }
21
22 };
23 //definition of fun():as a call interface
24 void fun(Graph* ptr)
25 {
26     std::cout << "pointer type: " << typeid(ptr).name() << "n";
27     std::cout << "RTTI type: " << typeid(*ptr).name() << "n";
28     ptr->draw();
29 }
30 //test
31 int main()
32 {
33     Graph g1;
34     Rectangle r1;
35     Circle c1;
36     //call by object name
37     g1.draw();
38     r1.draw();
39     c1.draw();
40     std::cout << "n";
41     // call by object name, and using the scope resolution operator::
42     r1.Graph::draw();
43     c1.Graph::draw();
44     std::cout << "n";
45     // call by pointer to Base class
46     fun(&g1);
47     fun(&r1);
48     fun(&c1);
49 }```

归纳总结：

实验任务3

``` 1 //task3.cpp
2 #include<iostream>
3 #include "electricCar.hpp"
4 int main()
5 {
6     using namespace std;
7     // test class of Car
8     Car oldcar("Audi", "a4", 2016);
9     cout << "--------oldcar's info--------" << endl;
10     oldcar.update_odometers(25000);
11     oldcar.info();
12     cout << endl;
13     // test class of ElectricCar
14     ElectricCar newcar("Tesla", "model s", 2016);
15     newcar.update_odometers(2500);
16     cout << "n--------newcar's info--------n";
17     newcar.info();
18 }```
``` 1 //electricCar.hpp
2 #pragma once
3 #include<iostream>
4 #include"car.hpp"
5 #include"battery.hpp"
6 using namespace std;
7 class ElectricCar :public Car
8 {
9 public:
10     ElectricCar(string maker0, string model0, int year0) :Car(maker0, model0, year0), battery() {}
11     void info()
12     {
13         cout << left << setw(20) << "maker:" << maker << endl;
14         cout << left << setw(20) << "model:" << model << endl;
15         cout << left << setw(20) << "year:" << year << endl;
16         cout << left << setw(20) << "odometers:" << odometers << endl;
17         cout << left << setw(20) << "capacity:" << battery.get_capacity() << "-kwh" << endl;
18     }
19 private:
20     Battery battery;
21 };```
``` 1 //battery.hpp
2 #pragma once
3 #include<iostream>
4 class Battery
5 {
6 public:
7     Battery() :capacity(70) {}
8     int get_capacity() { return capacity; }
9 protected:
10     double capacity;
11 };```
``` 1 //car.hpp
2 #pragma once
3 #include<iostream>
4 #include<iomanip>
5 using std::cout;
6 using std::cin;
7 using std::endl;
8 using std::string;
9 using std::setw;
10 using std::left;
11 class Car
12 {
13 public:
14     Car(string maker0,string model0,int year0):maker(maker0),model(model0),year(year0),odometers(0) {}
15     void info()
16     {
17         cout << left << setw(20) << "maker:" << maker << endl;
18         cout << left << setw(20) << "model:" << model << endl;
19         cout << left << setw(20) << "year:" << year << endl;
20         cout << left << setw(20) << "odometers:" << odometers << endl;
21     }
22     void update_odometers(int n)
23     {
24         int temp = odometers;
25         odometers = n;
26         if (odometers < temp)
27             cout << "warning,输入数据有误!" << endl;
28     }
29     string maker;
30     string model;
31     int year;
32     int odometers;
33 };```