ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2021-11-25 01:32:06  阅读:156  来源: 互联网

标签:include string HPP C++ 面向对象 MachinePets CPP 实验报告 public


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

运行截图


总结

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

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有