实验四

实验结论:

(1)

#ifndef BATTERY_H
#define BATTERY_H
class Battery
{
public:
    Battery(int x=70);
    int showbattery();
    int batterySize;

};
#endif
battery.h
#ifndef CAR_H
#define CAR_H
#include <string>
#include <iostream>
using namespace std;
class Car {
public:
    Car(string x, string y, int a, int b = 0);
    friend ostream&operator<<(ostream &out, Car &a);
    void updateOdometer(int x);
    string maker, model;
    int year, odometer;

};
#endif
car.h
#ifndef ELECTRICCAR_H
#define ELECTRICCAR_H
#include "battery.h"
#include "car.h"
class ElectricCar :public Car, public Battery {
public:
    ElectricCar(string x, string y, int a, int b = 0,int c=70);
    friend ostream&operator<<(ostream &out, ElectricCar &a);
private:
    Battery battery;

};
#endif 
electricCar.h
#include "battery.h"
Battery::Battery(int x):batterySize(x)
{
}
int Battery::showbattery() {
    return batterySize;
}
battery.cpp
#include <iostream>
using namespace std;
#include <string>
#include "car.h"
Car::Car(string x, string y, int a, int b ) :maker(x), model(y), year(a), odometer(b) {

}
ostream &operator<<(ostream &out, Car &a) {
    out << "maker:" << a.maker << endl
        << "model:" << a.model << endl
        << "year:" << a.year << endl
        << "odometer:" << a.odometer << endl;
    return out;
}
void Car::updateOdometer(int x) {
    if ( x < odometer)
        cout << "the new odometer is wrong!" << endl;
    else
        odometer = x;
    odometer = x;
}
car.cpp
#include "electricCar.h"
#include "car.h"
#include "battery.h"
#include <iostream>
using namespace std;
ElectricCar::ElectricCar(string x, string y, int a, int b, int c) :Car(x, y, a, b),Battery(c){

}
ostream &operator<<(ostream &out, ElectricCar &a) {
    out << "maker:" << a.maker << endl
        << "model:" << a.model << endl
        << "year:" << a.year << endl
        << "odometer:" << a.odometer << endl
        << "batterysize:" << a.batterySize <<"-kwh"<< endl;
        return out;
}
elecrtricCar.cpp
#include <iostream>
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

运行截图:

 

(2)

#ifndef ARRAY_INT_H
#define ARRAY_INT_H

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

#endif
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) {
        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 x)
{
    return p[x];
}
arrayint.cpp
#include <iostream>
using namespace std;

#include "arrayInt.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

运行截图:

实验总结与体验:

 1、了解[ ],<<运算符的重载方式,知道不同的地址访问方式

2、类的派生与继承,编程过程中纠正很多细节性错误,头文件类后的分号,参数传递,初始化方式。

posted @ 2019-05-18 14:39  Ternura  阅读(107)  评论(0编辑  收藏  举报