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

实验四 继承

作者:互联网

实验任务 二

tsak2.cpp文件源码

#include <typeinfo>

// definitation of Graph
class Graph
{
public:
   virtual void draw() { std::cout << "Graph::draw() : just as an interface\n"; }
};


// definition of Rectangle, derived from Graph
class Rectangle : public Graph
{
public:
    void draw() { std::cout << "Rectangle::draw(): programs of draw a rectangle\n"; }
};


// definition of Circle, derived from Graph
class Circle : public Graph
{
public:
    void draw() { std::cout << "Circle::draw(): programs of draw a circle\n"; }
};


// definitaion of fun(): as a call interface
void fun(Graph *ptr)
{
    std::cout << "pointer type: " << typeid(ptr).name() << "\n";
    std::cout << "RTTI type: " << typeid(*ptr).name() << "\n";
    ptr -> draw();
}

// test 
int main()
{
    Graph g1;
    Rectangle r1;
    Circle c1;

    // call by object name
    g1.draw();
    r1.draw();
    c1.draw();

    std::cout << "\n";

    // call by object name, and using the scope resolution operator::
    r1.Graph::draw();
    c1.Graph::draw();

    std::cout << "\n";

    // call by pointer to Base class
    fun(&g1);
    fun(&r1);
    fun(&c1);
}

 

 

总结:

  同名覆盖原则:LINE7、15、23分别是3个同名函数的定义,在调用时,如果像LINE42、43、44那样不添加作用域,直接调用的话,调用的是继承类中的同名函数,即继承类中同名函数将基类同名       函数覆盖。

  二元作用域分辨符:LINE7、15、23分别是3个同名函数的定义,在调用时,如果像LINE46、47那样添加作用域调用的话,调用的是对应作用域中的同名函数。

  类型兼容原则:LINE28中,定义了形参的类型是指向Graph类型的指针,所以不管传入的是rectangle类的对象地址还是circle类的对象地址,都只能访问基类graph中继承来的公有成员。

  基类Graph的成员函数draw()前面加关键字virtual时

 实验任务 三

Car.hpp

1 #ifndef CAR_HPP
 2 #define CAR_HPP
 3 #include <iostream>
 4 #include <string>
 5 
 6 using namespace std;
 7 
 8 class Car
 9 {
10 public:
11     Car(string maker0,string model0,int year0,int odometers0=0):maker(maker0),model(model0),year(year0),odometers(odometers0){ };
12     void info()const;
13     void update_odometers(int new_odometers);
14 private:
15     string maker,model;
16     int year,odometers;
17 };
18 void Car::info()const
19 {
20     cout << "maker:\t\t" << maker <<endl;
21     cout << "model:\t\t" << model <<endl;
22     cout << "year:\t\t" << year <<endl;
23     cout << "odometers:\t" << odometers <<endl;
24 }
25 void Car::update_odometers(int new_odometers)
26 {
27     if(new_odometers<odometers)
28         cout << "数据错误,请重新输入" << endl;
29     odometers=new_odometers;
30 }
31 #endif // CAR_HPP

Battery.hpp

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

EletricCar.hpp

1 #ifndef ElECTRIC_CAR_HPP
 2 #define ElECTRIC_CAR_HPP
 3 #include "Car.hpp"
 4 #include "Battery.hpp"
 5 #include <iostream>
 6 #include <string>
 7 
 8 using namespace std;
 9 
10 class ElectricCar:public Car
11 {
12 public:
13     ElectricCar(string maker0,string model0,int year0,int odometers0=0,int capacity0=70):Car(maker0,model0,year0,odometers0),battery(capacity0) {};
14     void info() ;
15 private:
16     Battery battery;
17 };
18 void ElectricCar:: info()
19 {
20     Car::info();
21     cout << "capacity:\t" << battery.get_capacity() << endl;
22 }
23 #endif // ElECTRIC_CAR_HPP

main.hpp

1 #include <iostream>
 2 #include "electricCar.hpp"
 3 int main()
 4 {
 5     using namespace std;
 6     // test class of Car
 7     Car oldcar("Audi", "a4", 2016);
 8     cout << "--------oldcar's info--------" << endl;
 9     oldcar.update_odometers(25000);
10     oldcar.info();
11     cout << endl; // test class of ElectricCar
12     ElectricCar newcar("Tesla", "model s", 2016);
13     newcar.update_odometers(2500);
14     cout << "\n--------newcar's info--------\n";
15     newcar.info();
16 }

 

 实验任务四

Pets.hpp

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

main.hpp

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 int main()
 9 {
10     PetCats cat("miku");
11     PetDogs dog("da huang");
12     play(&cat);
13     play(&dog);
14 }

 

标签:string,继承,Car,hpp,int,实验,include,public
来源: https://www.cnblogs.com/sthcnblgs/p/15624315.html