博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实验四---继承与派生练习以及运算符[ ]重载练习
阅读量:6861 次
发布时间:2019-06-26

本文共 4175 字,大约阅读时间需要 13 分钟。

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

/*代码如下*/

#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
using namespace std;void battery::get_batterysize(){ cout << batterysize << "-KWH";}
battery.cpp
#ifndef CAR_H#define CAR_H#include
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);//重载<
car.h
#include"car.h"#include
#include
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; }
car.cpp
#ifndef ELECTRICCAR_H#define ELECTRICCAR_H#include"car.h"#include"battery.h"#include
#include
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);//<
electricCar.h
#include"electricCar.h"#include
#include
using namespace std;void electricCar::update_odometer(double meters) { car::update_odometer(meters);//调用派生类的成员函数 } ostream & operator<<(ostream &out, electricCar &e) {
//<
electricCar.cpp
#include 
#include
using namespace std;#include "car.h"#include "electricCar.h"#include
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; }
main.cpp

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

/*代码如下*/

#ifndef ARRAYINT_H#define ARRAYINT_Hclass 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 
#include
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
arrayint.cpp
#include 
using namespace std;#include "arrayInt.h"#include
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

?????是我的codeblocks版本太老了吗,好像不支持这个标准

其他的IDE就可以运行

 二:实验总结与体会

1.怎样在派生类中引用基类的友元函数卡了,查资料得知。

2.派生类和重载不熟练,常常需要翻书。。

3.关于那个nullptr的问题

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

似乎在这里能找到一些头绪。

                             ----X.Raven

转载于:https://www.cnblogs.com/laboratory-X/p/10896615.html

你可能感兴趣的文章