实验四

1. 车辆基本信息管理

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

#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.h
#include "battery.h"
#include<iostream>
using namespace std;
Battery::Battery(int batterySize0){
    batterySize=batterySize0;
}
int Battery::GetbatterySize(){
    return batterySize;
}
Battery.cpp
#ifndef CAR_H
#define CAR_H
#include<iostream>
using namespace std;

class Car{
    public:
        Car(string maker0=" ",string model0=" ",int year0=2000);
        void updateOdometer(int newOdometer);
        string Getmaker()const;
        string Getmodel()const;
        int Getyear()const;
        int Getodometer()const;
        friend ostream & operator<<(ostream &out,const Car &t);
    private:
        string maker;
        string model;
        int year;
        int odometer;
};

#endif
Car.h
#include "car.h"
#include<iostream>
using namespace std;

Car::Car(string maker0,string model0,int year0){
    maker=maker0;
    model=model0;
    year=year0;
    odometer=0;
}

string Car::Getmaker()const{
    return maker;
}

string Car::Getmodel()const{
    return model;
}

int Car::Getyear()const{
    return year;
}

int Car::Getodometer()const{
    return odometer;
}

void Car::updateOdometer(int newOdometer){
    if(odometer<odometer)
    cout<<"Warning!Update data error."<<endl;
    else
    odometer=newOdometer;
}

ostream & operator<<(ostream &out,const Car &t){
    out<<"Maker:"<<t.maker<<endl<<"Model:"<<t.model<<endl<<"Year:"<<t.year<<endl<<"Odometer:"<<t.odometer<<endl;
    return out;
}
Car.cpp
#ifndef ELECTRIC_CAR_H
#define ELECTRIC_CAR_H
#include "battery.h"
#include "car.h"

class ElectricCar:public Car{
    public:
        ElectricCar(string maker0=" ",string model0=" ",int year0=2000,int odometer0=0);
        friend ostream & operator<<(ostream &out,const ElectricCar &m);
    private:
        Battery battery;
        int batterysize;
};
#endif
ElectricCar.h
#include "electriccar.h"
#include<iostream>
#include "car.h"
#include "battery.h"
using namespace std;

ElectricCar::ElectricCar(string maker0,string model0,int year0,int odometer):Car(maker0,model0,year0){
    batterysize=battery.GetbatterySize();
}

ostream & operator<<(ostream &out,const ElectricCar &m)
{
    out<<"Maker:"<<m.Getmaker()<<endl
       <<"Model:"<<m.Getmodel()<<endl
       <<"Year:"<<m.Getyear()<<endl
       <<"Odometer:"<<m.Getodometer()<<endl
       <<"BatterSize:"<<m.batterysize<<endl;
    return out;
}
ElectricCar.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. 补足程序,重载运算符[]为一维动态整形数组类ArrayInt的成员函数,使得通过动态整形数组对象名和下标可以
访问对象中具体元素。

#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.h
arrayInt.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;
}
main.cpp

 

posted @ 2019-05-20 08:55  GeorgeWan  阅读(107)  评论(1编辑  收藏  举报