实验四 继承

实验结论:

实验任务2:

源代码:

#include <iostream>
#include <typeinfo>

// definitation of Graph
class Graph
{
public:
    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);
}

 

运行截图:

 

 

对代码微调后运行截图:

 

 

归纳总结:

(1)公有派生类的对象可以用来在需要基类的对象的地方替代使用,但替代之后只能使用从基类继承的成员。

(2)对于同名的成员,访问派生类或基类的同名成员时,需加作用符,即 对象名.类名::成员名,否则以 对象名.成员名 的形式访问的是派生类的同名成员。

(3)在多重继承下,为了访问基类或派生类中同名成员,需要定义虚基类。

 

实验任务3:

Battery.hpp代码:
#ifndef Battery_hpp
#define Battery_hpp
#include <iostream>
using namespace std;

class Battery{
    public:
        Battery();
        Battery(int capacity0):capacity(capacity0){}
        
        int get_capacity()
        {
            return capacity;
        }
    private:
        int capacity;
};  

#endif

 

Car.hpp代码:

 

#ifndef Car_hpp
#define Car_hpp
#include <iostream>
using namespace std;

class Car{
    public:
        Car(string maker0,string model0,int year0,int odometers0=0):maker(maker0),model(model0),year(year0),odometers(odometers0){}
        void info()
        {
            cout<<"maker: \t\t\t"<<maker<<endl;
            cout<<"model: \t\t\t"<<model<<endl;
            cout<<"year: \t\t\t"<<year<<endl;
            cout<<"odeometers: \t\t"<<odometers<<endl;
        }
        void update_odometers(int odometers0)
        {
            if(odometers0<odometers)
            {
            cout<<endl;
            cout<<"warning! 更新数值有误"<<endl;
            cout<<endl;
            }
            else
            odometers=odometers0;
        }
    private:
        string maker;
        string model;
        int year;
        int odometers;
        
};

#endif

 

ElectricCar.hpp代码:
#ifndef ElectricCar_hpp
#define ElectricCar_hpp
#include "Car.hpp"
#include "Battery.hpp"
#include <iostream>
using namespace std;

class ElectricCar:public Car{
    public:
    ElectricCar(string a,string b,int c,int d=70):Car(a,b,c),battery(d){}
    void info()
    {
        Car::info();
        cout<<"capacity: \t\t"<<battery.get_capacity()<<"-kwh"<<endl;
    }
    private:
        Battery battery;
};

#endif

 

task3.cpp代码:
#include <iostream>
#include "ElectricCar.hpp"

int main()
{
    using namespace std;

    // test class of Car
    Car oldcar("Audi", "a4", 2016);
    cout << "--------oldcar's info--------" << endl;
    oldcar.update_odometers(25000);
    oldcar.info();

    cout << endl;

    // test class of ElectricCar
    ElectricCar newcar("Tesla", "model s", 2016);
    newcar.update_odometers(2500);
    cout << "\n--------newcar's info--------\n";
    newcar.info();
}

 

运行截图:

 

 

 

 

 

 

实验任务4:
pets.hpp代码:
#ifndef pets_hpp
#define pets_hpp
#include<iostream>
using namespace std;

class MachinePets{
    public:
        MachinePets(const string s):nickname(s){}
        virtual string talk(){}
        void play();
        
        string get_nickname() const
        {
            return nickname;
        }
        
    private:
        string nickname;
};

class PetCats:public MachinePets{
    public:
        PetCats(const string s):MachinePets(s){}
        
        string talk()
        {
            return "miao wu~";
        }
};

class PetDogs:public MachinePets{
    public:
        PetDogs(const string s):MachinePets(s){}
        
        string talk()
        {
            return "wang wang~";
        }
};

void play(MachinePets *ptr)
{
    std::cout<<ptr->get_nickname()<<" says "<<ptr->talk()<<std::endl;
}

#endif

 

task4.cpp代码:
#include "pets.hpp"
#include<iostream>

int main()
{
    PetCats cat("miku");
    PetDogs dog("dahuang");
    
    play(&cat);
    play(&dog);
}

 

运行截图:

 

 


 

 

posted @ 2021-11-25 08:40  EliaukYob  阅读(45)  评论(2编辑  收藏  举报