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

发布时间:2022-06-29 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了面向对象->实验报告四(C++)脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

task3


题目要求

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

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


源码

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) << "capacityt" << battery.get_capacity() << endl;
}

#endif //CPP_ELECTRICCAR_HPP

运行截图

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


总结

  • 在所给主函数的基础上增加了对于electricCar构造函数初始化capacity和修改capacity的测试
  • IDE的不同和不同操作系统间的等宽字体、编码问题,可能输出的对齐方式会有问题。当前测试环境:MacOS 11.6 VSCode
  • 在使用派生类对基类进行初始化的时候需要注意其逻辑方式

task4

题目要求

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


源码

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

运行截图

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


总结

  • 练习了基本的继承逻辑、原理、应用实践,对于基类与派生类构造函数的实现有了一定的理解

脚本宝典总结

以上是脚本宝典为你收集整理的面向对象->实验报告四(C++)全部内容,希望文章能够帮你解决面向对象->实验报告四(C++)所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: