实验4继承

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

 

同名覆盖原则:派生类和基类具有相同成员时,通过派生类对象访问,默认访问的是派生类的同名成员

二元作用域分辨符(::):用于显式指定使用的是哪个类的重名成员

类型兼容原则:派生类对象也可以当作基类对象来使用,但只能使用从基类继承的那部分功能调用虚函数时,根据指针实际指向的对象,选择相应的函数实现。

 更改virtual后

 

 实验任务3

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

Battery.hpp

 

#ifndef BATTERY_HPP
#define BATTERY_HPP
using namespace std;

class Battery
{
private:
    int capacity; // (kWh)
public:
    Battery(int cap=70):capacity(cap){}
    
    int get_capacity(){
        return capacity;
    }
};
#endif

 

Car.hpp

#ifndef CAR_HPP
#define CAR_HPP

#include <iostream>
#include <string>
 
using namespace std;
 
class Car
{
private:
    string maker;  //制造商
    string model;  //型号
    int year;   //生产年份
    int odometers; //行车里程数
public:
    Car(){};
    Car(string maker, string model, int year,int meters=0) : maker(maker), model(model), year(year), odometers(meters){};
 
    void info()
    {
        cout << "制造商: " << maker << endl
             << "型号: " << model << endl
             << "生产年份: " << year << endl
             << "行车里程数: " << odometers << endl;
    }
 
    void update_odometers(int meters){
        if (meters < odometers)
            cout << "new odometers is wrong!!!" << endl;
        else
            odometers = meters;
    }
};
#endif

ElectricCar.hpp

#ifndef ELECTRICCAR_HPP
#define ELECTRICCAR_HPP

#include "Car.hpp"
#include "Battery.hpp"

using namespace std;

class ElectricCar: public Car
{
private:
    Battery battery;
public:
    ElectricCar(string maker, string model, int year,int cap=70): Car(maker, model, year), battery(cap){};

    void info(){
        Car::info();
        cout << "电池: " << battery.get_capacity() << "kWh"<< endl;
    }
};
#endif

task3.cpp'

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

int main()
{
    using namespace std;

    // test class of Car
    Car oldcar("Audi", "a404", 2048);
    cout << "--------oldcar's info--------" << endl;
    oldcar.update_odometers(25624);
    oldcar.info();

    cout << endl;

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

测试结果:

 

 

 实验任务4
使用类的继承,模拟简单的机器宠物。

pets.hpp

#ifndef PETS_HPP
#define PETS_HPP

using namespace std;
class MachinePets
{
public:
    MachinePets(const string s) : nickname{s} {}
    string get_nickname() const { return nickname; }
    virtual string talk() { return "ze~ze~"; }

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~";
    }
};
#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);
}

测试结果:

 

 

总结与体会

1、派生类构造函数初始化,通过参数化列表,先调用基类构造函数初始化基类,再初始化派生类成员

2、有默认值的形参才可以没有实参

 

 

 

posted @ 2021-11-30 22:26  睡不醒的浅泽  阅读(21)  评论(2编辑  收藏  举报