1. 車輛基本信息管理 問題場景描述以下: 爲了對車量基本信息進行管理,對現實世界車量基本信息抽象後,抽象出Car類、ElectricCar類、Battery類, 它們之間的關係描述以下:基於Car類派生出ElectricCar類,派生類ElectricCar中新增數據成員爲Battery類 對象。html
/*代碼以下*/ios
#ifndef BATTERY_H #define BATTERY_H class battery { private: int batterysize; public: battery(int bs0=70):batterysize(bs0){} void get_batterysize(); }; #endif // !BATTERY_H
#include"battery.h" #include<iostream> using namespace std; void battery::get_batterysize() { cout << batterysize << "-KWH"; }
#ifndef CAR_H #define CAR_H #include<string> using namespace std; class car { private: string maker; string model; int year; double odometer; double sum_meters = 100000.0; public: car(string maker0, string model0, int year0, double odometer0 = 0.0) :maker(maker0), model(model0), year(year0), odometer(odometer0) {} car(){} friend ostream & operator<<(ostream &out, const car &c);//重載<<運算符。 void update_odometer(double meters);//更新行車總里程數。 }; #endif // !CAR_H
#include"car.h" #include<iostream> #include<string> using namespace std; void car::update_odometer(double meters) { double odometer1 = odometer; odometer1 += meters; if (odometer > odometer1) { cout << "WARNING,更新數據失敗" << endl; } else odometer = odometer1; } ostream & operator<<(ostream &out,const car &c) //重載實現 { cout << "汽車製造商:" << c.maker << endl << "汽車型號:" << c.model << endl << "生產年份:" << c.year << endl << "總里程數:" << c.odometer << "km" << endl; return out; }
#ifndef ELECTRICCAR_H #define ELECTRICCAR_H #include"car.h" #include"battery.h" #include<iostream> #include<string> using namespace std; class electricCar :private car, private battery { private: battery b;//新增成員 public: electricCar(string maker0, string model0, int year0, double odometer0 = 0.0, battery b0 = 70) :b(b0), car(maker0, model0, year0, odometer0) {} friend ostream & operator<<(ostream &out, electricCar &e);//<<重載聲明 void update_odometer(double meters);//里程數更新 }; #endif // !ELECTRICCAR_H
#include"electricCar.h" #include<iostream> #include<string> using namespace std; void electricCar::update_odometer(double meters) { car::update_odometer(meters);//調用派生類的成員函數 } ostream & operator<<(ostream &out, electricCar &e) {//<<重載實現 out << (const car &)e;//調用基類中的友元函數. cout << "剩餘能量:"; e.b.get_batterysize(); return out; }
#include <iostream> #include<string> using namespace std; #include "car.h" #include "electricCar.h" #include <stdlib.h> int main() { // 測試Car類 car oldcar("Audi", "a4", 2016); cout << "--------oldcar's info--------" << endl; oldcar.update_odometer(25000); cout << oldcar << endl; // 測試ElectricCar類 electricCar newcar("Tesla", "model s", 2016); cout << "\n--------newcar's info--------\n"; newcar.update_odometer(2500); cout << newcar << endl; system("pause"); return 0; }
二、重載運算符[]爲一維動態整形數組類ArrayInt的成員函數,使得經過動態整形數組對象名和下標能夠 訪問對象中具體元素。 數組
/*代碼以下*/dom
#ifndef ARRAYINT_H #define ARRAYINT_H class ArrayInt { public: ArrayInt(int n, int value=0); ~ArrayInt(); // 補足:將運算符[]重載爲成員函數的聲明 int& operator[](int i); void print(); private: int *p; int size; }; #endif // ARRAYINT_H
#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) {//關於null、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]; }
#include <iostream> using namespace std; #include "arrayInt.h" #include<stdlib.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; }
?????是個人codeblocks版本太老了嗎,好像不支持這個標準ide
其餘的IDE就能夠運行函數
二:實驗總結與體會測試
1.怎樣在派生類中引用基類的友元函數卡了,查資料得知。spa
2.派生類和重載不熟練,經常須要翻書。。code
3.關於那個nullptr的問題htm
https://www.cnblogs.com/yutongqing/p/6508327.html
彷佛在這裏能找到一些頭緒。
----X.Raven