实验4 继承

task2:

程序源码:

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

运行结果:

 归纳总结:

同名覆盖原则

派生类与基类中有相同成员时:
若未强行指名,则通过派生类对象使用的是派生类的同名成员;

如果要通过派生类的对象访问基类被覆盖的同名成员,需要加 对象名.基类名::同名成员 来限定

类型兼容原则

一个公有派生类的对象在使用上可以被当作基类的对象,反之则禁止。

具体表现在:

派生类的对象可以被赋值给基类对象。

派生类的对象可以初始化基类的引用。
基类的对象指针也可以指向派生类。但是如果派生类的指针想要指向基类,则必须要将基类对象的地址强转为派生类类型的。

微调后程序源码:

#include <iostream>
#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);
}

运行结果:

 task3:

源码:

battery.h:

//
// Created by 86178 on 2021/11/24.
//

#ifndef UNTITLED18_BATTERY_H
#define UNTITLED18_BATTERY_H
#include "iostream"
using namespace std;

class Battery {
private:
    int capacity;
public:
    Battery(int cap=70):capacity{cap}{
    };
    ~Battery()=default;
    int get_capacity(){
        return capacity;
    };
    int change(double ba){
        capacity=ba;
    };
};


#endif //UNTITLED18_BATTERY_H

car.h:

//
// Created by 86178 on 2021/11/24.
//

#ifndef UNTITLED18_CAR_H
#define UNTITLED18_CAR_H
#include "iostream"
#include "string"
using namespace std;

class Car {
protected:
    string maker;
    string model;
    int year;
    double odometers;
public:
    Car(string ma,string mo,int y):maker{ma},model{mo},year{y}{
    };
    ~Car()=default;
    void info(){
        cout<<"maker:           "<<maker<<endl;
        cout<<"model:           "<<model<<endl;
        cout<<"year:            "<<year<<endl;
        cout<<"odometers:       "<<odometers<<endl;
    };
    void update_odometers(double a){
        odometers=0;
        if(a<odometers){
            cout<<"Error message,please re-enter"<<endl;
        }
        odometers=a;
    };
};


#endif //UNTITLED18_CAR_H

electricCar.h:

//
// Created by 86178 on 2021/11/24.
//

#ifndef UNTITLED18_ELECTRICCAR_H
#define UNTITLED18_ELECTRICCAR_H
#include "iostream"
#include "string"
#include "car.h"
#include "battery.h"
using namespace std;

class ElectricCar:public Car
{
private:
    Battery battery;
public:
    ElectricCar(string ma,string mo,int y,double ba=70):Car( ma,mo,y){
        battery.change(ba);
    };
    ~ElectricCar()=default;
    void info(){
        cout<<"maker:           "<<maker<<endl;
        cout<<"model:           "<<model<<endl;
        cout<<"year:            "<<year<<endl;
        cout<<"odometers:       "<<odometers<<endl;
        cout<<"capacity:        "<<battery.get_capacity()<<"-kWh"<<endl;
    };
};


#endif //UNTITLED18_ELECTRICCAR_H

task3:

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

int main()
{
    using namespace std;

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

    cout << endl;

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

运行结果:

 

task4:

源码:

pets.h

#ifndef SHIYANSHI_TASK4_PETS_H
#define SHIYANSHI_TASK4_PETS_H
#include "iostream"
#include "string"
using namespace std;

class MachinePets{
private:
    string nickname;
public:
    MachinePets(const string s):nickname(s){};
    ~MachinePets()=default;
    string get_nickname(){
        return nickname;
    }
    virtual string talk(){
        cout<<"talk"<<endl;
    };
};

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

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

#endif //SHIYANSHI_TASK4_PETS_H
task4.cpp

#include <iostream> #include "pets.h" 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 23:08  沈超超  阅读(43)  评论(3编辑  收藏  举报