#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
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;
}
electricCar.cpp
#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 << "
--------newcar's info--------
";
newcar.update_odometer(2500);
cout << newcar << endl;
system("pause");
return 0;
}
main.cpp
![实验四---继承与派生练习以及运算符[ ]重载练习 实验四---继承与派生练习以及运算符[ ]重载练习](/default/index/img?u=aHR0cHM6Ly9pbWcyMDE4LmNuYmxvZ3MuY29tL2Jsb2cvMTYxMjkxNC8yMDE5MDUvMTYxMjkxNC0yMDE5MDUyMDIxMjE0ODY1MS0xMjU0MjM1NDAzLnBuZw==)
2、重载运算符[]为一维动态整形数组类ArrayInt的成员函数,使得通过动态整形数组对象名和下标可以 访问对象中具体元素。
/*代码如下*/
#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
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];
}
arrayint.cpp
#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;
}
main.cpp
![实验四---继承与派生练习以及运算符[ ]重载练习 实验四---继承与派生练习以及运算符[ ]重载练习](/default/index/img?u=aHR0cHM6Ly9pbWcyMDE4LmNuYmxvZ3MuY29tL2Jsb2cvMTYxMjkxNC8yMDE5MDUvMTYxMjkxNC0yMDE5MDUyMDIxMjQzMDk0OC0xNTYwOTYyNDU0LnBuZw==)
?????是我的codeblocks版本太老了吗,好像不支持这个标准
![实验四---继承与派生练习以及运算符[ ]重载练习 实验四---继承与派生练习以及运算符[ ]重载练习](/default/index/img?u=aHR0cHM6Ly9pbWcyMDE4LmNuYmxvZ3MuY29tL2Jsb2cvMTYxMjkxNC8yMDE5MDUvMTYxMjkxNC0yMDE5MDUyMDIxMjUxNTE5NS0xMDYyMzExNTc5LnBuZw==)
其他的IDE就可以运行
二:实验总结与体会
1.怎样在派生类中引用基类的友元函数卡了,查资料得知。
2.派生类和重载不熟练,常常需要翻书。。
3.关于那个nullptr的问题
https://www.cnblogs.com/yutongqing/p/6508327.html
似乎在这里能找到一些头绪。
----X.Raven