实验4 继承

实验任务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);
}

归纳总结:  

  同名覆盖原则:派生类与基类中有相同成员/函数时,若未强行指名,则通过派生类对象访问的是派生类的同名成员/函数。

  二元作用域分辨符:如果要通过派生类的对象访问基类被覆盖的同名成员/函数,需要使用 对象名.基类名::同名成员/函数 来限定。

  类型兼容原则:派生类的对象可以被赋值给基类对象;派生类的对象可以初始化基类的引用;基类的对象指针可以指向派生类。

在基类Graph的成员函数draw()前面加一个关键字virtual,重新编译运行后的结果:

观察结果可知,通过在Graph类的接口draw(),不加/加关键字virtual,运行时ptr实际指向的对象不同。

当基类的函数为虚函数的时候,可以通过基类去访问子类的函数。

 

实验任务3

Battery.hpp

#ifndef BATTERY_HPP
#define BATTERY_HPP

class Battery{
public:
    Battery(double capacity0 = 70);
    double get_capacity() const;

private:
    double capacity;
};

Battery::Battery(double capacity0): capacity{capacity0} {

}

double Battery::get_capacity() const {
    return capacity;
}

#endif

Car.hpp

#ifndef CAR_HPP
#define CAR_HPP

#include<string>
using namespace std;

class Car{
public:
    Car(string maker0, string model0, int year0, double odometers0 = 0);
    void info() const;
    void update_odometers(double odo);

private : 
    string maker;
    string model;
    int year;
    double odometers;
};

Car::Car(string maker0, string model0, int year0, double odometers0): maker{maker0}, model{model0}, year{year0}, odometers{odometers0} {

}

void Car::info() const {
    cout << "maker:\t\t\t" << maker << endl
         << "model:\t\t\t" << model << endl
         << "year:\t\t\t" << year << endl
         << "odometers:\t\t" << odometers << endl;
}

void Car::update_odometers(double odo) {
     if(odo < odometers){
         cout << "更新后的行车里程数值小于当前行车里程数!!!更新数值有误" << endl;
     }else{
         odometers = odo;
     }
}

#endif

ElectricCar.hpp

#ifndef ELECTRICCAR_HPP
#define ELECTRICCAR_HPP

#include "Battery.hpp"
#include "Car.hpp"
using namespace std;

class ElectricCar : public Car {

public:
    ElectricCar(string maker0, string model0, int year0, double odometers0 = 0, double capacity0 = 70);
    void info() const;

private : 
    Battery battery;
};

ElectricCar::ElectricCar(string maker0, string model0, int year0, double odometers0, double capacity0): Car{maker0, model0, year0, odometers0}, battery{capacity0} {

}

void ElectricCar::info() const {
    Car::info();
    cout << "capacity:\t\t" << battery.get_capacity() << "-kWh" << endl;
}

#endif

task3.cpp

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

int main()
{
    using namespace std;

    // test class of Car
    Car oldcar("BMW", "x3", 2019);
    cout << "--------oldcar's info--------" << endl;
    oldcar.update_odometers(30000);
    oldcar.info();

    cout << endl;

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

运行测试截图:

 

实验任务4

pets.hpp

#include<string>
#include<iostream>
using namespace std;

class MachinePets{
public:
    MachinePets(const string s) : nickname{s} {}
    virtual string talk() {
        return "";
    }
    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~";
    }
};

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-11-24 22:39  ╮君颜~ઇଓ  阅读(30)  评论(2编辑  收藏  举报