实验四:继承与派生练习,运算符[]重载练习

一:实验结论

/*问题描述*/

1. 车辆基本信息管理 问题场景描述如下: 为了对车量基本信息进行管理,对现实世界车量基本信息抽象后,抽象出Car类、ElectricCar类、Battery类, 它们之间的关系描述如下:基于Car类派生出ElectricCar类,派生类ElectricCar中新增数据成员为Battery类 对象。

/*代码如下*/

 1 #pragma once
 2 #ifndef BATTERY_H
 3 #define BATTERY_H
 4 
 5 class battery {
 6 private:
 7     int batterySize;
 8     double using_time;
 9 public:
10     battery(int batterySize0=70):batterySize(batterySize0){}
11     void get_batterySize();
12     void get_using_time();
13 };
14 
15 #endif // !BATTERY_H
battery.h
 1 #include"battery.h"
 2 #include<iostream>
 3 using namespace std;
 4 
 5 void battery::get_batterySize() {
 6     cout << batterySize << "—KWH";
 7 }
 8 
 9 void battery::get_using_time() {
10     using_time = batterySize / 60.0;//自己规定一下每小时需要60的能量。
11     cout << using_time << "h  " << "请注意及时充能";
12 }
battery.cpp
 1 #pragma once
 2 #ifndef CAR_H
 3 #define CAR_H
 4 #include<string>
 5 using namespace std;
 6 class car {
 7 private:
 8     string maker;
 9     string model;
10     int year;
11     double odometer;
12     double sum_meters = 100000.0;
13 public:
14     car(string maker0, string model0, int year0, double odometer0 = 0.0) :maker(maker0), model(model0), year(year0), odometer(odometer0) {}
15     car(){}
16     friend ostream & operator<<(ostream &out, const car &c);//重载<<运算符。
17     void update_odometer(double meters);//更新行车总里程数。
18 };
19 
20 #endif // !CAR_H
car.h
 1 //坑,在为<<重载的时候,只在头文件中用了using std::string ,否则无法识别string,但是friend没有飘红,
 2 //在我进行函数体实现的时候出了问题,然后我意识到可能是名称空间的问题,改成using namespace std就好了。
 3 #include"car.h"
 4 #include<iostream>
 5 #include<string>
 6 using namespace std;
 7 
 8 void car::update_odometer(double meters) {
 9     double odometer1 = odometer;
10     odometer1 += meters;
11     if (odometer > odometer1) {
12         cout << "WANNING,Failed to Update Odometer" << endl;
13     }
14     if (sum_meters - odometer1 <= 10000) {
15         cout << "里程数已接近汽车极限,行驶请注意安全" << endl;
16     }
17     odometer = odometer1;
18 }
19 
20 ostream & operator<<(ostream &out,const car &c) {
21     cout << "汽车制造商:" << c.maker << endl
22         << "汽车型号:" << c.model << endl
23         << "生产年份:" << c.year << endl
24         << "总里程数:" << c.odometer << "km" << endl;
25     return out;
26 }
car.cpp
 1 #pragma once
 2 #ifndef ELECTRICCAR_H
 3 #define ELECTRICCAR_H
 4 #include"car.h"
 5 #include"battery.h"
 6 #include<iostream>
 7 #include<string>
 8 using namespace std;
 9 
10 class electricCar :private car, private battery {
11 private:
12     battery b1;//新增成员
13 public:
14     electricCar(string maker0, string model0, int year0, double odometer0 = 0.0, battery b0 = 70) :b1(b0), car(maker0, model0, year0, odometer0) {}
15     friend ostream & operator<<(ostream &out, electricCar &e);//<<重载声明
16     void update_odometer(double meters);//里程数更新
17 };
18 
19 #endif // !ELECTRICCAR_H
electricCar.h
 1 //坑,主要遇到的坑是不知道如何在派生类中引用基类的友元函数,然后仔细思考了一下,考虑<<重载返回值的类型,翻了翻工具书,解决了嘻嘻嘻。
 2 #include"electricCar.h"
 3 #include<iostream>
 4 #include<string>
 5 using namespace std;
 6 
 7 void electricCar::update_odometer(double meters) {
 8     car::update_odometer(meters);//调用派生类的成员函数
 9 }
