实验四

1

#ifndef CAR_H
#define CAR_H
#include<string> 
#include<iostream>
using namespace std;

class Car{
         public:
             Car(string maker0,string model0,int year0,int odometer0=0);
             friend ostream& operator<<(ostream &out,const Car &t);
             int updateOdometer(int nodometer);
             string getmaker();
             string getmodel();
             int getyear();
             int getodometer();
         private:
                 string maker,model;
                 int year,odometer;
};


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

Car::Car(string maker0, string model0, int year0,int odometer0) :maker(maker0), model(model0), year(year0), odometer(odometer0) {};

ostream & operator<<(ostream &out,const Car &t){
    out<<"maker:"<<t.maker<<endl
       <<"model:"<<t.model<<endl
       <<"year:"<<t.year<<endl
       <<"odometer:"<<t.odometer<<endl;

    return out;
};

int Car::updateOdometer(int nodometer){
    if(nodometer<odometer)
        cout<<"更新数值有误"<<endl;
    else
        odometer=nodometer;
    
    return odometer;
};

string Car::getmaker() {
    return maker;
}
string Car::getmodel() {
    return model;
}
int Car::getyear() {
    return year;
}
int Car::getodometer() {
    return odometer;
}
Car.cpp
#ifndef BATTERY_H
#define BATTERY_H

class Battery{
             public:
                   Battery(int batterySize0=70);
                   int getbatterySize();
             private:
                    int batterySize;
};

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

Battery::Battery(int batterySize0):batterySize(batterySize0){}

int Battery::getbatterySize(){
    return batterySize;
}
battery.cpp
#ifndef ELECTRICAR_H
#define ELECTRICAR_H
#include "electricCar.h"
#include "car.h"
#include "battery.h"
#include<iostream>
#include<string> 
using namespace std;

class ElectricCar:public Car,public Battery{
     
      public:
             ElectricCar(string maker0,string model0,int year0,int odometer0=0);
             friend ostream& operator<<(ostream &out,const  ElectricCar &t);
      private:
              Battery battery;
};

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

ElectricCar::ElectricCar(string maker0,string model0,int year0,int odometer0):Car(maker0,model0,year0,odometer0){}

ostream & operator<<(ostream &out, ElectricCar &t){
    out<<"maker:"<<t.getmaker()<<endl
       <<"model:"<<t.getmodel()<<endl
       <<"year:"<<t.getyear()<<endl
       <<"odometer:"<<t.getodometer()<<endl
       <<"batterySize:"<<t.getbatterySize()<<"-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 "electricCar.h"
#include "car.h"
#include "battery.h"
#include<iostream>
#include<string> 
using namespace std;

ElectricCar::ElectricCar(string maker0,string model0,int year0,int odometer0):Car(maker0,model0,year0,odometer0){}

ostream & operator<<(ostream &out,ElectricCar &t){
    out<<"maker:"<<t.getmaker()<<endl
       <<"model:"<<t.getmodel()<<endl
       <<"year:"<<t.getyear()<<endl
       <<"odometer:"<<t.getodometer()<<endl
       <<"batterySize:"<<t.getbatterySize()<<"-kWh"<<endl;

    return out;
}
electricCar.cpp
#ifndef ELECTRICAR_H
#define ELECTRICAR_H
#include "electricCar.h"
#include "car.h"
#include "battery.h"
#include<iostream>
#include<string> 
using namespace std;

class ElectricCar:public Car,public Battery{
     
      public:
             ElectricCar(string maker0,string model0,int year0,int odometer0=0);
             friend ostream& operator<<(ostream &out,ElectricCar &t);
      private:
              Battery battery;
};

#endif
elecyricCar.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.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 == 0) {
        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 t){
    return p[t];
}
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

 

 

总结:

1.第一题没有结果,模块计算机与目标计算机冲突,我查了之后设置了项目属性和配置管理器,但还是没有用。

2.第一题编写程序的时候大概思路没什么问题,就是总是要查资料。编写出的程序调试的时候有许多的小问题.

修改后:第一题的错误是头文件中的声明与函数的实现时不匹配,多了一个const。

 

 

互评:

https://www.cnblogs.com/laboratory-X/

https://www.cnblogs.com/sq102217/

https://www.cnblogs.com/csl-40/

posted @ 2019-05-19 16:47  shona小暖  阅读(155)  评论(1编辑  收藏  举报