实验四
第一题
main.cpp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> 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; }
battery.h
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#ifndef _BTRY #define _BTRY class Battery { private: double batterySize, nowSize; public: Battery(double bSize=70); bool rechargeTo(double); bool useTo(double); double getNowSize(void); double getBatterySize(void); }; #endif
battery,cpp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include "battery.h" Battery::Battery(double bSize){ nowSize = batterySize = bSize; } bool Battery::rechargeTo(double target){ if(target>batterySize || target<nowSize) return false; nowSize = target; return true; } bool Battery::useTo(double target){ if(target>nowSize || target<0) return false; nowSize = target; return true; } double Battery::getNowSize(void){return nowSize;} double Battery::getBatterySize(void){return batterySize;}
car.h
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <string> using namespace std; class Car { protected: string maker, model; int year, odometer; friend class ElectricCar; public: Car(string, string, int); friend ostream & operator <<(ostream &out, Car &p); bool updateOdometer(int); };
car.cpp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include "car.h" #include <iostream> #include <string> using namespace std; Car::Car(string imaker, string imodel, int iyear):maker(imaker),model(imodel),year(iyear){ odometer = 0; } bool Car::updateOdometer(int newodo){ if(newodo<odometer) return false; odometer=newodo; return true; } ostream & operator <<(ostream &out, Car &p){ out<<"maker: "<<p.maker<<endl; out<<"model: "<<p.model<<endl; out<<"year: "<<p.year<<endl; out<<"odometer: "<<p.odometer<<endl; }
electriccar.h
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include "battery.h" class ElectricCar: public Car{ private: Battery battery; public: ElectricCar(string, string, int); friend ostream & operator <<(ostream &out, ElectricCar &p); };
electericcar.cpp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include "electriccar.h" #include <string> using namespace std; ElectricCar::ElectricCar(string imaker, string imodel, int iyear):Car(imaker,imodel,iyear),battery(){} ostream & operator <<(ostream &out, ElectricCar &p){ out<<"maker: "<<p.maker<<endl; out<<"model: "<<p.model<<endl; out<<"year: "<<p.year<<endl; out<<"odometer: "<<p.odometer<<endl; out<<"batterySize: "<<p.battery.getNowSize()<<"-kWh"<<endl; }
第二题:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#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 index) { if(index>=size) throw 1; return p[index]; }