C++实验四

#ifndef BATTERY_H
#define BATTERY_H

class Battery
{
public:
    Battery(int batterySize0=70);
    int showbattery()const;
    int batterySize;

};
#endif
battery.h
#include"battery.h"
Battery::Battery(int batterySize0):batterySize(batterySize0)
{
}

int Battery::showbattery()const
{
    return batterySize;
}
battery.cpp
#ifndef CAR_H
#define CAR_H
#include<string>
#include<ostream>
using std::string;
using std::ostream;
class Car{
    public:
        Car(string maker0, string model0, int year0, int odometer0=0);
        friend ostream& operator<<(ostream &out, const Car &gg1);
        void updateOdometer(int gg);
        string maker,model;
        int year,odometer;
};
#endif
car.h
#include"car.h" 
#include<string>
#include<iostream>
using namespace std;

Car::Car(string maker0, string model0, int year0, int odometer0) :maker(maker0), model(model0), year(year0), odometer(odometer0)
{}
void Car::updateOdometer(int gg)
{
    if (gg< odometer)
        cout << "wrong odometer" << endl;
    else
    odometer=gg;
}
ostream &operator<<(ostream &out, const Car &gg1)
{
    out << "maker:" << gg1.maker << endl
        << "model:" << gg1.model << endl
        << "year:" << gg1.year << endl
        << "odometer:" << gg1.odometer << endl;
    return out;
}
car.cpp
#ifndef ELECTRICCAR_H
#define ELECTRICCAR_H
#include"battery.h"
#include"car.h"
#include<iostream>
#include<string>
class ElectricCar:public Car,public Battery{
    public:
        ElectricCar(string maker1,string model1,int year1,int odometer1=0,int batterySize1=70):Car(maker1,model1,year1,odometer1),Battery(batterySize1){}
        friend ostream& operator<<(ostream &out, const ElectricCar &gg2);

};
#endif
electricCar.h
#include"electriccar.h"
using namespace std;

ostream& operator<<(ostream &out, const ElectricCar &gg2) {
    out << "maker:" << gg2.maker << endl << "model:" << gg2.model << endl << "year:" << gg2.year << endl << "odometer:" << gg2.odometer << endl << "batterySize:" << gg2.batterySize <<"-kWh"<< 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

 

 

#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 gg){
    return p[gg];
}
// 补足:将运算符[]重载为成员函数的实现
// ×××
arrayInt.cpp
#ifndef ARRAY_INT_H
#define ARRAY_INT_H

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

#endif
arrayInt.h
#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

 

 

实验小结:

第二题我用了dev没能运行出来,看了一下程序没问题,最后试了vs才运行出来,可能是dev配置不太行。

posted @ 2019-05-20 17:58  景行ai  阅读(166)  评论(1编辑  收藏  举报