c++ 实验四 类的继承丶派生和多态
battery.h car.h electricCar.h battery.cpp car.cpp electricCar.cpp main.cpp arrayint.h arrayint.cpp
发布日期:2022-03-30 20:19:30
浏览次数:26
分类:博客文章
本文共 3549 字,大约阅读时间需要 11 分钟。
---恢复内容开始---
重载运算符 <<
#pragma once#ifndef BATTERY_H#define BATTERY_Hclass battery{public: battery(int batterySize1 = 70); int getbattery()const;private: int batterySize;};#endif
#pragma once#ifndef CAR_H#define CAR_H#include#include using std::string;using std::ostream;class car{ public : car(string maker1, string model1, int year1, int odometer1 = 0); void updateOdometer(int a); friend ostream &operator<<(ostream &out, const car &c2);private: string maker; string model; int year; int odometer;};#endif
#pragma once#ifndef ELECTRICCAR_H#define ELECTRICCAR_H#include#include"car.h"#include"battery.h"using std::string;class ElectricCar :public car,public battery{public: ElectricCar(string maker2, string model2, int year2, int odometer2 = 0, int battery1 = 70); friend ostream &operator<<(ostream &out,const ElectricCar &c1); private: battery batterySize;};#endif
#include"battery.h"battery::battery(int batterySize1) :batterySize(batterySize1){}int battery::getbattery()const{ return batterySize;}
#include"car.h"#include#include using namespace std;using std::endl;using std::cout;car::car(string maker1, string model1, int year1, int odometer1) :maker(maker1), model(model1), year(year1), odometer(odometer1){}void car::updateOdometer(int a){ if (a < odometer) { cout << "wrong odometer" << endl; exit(0); } odometer = a;}ostream &operator<<(ostream &out, const car &c2){ out << "maker:" << c2.maker << endl << "model:" << c2.model << endl << "year:" << c2.year << endl << "odometer:" << c2.odometer << endl; return out;}
#include"electricCar.h"using namespace std;ElectricCar::ElectricCar(string maker2, string model2, int year2, int odometer2, int battery1) :car(maker2, model2, year2, odometer2), batterySize(battery1) {}ostream &operator<<(ostream &out, const ElectricCar &c1){ car c2 = c1; battery a = c1.batterySize; out << c2 << "batterySize:" << a.getbattery() << "-kWh" << endl; return out;}
#include#include using namespace std;#include "car.h"#include "electricCar.h" int main() { // 测试Car类 car oldcar("Audi", "a4", 2016); cout << "--------oldcar's info--------" << endl; oldcar.updateOdometer(25000); cout << oldcar << endl; // 测试ElectricCar类 ElectricCar newcar("Tesla", "model s", 2016); newcar.updateOdometer(2500); cout << "\n--------newcar's info--------\n"; cout << newcar << endl; system("pause"); return 0;}
#ifndef ARRAY_INT_H#define ARRAY_INT_Hclass ArrayInt{ public: ArrayInt(int n, int value=0); ~ArrayInt(); int &operator[](int n); // 补足:将运算符[]重载为成员函数的声明 // ××× void print(); private: int *p; int size;};#endif
#include "arrayInt.h"#include#include using std::cout;using std::endl;ArrayInt::ArrayInt(int n, int value): size(n) { p = new int[size]; if (p == nullptr) { cout << "fail to mallocate memory" << endl; exit(0); } for(int i=0; i
因为main没有添加改动,就不添加了。
实验总结:
1、一开始在electricCar重载 << 的时候,参考了书上的做法,用了很多get去获取数据,但在最后出现了错误(errorLNK2019:无法解析的外部符号),然后强制把eleccar转成了car,但不用eleccar继承battery,仍会出现上述错误,公有继承后,错误消除。
2、在array的实验里,重载的时候忘记加&符号,导致main.cpp中,b[0]变成了无法更改的左值。
转载地址:https://www.cnblogs.com/aiwenzhuo/p/10894261.html 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年04月23日 03时53分15秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Unity中为什么不能对transform.position.x直接赋值?
2019-04-27
Unity中物体移动方法详解
2019-04-27
使用对象池优化性能
2019-04-27
Unity中的UI方案(基础版)
2019-04-27
Lua(一)——Lua介绍
2019-04-27
Lua(二)——环境安装
2019-04-27
Unity中父子物体的坑
2019-04-27
基础知识——进位制
2019-04-27
Lua(十二)——表
2019-04-27
Lua(十三)——模块与包
2019-04-27
Lua(四)——变量
2019-04-27
Lua(十四)——元表
2019-04-27
Lua(十五)——协同程序
2019-04-27
Lua(十六)——文件
2019-04-27
Lua(十七)——面向对象
2019-04-27
Lua(十八)——错误处理,垃圾回收
2019-04-27
xLua(一)——介绍
2019-04-27
xLua(二)——下载
2019-04-27
xLua(三)——在C#中访问Lua代码
2019-04-27
xLua(四)——C#访问Lua的基本类型
2019-04-27