天才吃鸡少女

导航

实验四

第一题

   main.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;
}
View Code

    battery.h

#ifndef _BTRY
#define _BTRY

class Battery
{
    private:
        double batterySize, nowSize;
    public:
        Battery(double bSize=70);
        bool rechargeTo(double);
        bool useTo(double);
        double getNowSize(void);
        double getBatterySize(void);
};

#endif
View Code

   battery,cpp

#include "battery.h"


Battery::Battery(double bSize){
    nowSize = batterySize = bSize;
}
bool Battery::rechargeTo(double target){
    if(target>batterySize || target<nowSize)
        return false;
    nowSize = target;
    return true;
}
bool Battery::useTo(double target){
    if(target>nowSize || target<0)
        return false;
    nowSize = target;
    return true;
}
double Battery::getNowSize(void){return nowSize;}
double Battery::getBatterySize(void){return batterySize;}
View Code

car.h

#include <iostream>
#include <string>
using namespace std;

class Car
{
    protected:
        string maker, model;
        int year, odometer;
        friend class ElectricCar;
    public:
        Car(string, string, int);
        friend ostream & operator <<(ostream &out, Car &p); 
        bool updateOdometer(int);
};
View Code

car.cpp

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

Car::Car(string imaker, string imodel, int iyear):maker(imaker),model(imodel),year(iyear){ odometer = 0; }
bool Car::updateOdometer(int newodo){
    if(newodo<odometer)
        return false;
    odometer=newodo;
    return true;
}
ostream & operator <<(ostream &out, Car &p){
    out<<"maker: "<<p.maker<<endl;
    out<<"model: "<<p.model<<endl;
    out<<"year: "<<p.year<<endl;
    out<<"odometer: "<<p.odometer<<endl;
}
View Code

electriccar.h

 

#include "battery.h"

class ElectricCar: public Car{
    private:
        Battery battery;
    public:
        ElectricCar(string, string, int);
        friend ostream & operator <<(ostream &out, ElectricCar &p);
};
View Code

 

electericcar.cpp

#include "electriccar.h"
#include <string>
using namespace std;

ElectricCar::ElectricCar(string imaker, string imodel, int iyear):Car(imaker,imodel,iyear),battery(){}

ostream & operator <<(ostream &out, ElectricCar &p){
    out<<"maker: "<<p.maker<<endl;
    out<<"model: "<<p.model<<endl;
    out<<"year: "<<p.year<<endl;
    out<<"odometer: "<<p.odometer<<endl;
    out<<"batterySize: "<<p.battery.getNowSize()<<"-kWh"<<endl;
}
View Code

 

 

第二题:

 

#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 index) {
  if(index>=size)
    throw  1;
    return p[index];
}
View Code

 

posted on 2019-05-21 00:10  天才吃鸡少女  阅读(115)  评论(0编辑  收藏  举报