实验4 类的继承、派生和多态

实验结论

类的继承和派生练习

源码

battery.h
//
// Created by KOKODA on 2019/5/18.
//

#ifndef SHIYAN4_BATTERY_H
#define SHIYAN4_BATTERY_H

class Battery {
public:
    Battery(int batterySize = 70);
    int getBatterySize() const;

private:
    int batterySize;
};

#endif //SHIYAN4_BATTERY_H
battery.cpp
//
// Created by KOKODA on 2019/5/18.
//

#include "battery.h"

Battery::Battery(int batterySize0) : batterySize(batterySize0) {}

int Battery::getBatterySize() const {
    return batterySize;
}
car.h
//
// Created by KOKODA on 2019/5/18.
//

#ifndef SHIYAN4_CAR_H
#define SHIYAN4_CAR_H

#include <string>

using namespace std;

class Car {
public:
    Car(const string &maker = "", const string &model = "", int year = 0, int odometer = 0);
    friend ostream &operator<<(ostream &out, const Car &A);
    int updateOdometer(int odometer0);

private:
    string maker, model;
    int year, odometer;
};

#endif //SHIYAN4_CAR_H
car.cpp
//
// Created by KOKODA on 2019/5/18.
//

#include "car.h"
#include <string>
#include <iostream>

Car::Car(const string &maker, const string &model, int year, int odometer) : maker(maker), model(model), year(year),odometer(odometer) {}

ostream &operator<<(ostream &out, const Car &A) {
    out << "Maker:" << A.maker << endl;
    out << "Model:" << A.model << endl;
    out << "Year:" << A.year << endl;
    out << "Odometer:" << A.odometer << endl;
    return out;
}

int Car::updateOdometer(int odometer0) {
    if (odometer0 > odometer) {
        odometer = odometer0;
        return 1;
    } else
        cout<<"The new odometer must be bigger than the old one"<<endl;
        return 0;
}
electricCar.h
//
// Created by KOKODA on 2019/5/18.
//

#ifndef SHIYAN4_ELECTRICCAR_H
#define SHIYAN4_ELECTRICCAR_H

#include "car.h"
#include "battery.h"
using namespace std;

class ElectricCar : public Car {
private:
    Battery battery;
public:
    ElectricCar(const string &maker0 = "", const string &model0 = "", int year0 = 0, int odometer0 = 0,int batterySize0=70);
    friend ostream &operator<<(ostream &out, const ElectricCar &A);
};

#endif //SHIYAN4_ELECTRICCAR_H
electricCar.cpp
//
// Created by KOKODA on 2019/5/18.
//

#include <iostream>
#include "electricCar.h"

ElectricCar::ElectricCar(const string &maker0, const string &model0, int year0, int odometer0, int batterySize0):Car(maker0,model0,year0,odometer0),battery(batterySize0){}

ostream &operator<<(ostream &out, const ElectricCar &A) {
    out<<(Car) A<<"Battery:"<<A.battery.getBatterySize()<<"-kWh"<<std::endl;
    return out;
}
main.cpp
#include <iostream>
#include "car.h"
#include "electricCar.h"

using namespace std;

int main() {
    Car oldcar("Audi", "a4", 2016);
    cout << "--------oldcar's info--------" << endl;
    oldcar.updateOdometer(25000);
    cout << oldcar << endl;
    ElectricCar newcar("Tesla", "model s", 2016);
    newcar.updateOdometer(2500);
    cout << "
--------newcar's info--------
";
    cout << newcar << endl;
    system("pause");
    return 0;
}

运行截图

ArrayInt类练习

源码

arrayInt.h
#ifndef ARRAY_INT_H
#define ARRAY_INT_H

class ArrayInt{
public:
    ArrayInt(int n, int value=0);
    ~ArrayInt();
    void print();
    int& operator[](int a);
private:
    int *p;
    int size;
};

#endif
arrayInt.cpp
#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) {
		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 a) {
    return p[a];
}
main.cpp
#include <iostream>

using namespace std;

#include "arrayInt.h"

int main() {
    ArrayInt a(2);
    a.print();

    ArrayInt b(3, 6);
    b.print();

    b[0] = 2;
    cout << b[0] << endl;
    b.print();

    system("pause");

    return 0;
}

运行截图

实验总结与体会

这次的实验没啥好讲的,唯一值得一提的就是我在ElectricCar类中对<<重载的实现上重复利用了Car类中的重载。

原文地址:https://www.cnblogs.com/KOKODA/p/10894297.html