其他分享
首页 > 其他分享> > 实验四 继承

实验四 继承

作者:互联网

实验任务三

1、Battery类:

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

class Battery
{
private:
    int capacity;

public:
    Battery(int capacity);
    ~Battery();
    int get_capacity();
};

Battery::Battery(int capacity = 70)
{
    this->capacity = capacity;
}

Battery::~Battery()
{
}

int Battery::get_capacity()
{
    return this->capacity;
}

2、Car类

#include <iostream>
#include <string>

using namespace std;

class Car
{
private:
    string maker;  //制造商
    string model;  //型号
    int year;      //生产年份
    int odometers; //行车里程数
public:
    Car(string maker, string model, int year);
    ~Car();
    void info();
    void update_odometers(int newOdometers);
};

Car::Car(string maker, string model, int year)
{
    this->maker = maker;
    this->model = model;
    this->year = year;
    this->odometers = 0;
}

Car::~Car()
{
}
void Car::info()
{
    cout << "maker:\t\t" << this->maker << endl;
    cout << "model:\t\t" << this->model << endl;
    cout << "year:\t\t" << this->year << endl;
    cout << "odometers:\t" << this->odometers << endl;
}
void Car::update_odometers(int newOdometers)
{
    if (newOdometers < this->odometers)
        cout << "更新里程数有误" << endl;
    else
        this->odometers = newOdometers;
}

3、ElectricCar类

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

class ElectricCar : public Car
{
private:
    Battery battery;

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

ElectricCar::ElectricCar(string maker, string model, int year) : Car(maker, model, year)
{
    Battery battery(70);
    this->battery = battery;
}

void ElectricCar::info()
{
    Car::info();
    cout << "capacity:\t" << this->battery.get_capacity() << "-kWh" << endl;
}
#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();
}

实验任务四

1、pets.hpp

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

class MachinePets
{
private:
    string nickname;

public:
    MachinePets(const string nickname);
    ~MachinePets();
    virtual string talk();
    const string get_nickname();
};

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

MachinePets::~MachinePets() {}

string MachinePets::talk() { return ""; }

const string MachinePets::get_nickname()
{
    return nickname;
}

class PetCats : public MachinePets
{
public:
    PetCats(const string s);
    ~PetCats();
    string talk();
};

PetCats::PetCats(const string s) : MachinePets(s) {}

PetCats::~PetCats() {}

string PetCats::talk()
{
    return "miao wu~";
}

class PetDogs : public MachinePets
{
public:
    PetDogs(const string s);
    ~PetDogs();
    string talk();
};

PetDogs::PetDogs(const string s) : MachinePets(s) {}

PetDogs::~PetDogs() {}

string PetDogs::talk()
{
    return "wang wang~";
}
#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);
}

标签:string,继承,Car,Battery,int,实验,MachinePets,include
来源: https://www.cnblogs.com/suu-8/p/15599905.html