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

实验4 继承

作者:互联网

source code :

 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 }

 

The Running results :

 

 

 对代码稍作改动,在基类Graph的成员函数draw()前面加一个关键字virtual,重新编译运行程序,再次观察结果,对比一下

 

如果派生类中声明了与基类成员函数同名的新函数,即使函数的参数表不同,从基类继承的同名函数的所有重载形式也都会被隐藏。

source code :

 1 #include"battery.hpp"
 2 #include"car.hpp"
 3 #include<iomanip>
 4 
 5 class ElectricCar:public Car{
 6     public:
 7         ElectricCar(string s1,string s2,int a,int b=0,int ca=70):Car(s1,s2,a,b),battery(ca){};
 8         void info() const {
 9             Car::info();
10             cout<<left<<setw(20)<<"capacity: "<<battery.get_capacity()<<"-kWh"<<endl;
11         }
12     private:
13             Battery battery;    
14 };
 1 #include<iostream>
 2 #include<string>
 3 
 4 using namespace std;
 5 
 6 class Battery{
 7     public:
 8         Battery(int b=70):capacity(b){}; //Battery
 9         int get_capacity() const{return capacity;}
10     private:
11         int capacity;
12 };
 1 #include<iostream>
 2 #include<string>
 3 
 4 #include<iomanip>
 5 
 6 using namespace std;
 7 
 8 class Car{
 9     
10     public:
11         Car(string s1,string s2,int a,int b=0):maker(s1),model(s2),year(a),odometers(b){};
12         void info() const {
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         }
18         void update_odometers(int meters){
19             if(meters<odometers){
20                 cout<<"incorrect number! "<<endl;
21             }
22                 
23             else{
24                 odometers=meters;
25             }
26                 
27         }
28     private:
29             string maker;
30             string model;
31             int year, odometers;
32 };

 

The Running results :

 

 

source code :

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

 

The Running results :

 

标签:const,string,继承,int,实验,MachinePets,include,public
来源: https://www.cnblogs.com/zmqh/p/15628972.html