日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

实验四---继承与派生练习以及运算符[ ]重载练习

發(fā)布時(shí)間:2024/4/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实验四---继承与派生练习以及运算符[ ]重载练习 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 車輛基本信息管理 問題場(chǎng)景描述如下: 為了對(duì)車量基本信息進(jìn)行管理,對(duì)現(xiàn)實(shí)世界車量基本信息抽象后,抽象出Car類、ElectricCar類、Battery類, 它們之間的關(guān)系描述如下:基于Car類派生出ElectricCar類,派生類ElectricCar中新增數(shù)據(jù)成員為Battery類 對(duì)象。

/*代碼如下*/

#ifndef BATTERY_H #define BATTERY_Hclass battery {private:int batterysize;public:battery(int bs0=70):batterysize(bs0){}void get_batterysize();};#endif // !BATTERY_H battery.h #include"battery.h" #include<iostream> using namespace std; void battery::get_batterysize() {cout << batterysize << "-KWH"; } battery.cpp #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);//重載<<運(yùn)算符。void update_odometer(double meters);//更新行車總里程數(shù)。 };#endif // !CAR_H 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,更新數(shù)據(jù)失敗" << endl;}else odometer = odometer1;}ostream & operator<<(ostream &out,const car &c) //重載實(shí)現(xiàn) {cout << "汽車制造商:" << c.maker << endl<< "汽車型號(hào):" << c.model << endl<< "生產(chǎn)年份:" << c.year << endl<< "總里程數(shù):" << c.odometer << "km" << endl;return out;} car.cpp #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);//里程數(shù)更新 };#endif // !ELECTRICCAR_H electricCar.h #include"electricCar.h" #include<iostream> #include<string> using namespace std; void electricCar::update_odometer(double meters) {car::update_odometer(meters);//調(diào)用派生類的成員函數(shù) }ostream & operator<<(ostream &out, electricCar &e) {//<<重載實(shí)現(xiàn)out << (const car &)e;//調(diào)用基類中的友元函數(shù).cout << "剩余能量:";e.b.get_batterysize();return out;} electricCar.cpp #include <iostream> #include<string> using namespace std; #include "car.h" #include "electricCar.h" #include <stdlib.h> int main() {// 測(cè)試Car類car oldcar("Audi", "a4", 2016);cout << "--------oldcar's info--------" << endl;oldcar.update_odometer(25000);cout << oldcar << endl;// 測(cè)試ElectricCar類electricCar newcar("Tesla", "model s", 2016);cout << "\n--------newcar's info--------\n";newcar.update_odometer(2500);cout << newcar << endl;system("pause");return 0;} main.cpp

2、重載運(yùn)算符[]為一維動(dòng)態(tài)整形數(shù)組類ArrayInt的成員函數(shù),使得通過動(dòng)態(tài)整形數(shù)組對(duì)象名和下標(biāo)可以 訪問對(duì)象中具體元素。?

/*代碼如下*/

#ifndef ARRAYINT_H #define ARRAYINT_H class ArrayInt {public:ArrayInt(int n, int value=0);~ArrayInt();// 補(bǔ)足:將運(yùn)算符[]重載為成員函數(shù)的聲明int& operator[](int i);void print();private:int *p;int size;};#endif // ARRAYINT_H 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];//動(dòng)態(tài)內(nèi)存分配if (p == nullptr) {//關(guān)于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;}// 補(bǔ)足:將運(yùn)算符[]重載為成員函數(shù)的實(shí)現(xiàn)int& ArrayInt::operator[](int i) {return p[i];} arrayint.cpp #include <iostream> using namespace std; #include "arrayInt.h" #include<stdlib.h> int main() {// 定義動(dòng)態(tài)整型數(shù)組對(duì)象a,包含2個(gè)元素,初始值為0ArrayInt a(2);a.print();// 定義動(dòng)態(tài)整型數(shù)組對(duì)象b,包含3個(gè)元素,初始值為6ArrayInt b(3, 6);b.print();// 通過對(duì)象名和下標(biāo)方式訪問并修改對(duì)象元素b[0] = 2;cout << b[0] << endl;b.print();system("pause");return 0;} main.cpp

?????是我的codeblocks版本太老了嗎,好像不支持這個(gè)標(biāo)準(zhǔn)

其他的IDE就可以運(yùn)行

?二:實(shí)驗(yàn)總結(jié)與體會(huì)

1.怎樣在派生類中引用基類的友元函數(shù)卡了,查資料得知。

2.派生類和重載不熟練,常常需要翻書。。

3.關(guān)于那個(gè)nullptr的問題

https://www.cnblogs.com/yutongqing/p/6508327.html

似乎在這里能找到一些頭緒。

???????????????????????????? ----X.Raven

轉(zhuǎn)載于:https://www.cnblogs.com/laboratory-X/p/10896615.html

與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的实验四---继承与派生练习以及运算符[ ]重载练习的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。