實驗四
1. 車輛基本信息管理
問題場景描述如下:
為了對車量基本信息進行管理,對現(xiàn)實世界車量基本信息抽象后,抽象出Car類、ElectricCar類、Battery類,
它們之間的關系描述如下:基于Car類派生出ElectricCar類,派生類ElectricCar中新增數(shù)據成員為Battery類
對象。
代碼:
#ifndef BATTERY_H #define BATTERY_H class battery{ public: battery(int bS=70); int getbattery(); private: int batterySize; }; #endif
#include "battery.h" #include <iostream> using std::cout; using std::endl; battery::battery(int bS):batterySize(bS){ }; int battery::getbattery(){ return batterySize; }
#ifndef CAR_H #define CAR_H #include <string> using std::string; class Car{ public: Car(string ma,string mo,int y,int od=0); friend std::ostream & operator << (std::ostream &out,Car &c); int updateOdometer(int od);//更新里程數(shù) string getMaker(); string getModel(); int getYear(); int getOdometer(); private: string maker; string model; int year; int odometer; }; #endif
#include "car.h" #include <iostream> #include <string> using std::cin; using std::cout; using std::endl; using std::string; Car::Car(string ma,string mo,int y,int od):maker(ma),model(mo),year(y),odometer(od){ }; string Car::getMaker(){ return maker; } string Car::getModel(){ return model; } int Car::getYear(){ return year; } int Car::getOdometer(){ return odometer; } int Car::updateOdometer(int od){ if(odometer<=od) odometer=od; else{ cout<<"error!"<<endl; cin>>odometer; } return odometer; } std :: ostream & operator << (std::ostream &out,Car &c){ out<<"maker: "<<c.maker<<endl <<"model: "<<c.model<<endl <<"year: "<<c.year<<endl <<"odometer: "<<c.odometer<<endl; return out; }
#ifndef ELECTRICCAR_H #define ELECTRICCAR_H #include "car.h" #include "battery.h" class ElectricCar:public Car,public battery { public: ElectricCar(string ma,string mo,int y,int od=0); friend std::ostream & operator<<(std::ostream &out,ElectricCar &c); }; #endif
#include "car.h" #include "battery.h" #include "electricCar.h" #include <iostream> using namespace std; ElectricCar::ElectricCar(string ma,string mo,int y,int od):Car(ma,mo,y,od){ } std::ostream & operator<<(std::ostream &out,ElectricCar &c){ out<<"maker: "<<c.getMaker()<<endl <<"model: "<<c.getModel()<<endl <<"year: "<<c.getYear()<<endl <<"odomter: "<<c.getOdometer()<<endl <<"batterySize: "<<c.getbattery()<<"-KWh"<<endl; return out; }
#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; }
結果:

2. 補足程序,重載運算符[]為一維動態(tài)整形數(shù)組類ArrayInt的成員函數(shù),使得通過動態(tài)整形數(shù)組對象名和下標可以
訪問對象中具體元素。
代碼:
#ifndef ARRAY_INT_H #define ARRAY_INT_H class ArrayInt{ public: ArrayInt(int n, int value=0); ~ArrayInt(); // 補足:將運算符[]重載為成員函數(shù)的聲明 int & operator [] (int k); void print(); private: int *p; int size; }; #endif
#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 == NULL) { 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; } // 補足:將運算符[]重載為成員函數(shù)的實現(xiàn) int &ArrayInt :: operator [] (int k){ return p[k]; }
#include <iostream> using namespace std; #include "arrayInt.h" int main() { // 定義動態(tài)整型數(shù)組對象a,包含2個元素,初始值為0 ArrayInt a(2); a.print(); // 定義動態(tài)整型數(shù)組對象b,包含3個元素,初始值為6 ArrayInt b(3, 6); b.print(); // 通過對象名和下標方式訪問并修改對象元素 b[0] = 2; cout << b[0] << endl; b.print(); system("pause"); return 0; }
結果:

小結:
第一題通過百度,查到了類似的題目,通過模仿,把他做了出來。說明我遇到這種綜合的實驗,還是有一點難度。基礎一定要打好。
第二題做起來就比較輕松,只需要定義一個operator來實現(xiàn),補充的程序也比較少。
浙公網安備 33010602011771號