实验四 继承

 

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

battery.hpp代码如下:

#ifndef BATTERY_HPP
#define BATTERY_HPP

#include<iostream>

class Battery {
public:
    Battery(int capacity0 = 70) :capacity(capacity0) {}
    int get_capacity() { return capacity; }
private:
    int capacity;
};

#endif

 car.hpp代码如下:

#ifndef CAR_HPP
#define CAR_HPP

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

class Car {
public:
    Car(string maker0, string model0, int year0, int odometers0 = 0) :maker(maker0), model(model0), year(year0), odometers(odometers0) {}
    virtual void info();
    void update_odometers(int new_odometers);
protected:
    string maker;
    string model;
    int year;
    int odometers;
};

void Car::info() {
    using namespace std;

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

void Car::update_odometers(int new_odometers) {
    using namespace std;

    if (new_odometers < odometers)
        cout << "对不起,更新后的里程数要大于等于更新前的里程数." << endl;
    else
        odometers = new_odometers;
}

#endif

electricCar.hpp代码如下:

#ifndef ELECTRICCAR_HPP
#define ELECTRICCAR_HPP

#include<iostream>
#include"battery.hpp"
#include"car.hpp"
#include<iomanip>

class ElectricCar:public Car {
public:
    ElectricCar(string maker0, string model0, int year0, int capacity) ;
    virtual void info();
private:
    Battery battery;
};

ElectricCar::ElectricCar(string maker0, string model0, int year0,int capacity=70):Car(maker0,model0,year0), battery(capacity) {};

void ElectricCar::info() {
    using namespace std;
    cout << setw(15) << left << "maker:" << maker << endl;
    cout << setw(15) << left << "model:" << model << endl;
    cout << setw(15) << left << "year:" << year << endl;
    cout << setw(15) << left << "odometers:" << odometers << endl;
    cout << setw(15) << left << "capacity:" << battery.get_capacity () << "-kWh" << endl;
}

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

pets.hpp代码如下:

#include<iostream>
#include<string>
using std::string;

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

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

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

测试结果如下:

 

 实验总结

通过这次实验体会到protect数据成员在派生类中的应用,同时要注意在派生类中构造函数初始化的方法。

posted @ 2021-11-28 14:34  voidsora  阅读(28)  评论(2编辑  收藏  举报