实验四 继承

"Battery.hpp"

#ifndef BATTERY_HPP
#define BATTERY_HPP

#include <bits/stdc++.h>
using namespace std;
class Battery
{
    public:
        Battery(int _capacity = 70 ):capacity(_capacity){}
        void get_capacity() const;
    private:
        int capacity;
};

void Battery::get_capacity() const
{
    cout << "capacity:      " << capacity << "-kWh" << endl;
}

#endif

"car.hpp"

#ifndef CAR_HPP
#define CAR_HPP


#include <bits/stdc++.h>
using namespace std;
class Car
{
    public:
        Car(string a="",string b = "", int c = -1, int d = 0):maker(a),model(b),year(c),odometers(d){}
        void info();
        void update_odometers(int a);
    protected:
        string maker;
        string model;
        int year;
        int odometers;
};

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

void Car::update_odometers(int a)
{
    if(a < odometers) cout << "ERROR!"<<endl;
    else odometers = a;
}
#endif

"electricCar.hpp"

#ifndef ELECTRICCAR_HPP
#define ELECTRICCAR_HPP

#include "car.hpp"
#include "Battery.hpp"
#include <iostream>
using namespace std;

class ElectricCar:public Car
{
    public:
        ElectricCar(string a="",string b="", int c=-1, int d=0,int e = 70):Car(a,b,c,d),battery(e){}
        void info();
    private:
        Battery battery;
};
void ElectricCar::info()
{
    Car::info();
    battery.get_capacity();
}

#endif

"task3.cpp"

#include <iostream>
#include "electricCar.hpp"
using namespace std;


int main()
{
    using namespace std;

    // test class of Car
    Car oldcar("audi", "A4", 2077);
    cout << "--------oldcar's info--------" << endl;
    oldcar.update_odometers(25000);
    oldcar.info();

    cout << endl;

    // test class of ElectricCar
    ElectricCar newcar("Tesla", "model S+", 2078);
    newcar.update_odometers(2500);
    cout << "\n--------newcar's info--------\n";
    newcar.info();
}

"运行截图"

 

 

 

 

 

"pets.hpp"

#ifndef PETS_HPP
#define PETS_HPP

#include <bits/stdc++.h>
using namespace std;

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

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

};

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

#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-11-24 17:26  人生何处不青山L  阅读(44)  评论(3编辑  收藏  举报