编程语言
首页 > 编程语言> > 面向对象->实验报告四(C++)

面向对象->实验报告四(C++)

作者:互联网

task3


题目要求



源码

main.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();

    ElectricCar anothercar("Tesla", "model Y", 2018, 180);
    anothercar.update_odometers(5200);
    cout << "\n--------anothercar's info--------\n";
    anothercar.update_odometers(230);
    anothercar.info();
}

battery.hpp

#ifndef CPP_BATTERY_HPP
#define CPP_BATTERY_HPP

class Battery{
public:
    Battery(double _cap = 70):capacity(_cap){};
    double get_capacity(){
        return capacity;
    };

private:
    double capacity;
};
#endif //CPP_BATTERY_HPP

car.hpp

#ifndef CPP_CAR_HPP
#define CPP_CAR_HPP

#include "iostream"
#include "string"
#include "iomanip"
using namespace std;
class Car{
public:
    Car(string _maker, string _model, int _year, int _odometers = 0):
        maker(_maker), model(_model), year(_year), odometers(_odometers){}
    virtual void info();
    void update_odometers(int);

protected:
    string maker, model;
    int year, odometers;
};

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

void Car::update_odometers(int _tempOdometers) {
    if(_tempOdometers < odometers)
        cout << "Change failed." << endl;
    else
        odometers = _tempOdometers;
}

#endif //CPP_CAR_HPP

electricCar.hpp

#ifndef CPP_ELECTRICCAR_HPP
#define CPP_ELECTRICCAR_HPP

#include "car.hpp"
#include "battery.hpp"
class ElectricCar:public Car{
private:
    Battery battery;
public:
    ElectricCar(string, string, int, int);
    virtual void info();
};

ElectricCar::ElectricCar(string _maker, string _model, int _year, int capacity = 70):
        Car(_maker, _model, _year), battery(capacity){}

void ElectricCar::info() {
    cout << left << setw(16) << "maker:\t" << maker << endl;
    cout << left << setw(16) << "model:\t" << model << endl;
    cout << left << setw(15) << "year:\t" << year << endl;
    cout << left << setw(12) << "odometers:\t" << odometers << endl;
    cout << left << setw(10) << "capacity\t" << battery.get_capacity() << endl;
}

#endif //CPP_ELECTRICCAR_HPP

运行截图


总结


task4

题目要求


源码

main.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

#ifndef MAIN_CPP_PETS_HPP
#define MAIN_CPP_PETS_HPP

#include "iostream"
#include "string"
using namespace std;
class MachinePets{
private:
    string nickname;
public:
    MachinePets(const string s);
    string get_nickname() const{
        return nickname;
    };
    virtual string talk();
};

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

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

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

string MachinePets::talk() {
    string say = "hello";
    return say;
}
PetCats::PetCats(const string s):
        MachinePets(s){}

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

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

string PetDogs::talk() {
    return "wang wang~";
}

#endif //MAIN_CPP_PETS_HPP

运行截图


总结

标签:include,string,HPP,C++,面向对象,MachinePets,CPP,实验报告,public
来源: https://www.cnblogs.com/xzajyjs/p/15601039.html