c++ 实验四 类的继承丶派生和多态

---恢复内容开始---

重载运算符  <<

#pragma once
#ifndef BATTERY_H
#define BATTERY_H
class battery

{
public:
    battery(int batterySize1 = 70);
    int getbattery()const;

private:
    int batterySize;
};


#endif
battery.h
#pragma once
#ifndef CAR_H
#define CAR_H
#include<string>
#include
using std::string;
using std::ostream;
class car
{
    public
        :
            car(string maker1, string model1, int year1, int odometer1 = 0);
            void updateOdometer(int a);
            friend  ostream &operator<<(ostream &out, const car &c2);

private:
    string maker;
    string model;
    int year;
    int odometer;
};
#endif
car.h
#pragma once
#ifndef ELECTRICCAR_H
#define ELECTRICCAR_H
#include<string>
#include"car.h"
#include"battery.h"
using std::string;
class ElectricCar :public car,public battery
{
public:
    ElectricCar(string maker2, string model2, int year2, int odometer2 = 0, int battery1 = 70);
    friend ostream &operator<<(ostream &out,const ElectricCar &c1);
    
private:
    battery batterySize;
};



#endif
electricCar.h
#include"battery.h"


battery::battery(int batterySize1) :batterySize(batterySize1)
{
}
int battery::getbattery()const
{
    return batterySize;
}
battery.cpp
#include"car.h"
#include<string>
#include
using namespace std;
using std::endl;
using std::cout;

car::car(string maker1, string model1, int year1, int odometer1) :maker(maker1), model(model1), year(year1), odometer(odometer1)
{}
void car::updateOdometer(int a)
{
    if (a < odometer)
    {
        cout << "wrong odometer" << endl;
        exit(0);
    }
    odometer = a;
}
ostream &operator<<(ostream &out, const car &c2)
{
    out << "maker:" << c2.maker << endl
        << "model:" << c2.model << endl
        << "year:" << c2.year << endl
        << "odometer:" << c2.odometer << endl;
    return out;
}
car.cpp
#include"electricCar.h"
using namespace std;
ElectricCar::ElectricCar(string maker2, string model2, int year2, int odometer2, int battery1) :car(maker2, model2, year2, odometer2), batterySize(battery1) {

}

ostream &operator<<(ostream &out, const ElectricCar &c1)
{    
    car c2 = c1;
    battery a = c1.batterySize;
    out << c2 
        << "batterySize:" << a.getbattery() << "-kWh" << endl;

    return out;
}
electricCar.cpp
#include 
#include
using namespace std;

#include "car.h"
#include "electricCar.h" 

int main() 
{
    // 测试Car类 
    car oldcar("Audi", "a4", 2016);
    cout << "--------oldcar's info--------" << endl;
    oldcar.updateOdometer(25000);
    cout << oldcar << endl;

    // 测试ElectricCar类 
    ElectricCar newcar("Tesla", "model s", 2016);
    newcar.updateOdometer(2500);
    cout << "\n--------newcar's info--------\n";
    cout << newcar << endl;

    system("pause");

    return 0;
}
main.cpp

c++ 实验四 类的继承丶派生和多态_第1张图片

 

#ifndef ARRAY_INT_H
#define ARRAY_INT_H

class ArrayInt{
    public:
        ArrayInt(int n, int value=0);
        ~ArrayInt();
        int &operator[](int n);
            // 补足:将运算符[]重载为成员函数的声明
        // ×××
        void print(); 
    private:
        int *p;
        int size;
};

#endif
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) {
        cout << "fail to mallocate memory" << endl;
        exit(0); 
    } 
    
    for(int i=0; i)
        p[i] = value;
}

ArrayInt::~ArrayInt() {
    delete[] p;
}


void ArrayInt::print() {
    for(int i=0; i)
        cout << p[i] << " ";
    cout << endl;
}

int &ArrayInt::operator[](int n)
{

    return p[n];
}

// 补足:将运算符[]重载为成员函数的实现
// ×××
arrayint.cpp

因为main没有添加改动,就不添加了。

c++ 实验四 类的继承丶派生和多态_第2张图片

 

实验总结:

1、一开始在electricCar重载  << 的时候,参考了书上的做法,用了很多get去获取数据,但在最后出现了错误(errorLNK2019:无法解析的外部符号),然后强制把eleccar转成了car,但不用eleccar继承battery,仍会出现上述错误,公有继承后,错误消除。

2、在array的实验里,重载的时候忘记加&符号,导致main.cpp中,b[0]变成了无法更改的左值。

 

你可能感兴趣的:(c++ 实验四 类的继承丶派生和多态)