实验四

1.

#include 
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
#ifndef BATTERY_H
#define BATTERY_H

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

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

int Battery::getbatterySize(){
    return batterySize;
}
battery.cpp
#ifndef ELECTRICCAR_H
#define ELECTRICCAR_H
#include"battery.h"
#include"car.h"
#include
#include<string>

class ElectricCar:public Car,public Battery{
    public:
        ElectricCar(string maker0,string model0,int year0,int odometer0=0,int batterySize0=70):Car(maker0,model0,year0,odometer0),Battery(batterySize0){}
        friend ostream& operator<<(ostream &outCar, const ElectricCar &c);

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

ostream &operator<<(ostream &out,const ElectricCar &c1) {
    out << "maker:" << c1.maker << endl
        << "model:" << c1.model << endl
        << "year:" << c1.year << endl
        << "odometer:" << c1.odometer << endl
        << "batterysize:" << c1.batterysize <<"-kwh"<< endl;
        return out;
}
electriccar.cpp
#ifndef CAR_H
#define CAR_H
#include<string>
#include
using std::string;
using std::ostream;
class Car{
    public:
        Car(string maker1, string model1, int year1, int odometer1=0);
        friend ostream& operator<<(ostream &out, const Car &a);
        void updateOdometer(int b);
        string maker,model;
        int year,odometer;
};
#endif
car.h
#include"car.h"
#include<string>
#include
using namespace std;

ostream &operator<<(ostream &out,Car &c){
    out<<"maker:"<endl
       <<"model:"<endl
       <<"year:"<endl
       <<"odometer:"<endl;
       return out;
}

void Car::updateOdometer(int odometer1){
    if(odometer1<odometer) 
    cout<<"wrong"<<endl;
    else 
    odometer=odometer1;
}
car.cpp

 

 

 

 

 

2.

#include 
using namespace std;
#include "arraylnt.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
#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 "arraylnt.h"
#include 
#include 
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)
p[i] = value;
}
ArrayInt::~ArrayInt() {
delete[] p;
}
void ArrayInt::print() {
for(int i=0; i)
cout << p[i] << " ";
cout << endl;
}
int& ArrayInt::operator[](int i){
    return p[i];
}
// 补足:将运算符[]重载为成员函数的实现
// ×××
arrayInt.cpp

 

你可能感兴趣的:(实验四)