实验4 继承

Posted on 2021-11-25 23:05  Roiery  阅读(19)  评论(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);
}

运行测试截图:

 修改后task2.cpp:

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

运行测试截图:

 

实验任务3:

Battery.hpp:

#ifndef BATTERY_HPP
#define BATTERY_HPP

#include<iostream>

using namespace std;

class Battery{
    public:
        Battery():capacity{70}{}
        Battery(int capacity_):capacity{capacity_}{}
        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 maker_, string model_, int year_):maker{maker_}, model{model_}, year{year_}, odometers{0}{}
        void info(){
            cout << "maker:\t\t\t" << maker << endl;
            cout << "model:\t\t\t" << model << endl;
            cout << "year:\t\t\t" << year << endl;
            cout << "odometers:\t\t" << odometers << endl;
        }
        void update_odometers(int odometers_){
            if(odometers_ < odometers){
                cout << "更新后的里程数有误" << endl;
            }
            else{
                odometers = odometers_;
            }
        }
    private:
        string maker;
        string model;
        int year;
        int odometers;
};

#endif

ElectricCar.hpp:

#ifndef ELECTRICCAR_HPP
#define ELECTRICCAR_HPP
#include "Battery.hpp"
#include "Car.hpp"

#include<iostream>

using namespace std;

//继承Car类 
class ElectricCar : public Car{
    public:
        ElectricCar(string maker_, string model_, int year_):Car(maker_, model_, year_), battery(){}
        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(){}
        string get_nickname(){
            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~";
        }
};

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

运行测试截图:

 

Copyright © 2024 Roiery
Powered by .NET 8.0 on Kubernetes