实验四

1.车辆信息.

battery.h:

 

#ifndef BATTERY_H

#define BATTERY_H

class Battery
{
    
    public: 
        friend class ElectricCar;
        Battery (string a1="70km/h")
        {
            a=a1;
        }
        Battery(Battery &p);
    private:
        string a;
};

 

#endif
car.h:

 

#ifndef CAR_H

#define CAR_H

#include <string>

#include <iostream>

using namespace std;

class Car{
    public:
        Car (string maker1,string model1,int year1){
            maker=maker1;
            model=model1;
            year=year1;
            odometer=0;
        };
        friend ostream &operator <<(ostream &out, Car &c);
        void updateOdometer(int odometer1){
            if(odometer>odometer1)
            {
                cout<<"输入错误\n"; 
            }
            else
            {
            odometer=odometer1;
            }
        }; 
        friend class ElectricCar;
        
    protected:
        string maker;
        string model;
        int year;
        int odometer;
};

 

#endif
electricCar.h:

#ifndef  ELECTRICCAR_H

#define ELECTRICCAR_H

#include "car.h"

#include "battery.h"

class ElectricCar : public Car{
    private:
        Battery battery;
    public:
        ElectricCar(string maker2,string model2,int year2):Car(maker2,model2,year2)
        {};
        friend ostream &operator <<(ostream &out, ElectricCar &d);
        string gatbattery(){
            return battery.a;
        }

};

#endif
car.cpp:

#include "car.h"

ostream& operator<<(ostream &out, Car &c){
    out<<"maker:"<<c.maker<<"\n"<<"model:"<<c.model<<"\n"<<"year:"<<c.year<<"\n"<<"odometer:"<<c.odometer;
    return out;
};
electricCar.h:

#include "electricCar.h"

ostream& operator<<(ostream& out, const ElectricCar& ec) {

    Car tmp = ec;

    out << tmp << endl;

    out << "batterySize: " << (ec.battery).getSize()<<"-kWh";

    return out;

}
main.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;
}

运行结果

 

 

2.重载运算符【】

arrayInt.h:

#ifndef ARRAY_INT_H

#define ARRAY_INT_H

 

class ArrayInt{
    public:
        ArrayInt(int n, int value=0);
        ~ArrayInt();
        // 补足:将运算符[]重载为成员函数的声明
        int  &operator[](int t);
        void print(); 
    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;
}
int &ArrayInt::operator[](int t)
{
    return p[t];
}
void ArrayInt::print() {
    for(int i=0; i<size; i++)
        cout << p[i] << " ";
    cout << endl;
}
 
main.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;
}

运行结果

在实验之前已经开始准备了,完成时间还是长,慢慢来吧

posted @ 2019-05-21 22:05  lj1831726125  阅读(134)  评论(1编辑  收藏  举报