c++实验4

1. 车辆基本信息管理 

#include <iostream>
using namespace std;
#include <string>
#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;

    return 0;
}
main.cpp
#ifndef BATTERY_H
#define BATTERY_H

class battery{
    public:
        battery(int ba=70);
        int batterysize;
};

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

battery::battery(int ba): batterysize(ba){
}
battery.cpp
#ifndef CAR_H
#define CAR_H
#include <string>
#include <iostream>
using namespace std;
class car{
    public:
        car(string ma,string mo,int y,int od=0);
        friend ostream &operator<<(ostream &out,car &c);
        void updateOdometer(int up);
        string getmaker();
                string getmodel();
                int getyear();
                int getodometer();

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

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

car::car(string ma,string mo,int y,int od):maker(ma),model(mo),year(y),odometer(od){
}
ostream &operator<<(ostream &out,car &c){
 out<<"maker: "<<c.maker<<endl
    <<"model: "<<c.model<<endl
    <<"year: "<<c.year<<endl
    <<"odometer: "<<c.odometer;
 return out;
}
void car::updateOdometer(int up){
 if(up<odometer)
     cout<<"update value error."<<endl;
 else
     odometer=up;
}

string car::getmaker(){
    return maker;
}

string car::getmodel(){
    return model;
}

int car::getyear(){
    return year;
}

int car::getodometer(){
    return odometer;
}
car.cpp
#ifndef ELECTRIC_CAR_H
#define ELECTRIC_CAR_H
#include "car.h"
#include "battery.h"
#include <string>
class ElectricCar: public car,public battery{
    public:
        ElectricCar(string ma,string mo,int y,int od=0,int ba=70);
        friend ostream &operator<<(ostream &out, ElectricCar &e);

};

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

ElectricCar::ElectricCar(string ma,string mo,int y,int od,int ba):car(ma,mo,y,od),battery(ba){                                            
}

ostream &operator<<(ostream &out, ElectricCar &e)
{
    out<<"maker:  "<<e.getmaker()<<endl
       <<"model:  "<<e.getmodel()<<endl
       <<"year:  "<<e.getyear()<<endl
       <<"odometer:  "<<e.getodometer()<<endl
       <<"batterySize:  "<<e.batterysize<<"-kWh";
    return out;
}
ElectricCar.cpp

 

 

 

 

2. 补足程序,重载运算符[]为一维动态整形数组类ArrayInt的成员函数,使得通过动态整形数组对象名和下标可以 访问对象中具体元素。 

#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();



    return 0;
}
main.cpp
#ifndef ARRAY_INT_H
#define ARRAY_INT_H

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

 

 

 实验小结:

1、在做第一题时,总会在编译运行时跳出这个界面,这使我有点崩溃

自己在很多地方修改了,但不知道这是什么。

在main中添加了 #include <string>

把main中的 #include "electricCar.h"  改成了 #include "ElectricCar.h"

2、类的成员函数如果有默认形参值,必须写在类的定义中,这也是我在编写时,犯过的错。

评论:

1、https://www.cnblogs.com/Yyaoyyy/p/10887670.html

2、https://www.cnblogs.com/knight04/p/10853219.html

3、https://www.cnblogs.com/zuiyankh/p/10884276.html

posted @ 2019-05-19 12:25  独倚泪静  阅读(154)  评论(2编辑  收藏  举报