实验四 继承
实验任务三
car.hpp
#ifndef car_hpp #define car_hpp #include <iostream> #include <iomanip> #include <string> using namespace std; class Car { private: string maker; string model; int year; int odometers; public: Car(string maker, string model, int year, int odometers = 0); void info(); void update_odometers(int km); }; Car::Car(string maker, string model, int year, int odometers) : maker(maker), model(model), year(year), odometers(odometers) { } void Car::info() { cout << left << setw(15) << "maker:" << maker << endl; cout << left << setw(15) << "model:" << model << endl; cout << left << setw(15) << "year:" << year << endl; cout << left << setw(15) << "odometers:" << odometers << endl; } void Car::update_odometers(int km) { if (km < odometers) cout << "wrong information!!!" << endl; else odometers = km; } #endif
battery.hpp
#ifndef battery_hpp #define battery_hpp class battery { private: int capacity; public: battery(int capacity = 70); int get_capacity() const; }; battery::battery(int capacity) : capacity(capacity) { } int battery::get_capacity() const { return capacity; } #endif
electricCar.hpp
#ifndef electricCar_hpp #define electricCar_hpp #include "car.hpp" #include "battery.hpp" class ElectricCar : public Car { private: battery battery; public: ElectricCar(string maker, string model, int year, int capacity = 70); void info(); }; ElectricCar::ElectricCar(string maker, string model, int year, int capacity) : Car(maker, model, year), battery(capacity) { } void ElectricCar::info() { Car::info(); cout << left << setw(15) << "capacity:" << battery.get_capacity() << "-KWh" << endl; } #endif
task3.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(); }
实验结果截图
实验任务四
pets.hpp
#ifndef pet_hpp #define pet_hpp #include <iostream> #include <string> #include <iomanip> using namespace std; // 机器宠物类MachinePets class MachinePets { private: string nickname; public: MachinePets(const string s) : nickname(s) {} string get_nickname() const { return nickname; } virtual string talk() { return "listen"; } }; // 电子宠物猫类PetCats class PetCats : public MachinePets { public: PetCats(const string s) : MachinePets(s) {} string talk() { return "miao wu ~"; } }; // 电子宠物狗类PetDogs class PetDogs : public MachinePets { public: PetDogs(const string s) : MachinePets(s) {} string talk() { return "wang wang wang !"; } }; #endif
task4.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); }
实验结果截图
本文作者:请去看诡秘之主
本文链接:https://www.cnblogs.com/xjy881/p/15599835.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步