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
运行截图
总结
- 练习了基本的继承逻辑、原理、应用实践,对于基类与派生类构造函数的实现有了一定的理解