实验四 继承

实验任务三

car.hpp

#ifndef car_hpp
#define car_hpp
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Car
{
private:
    string maker;
    string model;
    int year;
    int odometers;

public:
    Car(string maker, string model, int year, int odometers = 0);
    void info();
    void update_odometers(int km);
};

Car::Car(string maker, string model, int year, int odometers) : maker(maker), model(model), year(year), odometers(odometers)
{
}

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

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

battery.hpp

#ifndef battery_hpp
#define battery_hpp

class battery
{
private:
    int capacity;

public:
    battery(int capacity = 70);
    int get_capacity() const;
};

battery::battery(int capacity) : capacity(capacity)
{
}

int battery::get_capacity() const
{
    return capacity;
}
#endif

electricCar.hpp

#ifndef electricCar_hpp
#define electricCar_hpp

#include "car.hpp"
#include "battery.hpp"

class ElectricCar : public Car
{
private:
    battery battery;

public:
    ElectricCar(string maker, string model, int year, int capacity = 70);
    void info();
};

ElectricCar::ElectricCar(string maker, string model, int year, int capacity) : Car(maker, model, year), battery(capacity)
{
}

void ElectricCar::info()
{
    Car::info();
    cout << left << setw(15) << "capacity:" << 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", "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();
}

实验结果截图

 


实验任务四

pets.hpp

#ifndef pet_hpp
#define pet_hpp

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

// 机器宠物类MachinePets
class MachinePets
{
private:
    string nickname;

public:
    MachinePets(const string s) : nickname(s) {}

    string get_nickname() const
    {
        return nickname;
    }

    virtual string talk()
    {
        return "listen";
    }
};

// 电子宠物猫类PetCats
class PetCats : public MachinePets
{
public:
    PetCats(const string s) : MachinePets(s) {}
    string talk()
    {
        return "miao wu ~";
    }
};

// 电子宠物狗类PetDogs
class PetDogs : public MachinePets
{
public:
    PetDogs(const string s) : MachinePets(s) {}
    string talk()
    {
        return "wang 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);
}

实验结果截图

 

 

posted @ 2021-11-24 20:08  请去看诡秘之主  阅读(34)  评论(3编辑  收藏  举报