实验四 继承

1、试验任务1

2、实验任务2

验证性实验。

 

task2.cpp

#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. 类型兼容原则:在需要基类对象的任何地方,都可以使用公有派生类的对象来替代。

 

对代码微调后,运行结果截图:

 

 

对比结果分析:

对Graph::draw前加上virtual关键字,声明该函数为虚函数,那么该函数地址只有在运行时才能确定,这是多态实现的原理。

3、实验任务3

任务描述:使用类的组合和继承模拟简单的车辆信息管理。

Battery.hpp

#ifndef BATTERY_HPP
#define BATTERY_HPP

//电池类Battery
#include<iostream>
#include<cstring>
using namespace std;

class Battery{
    public:
        Battery(int val=70):capacity(val){}
        int get_capacity(){
            return capacity;
        }
    public:
        int capacity;
}; 

#endif

 

Car.hpp

#ifndef CAR_HPP
#define CAR_HPP

//汽车类Car
#include<iostream>
#include<cstring>
using namespace std;

class Car{
    public:
        Car(string maker_val,string model_val,int year_val):
            maker(maker_val),model(model_val),year(year_val),odometers(0){}
        
        void info(){
            cout<<"maker:\t\t"<<maker<<endl;
            cout<<"model:\t\t"<<model<<endl;
            cout<<"year:\t\t"<<year<<endl;
            cout<<"odometers:\t"<<odometers<<endl;
        }
        
        void update_odometers(int x){
            if(x<odometers)cout<<"更新数值有误"<<endl;
            else odometers=x;
        } 
        
    public:
        string maker;
        string model;
        int year;
        int odometers;
}; 

#endif

 

ElectricCar.hpp

#ifndef ELECTRICCAR_HPP
#define ELECTRICCAR_HPP

//电动汽车类ElectricCar
#include<iostream>
#include<cstring>
#include"car.hpp"
#include"battery.hpp"
using namespace std;

class ElectricCar:public Car,public Battery{
    public:
        ElectricCar(string maker_val,string model_val,int year_val):
            Car(maker_val,model_val,year_val),battery(){}
            
        void info(){
            this->Car::info();
            cout<<"capacity:\t"<<battery.get_capacity()<<"-kWh"<<endl;
        }
    public:
        Battery battery;
};

#endif

 

task3.cpp

#include <iostream>
#include "electricCar.hpp"

int main()
{
    using namespace std;

    // test class of Car
    Car oldcar("BMW", "X7", 2016);
    
    oldcar.update_odometers(25000);
    oldcar.update_odometers(12500);
    cout << "--------oldcar's info--------" << endl;
    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、实验任务4

任务描述:使用类的继承,模拟简单的机器宠物。

pets.hpp

#ifndef PETS_HPP
#define PETS_HPP

//宠物类pets 
#include<iostream>
#include<cstring>
using namespace std;

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


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

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

#endif

 

task4.cpp

#include <iostream>
#include "pets.hpp"

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

int main()
{
    PetCats cat("miku");
    PetDogs dog("da huang");

    play(&cat);
    play(&dog);
}

 

运行结果:

 

 

 
posted @ 2021-12-01 15:04  Gpp-ist  阅读(41)  评论(3编辑  收藏  举报