c++第四次實驗
阿新 • • 發佈:2019-05-12
define 圖片 inf tput 初始 void use make 繼承
1.車輛基本信息管理
battery.h
1 #ifndef BATTERY_H 2 #define BATTERY_H 3 class Battery 4 { 5 public: 6 Battery(int p=70):batterySize(p){} 7 int showbatterySize(); 8 private: 9 int batterySize; 10 }; 11 #endif // BATTERY_H
battery.cpp
1 #include "battery.h" 2 3 int Battery::showbatterySize()4 { 5 return batterySize; 6 }
car.h
1 #ifndef CAR_H 2 #define CAR_H 3 #include <cstring> 4 #include <iostream> 5 using namespace std; 6 class Car 7 { 8 public: 9 Car (string maker1,string model1,int year1,int odometer1=0); 10 void updateOdometer(int update);11 string showMaker(); 12 string showModel(); 13 int showYear(); 14 int showOdometer(); 15 friend ostream & operator<<(ostream &output,const Car &car1); 16 private: 17 string maker; 18 string model; 19 int year; 20 int odometer; 21 }; 22#endif // CAR_H
car.cpp
1 #include "car.h" 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 6 Car::Car(string maker1,string model1,int year1,int odometer1): 7 maker(maker1),model(model1),year(year1),odometer(odometer1) 8 { 9 10 } 11 void Car::updateOdometer(int update) 12 { 13 if(update<odometer) 14 cout<<"wrong updateodometer"<<endl; 15 else 16 { 17 odometer=update; 18 } 19 } 20 string Car::showMaker() 21 { 22 return maker; 23 } 24 string Car::showModel() 25 { 26 return model; 27 } 28 int Car::showYear() 29 { 30 return year; 31 } 32 int Car::showOdometer() 33 { 34 return odometer; 35 } 36 ostream &operator<<(ostream &output,const Car &car1) 37 { 38 output<<"maker"<<":"<<car1.maker<<endl 39 <<"model"<<":"<<car1.model<<endl 40 <<"year"<<":"<<car1.year<<endl 41 <<"odometer"<<":"<<car1.odometer<<endl; 42 return output; 43 }
electricCar.h
1 #ifndef ELECTRICCAR_H 2 #define ELECTRICCAR_H 3 #include <iostream> 4 #include "car.h" 5 #include "battery.h" 6 using namespace std; 7 class electricCar:public Car,public Battery 8 { 9 public: 10 electricCar(string maker1,string model1,int year1,int odometer1=0); 11 friend ostream & operator<<(ostream &output,electricCar &electriccar1); 12 private: 13 Battery battery; 14 15 }; 16 #endif
electricCar.cpp
1 #include "electricCar.h" 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 electricCar::electricCar(string maker1,string model1,int year1,int odometer1): 6 Car(maker1,model1,year1,odometer1),battery(70) 7 { 8 9 } 10 ostream &operator<<(ostream &output,electricCar &electriccar1) 11 { 12 output<<"maker"<<":"<<electriccar1.showMaker()<<endl 13 <<"model"<<":"<<electriccar1.showModel()<<endl 14 <<"year"<<":"<<electriccar1.showYear()<<endl 15 <<"odometer"<<":"<<electriccar1.showOdometer()<<endl 16 <<"batterySize"<<":"<<electriccar1.showbatterySize()<<"-kWh"<<endl; 17 return output; 18 }
main.cpp
1 #include <iostream> 2 #include <stdlib.h> 3 using namespace std; 4 5 #include "car.h" 6 #include "electricCar.h" 7 8 int main() { 9 // 測試Car類 10 Car oldcar("Cheng nuo","knight4",2019); 11 cout << "--------oldcar‘s info--------" << endl; 12 oldcar.updateOdometer(25000); 13 cout << oldcar << endl; 14 15 // 測試ElectricCar類 16 electricCar newcar("Tesla","model s",2016); 17 newcar.updateOdometer(2500); 18 cout << "\n--------newcar‘s info--------\n"; 19 cout << newcar << endl; 20 newcar.updateOdometer(1500); 21 cout << "\n--------newcar‘s info--------\n"; 22 cout << newcar << endl; 23 24 system("pause"); 25 26 return 0; 27 }
更改了一些信息,並且在裏程數小於之前時會報錯
2.補足程序
arrayInt.h
1 #ifndef ARRAY_INT_H 2 #define ARRAY_INT_H 3 4 class ArrayInt{ 5 public: 6 ArrayInt(int n, int value=0); 7 ~ArrayInt(); 8 // 補足:將運算符[]重載為成員函數的聲明 9 // ××× 10 int &operator[](int point); 11 void print(); 12 private: 13 int *p; 14 int size; 15 }; 16 17 #endif
arrayInt.cpp
1 #ifndef ARRAY_INT_H 2 #define ARRAY_INT_H 3 4 class ArrayInt{ 5 public: 6 ArrayInt(int n, int value=0); 7 ~ArrayInt(); 8 // 補足:將運算符[]重載為成員函數的聲明 9 // ××× 10 int &operator[](int point); 11 void print(); 12 private: 13 int *p; 14 int size; 15 }; 16 17 #endif
main.cpp
1 #include <iostream> 2 #include <stdlib.h> 3 using namespace std; 4 5 #include "arrayInt.h" 6 7 int main() { 8 // 定義動態整型數組對象a,包含2個元素,初始值為0 9 ArrayInt a(3); 10 a.print(); 11 12 // 定義動態整型數組對象b,包含3個元素,初始值為6 13 ArrayInt b(5, 4); 14 b.print(); 15 16 // 通過對象名和下標方式訪問並修改對象元素 17 b[3] = 5; 18 cout << b[3] << endl; 19 b.print(); 20 21 system("pause"); 22 23 return 0; 24 }
對main.cpp做了一些修改
實驗總結:
1.派生類的構造函數對派生類進行初始化時,要先將基類初始化,然後對成員對象初始化
2.重載<<運算符時,要註意引用符號的使用,並且要返回輸出流對象
3.由於繼承後的派生類在打印時無法調用基類的私有成員,所以要在基類中設置接口,使得能夠在派生類中輸出基類中私有成員的值
4.一個問題,友元函數operator<<中使用electriccar1.battery.showbatterySize()時,編譯器顯示battery為私有成員無法調用,但是友元函數不是可以訪問私有成員?
5.有關<<運算符重載時要註意頭文件iostream和using namespace std;要寫全
6.system("pause")要在前面加上#include<stdlib.h>
7.運算符[]重載時,返回值必須為引用類型,因為它作為左值不能為常量
c++第四次實驗