栖枝Fairy

导航

实验四

1. 车辆基本信息管理

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

代码:

#ifndef BATTERY_H
#define BATTERY_H

class battery{
    public:
        battery(int bS=70);
        int getbattery();
    private:
        int batterySize;
};

#endif
#include "battery.h"
#include <iostream>

using std::cout;
using std::endl;

battery::battery(int bS):batterySize(bS){
};
int battery::getbattery(){
    return batterySize;
}
#ifndef CAR_H
#define CAR_H
#include <string>
using std::string;

class Car{
    public:
        Car(string ma,string mo,int y,int od=0);
        friend std::ostream & operator << (std::ostream &out,Car &c);
        int updateOdometer(int od);//更新里程数 
        string getMaker();
        string getModel();
        int getYear();
        int getOdometer();
    private:
        string maker;
        string model;
        int year;
        int odometer;
};

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

using std::cin;
using std::cout;
using std::endl;
using std::string;

Car::Car(string ma,string mo,int y,int od):maker(ma),model(mo),year(y),odometer(od){
};

string Car::getMaker(){
    return maker;
}

string Car::getModel(){
    return model;
}

int Car::getYear(){
    return year;
}

int Car::getOdometer(){
    return odometer;
}

int Car::updateOdometer(int od){
    if(odometer<=od)
    odometer=od;
    else{
        cout<<"error!"<<endl;
        cin>>odometer;
    }
    return odometer;
}

std :: ostream & operator << (std::ostream &out,Car &c){
    out<<"maker: "<<c.maker<<endl
    <<"model: "<<c.model<<endl
    <<"year: "<<c.year<<endl
    <<"odometer: "<<c.odometer<<endl;
    return out;
}
#ifndef ELECTRICCAR_H
#define ELECTRICCAR_H
#include "car.h"
#include "battery.h"

class ElectricCar:public Car,public battery {
    public:
        ElectricCar(string ma,string mo,int y,int od=0);
        friend std::ostream & operator<<(std::ostream &out,ElectricCar &c);
};

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

ElectricCar::ElectricCar(string ma,string mo,int y,int od):Car(ma,mo,y,od){
}

std::ostream & operator<<(std::ostream &out,ElectricCar &c){
    out<<"maker: "<<c.getMaker()<<endl
    <<"model: "<<c.getModel()<<endl
    <<"year: "<<c.getYear()<<endl
    <<"odomter: "<<c.getOdometer()<<endl
    <<"batterySize: "<<c.getbattery()<<"-KWh"<<endl;
    return out;
}
#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的成员函数,使得通过动态整形数组对象名和下标可以
访问对象中具体元素。

代码:

#ifndef ARRAY_INT_H
#define ARRAY_INT_H

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

#endif
#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 == NULL) {
        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 k){
    return p[k];
}
#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;
}

结果:

 

 小结:

第一题通过百度,查到了类似的题目,通过模仿,把他做了出来。说明我遇到这种综合的实验,还是有一点难度。基础一定要打好。

第二题做起来就比较轻松,只需要定义一个operator来实现,补充的程序也比较少。

posted on 2019-05-19 13:55  栖枝Fairy  阅读(185)  评论(0编辑  收藏  举报