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

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

/*代码如下*/

#ifndef BATTERY_H
#define BATTERY_H

class 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);//重载<<运算符。
     void update_odometer(double meters);//更新行车总里程数。
 };

     #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,更新数据失败" << 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<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 << "\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_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

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

其他的IDE就可以运行

 二:实验总结与体会

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

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

3.关于那个nullptr的问题

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

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

                             ----X.Raven

猜你喜欢

转载自www.cnblogs.com/laboratory-X/p/10896615.html
今日推荐