10 
11 ostream & operator<<(ostream &out, electricCar &e) {//<<重载实现
12     out << (const car &)e;//调用基类中的友元函数.
13     cout << "剩余能量:";
14     e.b1.get_batterySize();
15     cout << endl << "可使用时长:";
16     e.b1.get_using_time();
17     return out;
18 }
electricCar.cpp
 1 #include <iostream>
 2 #include<string>
 3 using namespace std;
 4 
 5 #include "car.h"
 6 #include "electricCar.h" 
 7 
 8 int main() {
 9     // 测试Car类 
10     car oldcar("Audi", "a4", 2016);
11     cout << "--------oldcar's info--------" << endl;
12     oldcar.update_odometer(25000);
13     cout << oldcar << endl;
14 
15     // 测试ElectricCar类 
16     electricCar newcar("Tesla", "model s", 2016);
17     cout << "\n--------newcar's info--------\n";
18     newcar.update_odometer(91000);//老师提供的这个语句放错了位置,line17与line18应交换,否则出现提示的位置感觉不太对。
19     cout << newcar << endl;
20 
21     system("pause");
22 
23     return 0;
24 }
main.cpp

/*运行截图*/

 

/*问题描述*/

2、重载运算符[]为一维动态整形数组类ArrayInt的成员函数,使得通过动态整形数组对象名和下标可以 访问对象中具体元素。 

/*代码如下*/

 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         int& operator[](int i);
10         void print(); 
11     private:
12         int *p;
13         int size;
14 };
15 
16 #endif
arrayInt.h
 1 #include "arrayInt.h"
 2 #include <iostream>
 3 #include <cstdlib>
 4 using std::cout;
 5 using std::endl;
 6 
 7 ArrayInt::ArrayInt(int n, int value): size(n) {
 8     p = new int[size];
 9     
10     if (p == nullptr) {
11         cout << "fail to mallocate memory" << endl;
12         exit(0); 
13     } 
14     
15     for(int i=0; i<size; i++)
16         p[i] = value;
17 }
18 
19 ArrayInt::~ArrayInt() {
20     delete[] p;
21 }
22 
23 void ArrayInt::print() {
24     for(int i=0; i<size; i++)
25         cout << p[i] << " ";
26     cout << endl;
27 }
28 
29 // 补足:将运算符[]重载为成员函数的实现
30 int& ArrayInt::operator[](int i) {
31     return p[i];
32 }
arrayInt.cpp
 1 #include <iostream>
 2 using namespace std;
 3 
 4 #include "arrayInt.h"
 5 
 6 int main() {
 7     // 定义动态整型数组对象a,包含2个元素,初始值为0
 8     ArrayInt a(2);
 9     a.print();
10     
11     // 定义动态整型数组对象b,包含3个元素,初始值为6
12     ArrayInt b(3, 6);
13     b.print();
14 
15     // 通过对象名和下标方式访问并修改对象元素
16     b[0] = 2;
17     cout << b[0] << endl;
18     b.print();
19 
20     system("pause");
21 
22     return 0;
23 }
main.cpp

/*运行截图*/

/*拓展部分*/

 等我考完离散和英语补上去。(渣渣无力的微笑(透露着疲惫))

 

二:实验总结与体会

1、第一个问题在敲的过程中出现的问题会比较多,课本上的知识有限,无法避免地需要查找工具书如《C++ Primer 》等,在查找过程中会更好地学习到更多的东西,个人觉得发现问题&自我思考&解决问题,会得到更好地学习效果。

2、世上本没有路,不代表不能出现路。

三:互评地址

1、

2、

3、

 

posted @ 2019-05-14 13:35  爱因斯坦PLUS  阅读(251)  评论(1编辑  收藏  举报