实验四
实验四:
1. 车辆基本信息管理
问题场景描述如下:
为了对车量基本信息进行管理,对现实世界车量基本信息抽象后,抽象出Car类、ElectricCar类、Battery类,
它们之间的关系描述如下:基于Car类派生出ElectricCar类,派生类ElectricCar中新增数据成员为Battery类
对象。
代码:
Car.h:
Car.h:
#ifndef CAR_H #define CAR_H #include<iostream> #include<string> using namespace std; class Car{ public: Car(string ma,string mo,int ye,int od=0); int updateOdometer(int i); friend ostream & operator <<(ostream &out,const Car &c); protected: string maker; string model; int year; int odometer; }; #endif
Car.cpp:
#include"Car.h" #include<string> #include<iostream> using namespace std; Car::Car(string ma,string mo,int ye,int od):maker(ma),model(mo),year(ye),odometer(od){} int Car::updateOdometer(int i) { if (i < odometer) { odometer = i; return odometer; } else { cout << "Wrong input!" << endl; return 0; } } ostream & operator <<(ostream &out,const Car &c){ out<<"maker: "<<c.maker<<endl<<"model: "<<c.model<<endl<<"year: "<<c.year<<endl<<"odometer: "<<c.odometer; return out; }
Battery.h:
#ifndef BATTERY_H #define BATTERY_H class Battery{ public: Battery(int power1=70); int getpower()const; private: int batterysize; }; #endif
Battery.cpp:
#include"Battery.h" #include<iostream> using namespace std; Battery::Battery(int power1) { batterysize=power1; } int Battery::getpower()const { return batterysize; }
ElectricCar.h:
#ifndef ELECTRICCAR_H #define ELECTRICCAR_H #include"Battery.h" #include<string> #include "Car.h" class ElectricCar:public Car{ public: friend ostream & operator <<(ostream& out, const ElectricCar& c); ElectricCar(string ma, string mo, int ye, int od = 0, int p=70); private: Battery battery; }; #endif
ElectricCar.cpp:
#include "ElectricCar.h" #include "Battery.h" #include "Car.h" #include<string> #include<iostream> using namespace std; ElectricCar::ElectricCar(string ma, string mo, int ye, int od, int po ):Car(ma, mo, ye, od){ Battery battery(po); } ostream&operator<<(ostream&out,const ElectricCar &c){ out << "maker: " << c.maker << endl << "model: " << c.model << endl << "year: " << c.year << endl << "odometer: " << c.odometer <<endl<<"battery: "<< c.battery.getpower() <<"KWH"<< endl;; return out; }
main.cpp:
#include"Battery.h" #include"Car.h" #include"ElectricCar.h" #include<iostream> using namespace std; 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(25000); cout << "\n--------newcar's info--------\n"; cout << newcar << endl; system("pause"); return 0; }
运行截图:
2.
补足程序,重载运算符[]为一维动态整形数组类ArrayInt的成员函数,使得通过动态整形数组对象名和下标可以访问对象中具体元素。
代码:
arrayInt.h:
arrayInt.h:
#ifndef ARRAY_INT_H #define ARRAY_INT_H class ArrayInt{ public: ArrayInt(int n, int value=0); ~ArrayInt(); int& operator[](int i); // 补足:将运算符[]重载为成员函数的声明 // ××× void print(); private: int *p; int size; }; #endif
arrayInt.cpp:
#include "arrayInt.h" #include <iostream> #include <cstdlib> 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<size; i++) p[i] = value; } ArrayInt::~ArrayInt() { delete[] p; } void ArrayInt::print() { for(int i=0; i<size; i++) cout << p[i] << " "; cout << endl; } int& ArrayInt::operator[](int i) { return p[i]; } // 补足:将运算符[]重载为成员函数的实现 // ×××
main.cpp:
include <iostream> using namespace std; #include "arrayInt.h" int main() { // 定义动态整型数组对象a,包含2个元素,初始值为0 ArrayInt a(2); a.print(); // 定义动态整型数组对象b,包含3个元素,初始值为6 ArrayInt b(3, 6); b.print(); // 通过对象名和下标方式访问并修改对象元素 b[0] = 2; cout << b[0] << endl; b.print(); system("pause"); return 0; }
运行截图:
总结:.关于第一个实验,一开始我认为Car和ElectricCar基类和派生类的<<重载是同名函数,不可以同时出现,因而在这浪费了很多时间,因而一开始是将派生类类的operator<<强制转化为基类Car的形式
代码如下:
ostream&operator<<(ostream&out,const ElectricCar &c){ out << *((Car*)&c<<c.battery.getbattery();
//强制转化后难以实现batterysize的实现,只能输出继承来父类的数据,但按理说应该可以输出,但我一直没能成功。
后来发现正常两个一起出现并没有什么问题。。。。。知道后来浏览同学的博客发现了是我的getbattery()为非const,而重载的<<也为const,牵连了权限大小的问题因而会报错。
We Turn Not Older With Years ,But Newer Everyday!

浙公网安备 33010602011771号