实验四

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

class Battery
{
    public:
        Battery(int batterSize0=70);
        int getbatterysize();
       
    private:
        int batterySize;    
};

#endif
#include"Battery.h"

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

int Battery::getbatterysize()
{
    return batterySize;
}

Battery::~Battery()
{}
#ifndef CAR_H
#define CAR_H
#include
using std::string;
using std::ostream;
class Car
{
    public:
        Car(string maker0,string model0,int year0);
        friend ostream &operator<<(ostream &out,const Car &c);
        void updateOdometer(int updatemeter);
        const string getmaker() ;
        const string getmodel() ;
        const int getyear() ;
        const int getodometer() ;
    private:
        string maker;
        string model;
        int year;
        int odometer;
};

#endif
#include
#include"Car.h"
using namespace std;

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

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

void Car::updateOdometer(int updatemeter)
{
    if(updatemeter<odometer)
    {
        cout<<"Warning:Wrong!"<<endl;
    }
    else odometer=updatemeter;
}

string Car::getmaker() const
{
    return maker;
}

string Car::getmodel() const
{
    return model;
}

int Car::getyear() const
{
    return year;
}

int Car::getodometer() const
{
    return odometer;
}

Car::~Car()
{ }
#ifndef ElectricCar_H
#define ElectricCar_H
#include
#include"Car.h"
#include"Battery.h"
using std::string;
using std::ostream;

class ElectricCar:public Car
{
    public:
        ElectricCar(string maker0,string model0,int year0);
        friend ostream &operator<<(ostream &out,const ElectricCar &e);
        ~ElectricCar();
    private:
        Battery battery;
        int batterySize;
};

#endif
#include"ElectricCar.h"
#include"Car.h"
#include"Battery.h"
using namespace std;

ElectricCar::ElectricCar(string maker0,string model0,int year0):Car(maker0,model0,year0)
{
    batterySize=battery.getbatterysize();
}

ostream &operator<<(ostream &out,const ElectricCar &e)
{
    out<<"maker"<<":"<endl
       <<"model"<<":"<endl
       <<"year"<<":"<endl
       <<"odometer"<<":"<endl
       <<"batterySize"<<":"<"-kWh"<<endl;
    return out;
}

ElectricCar::~ElectricCar()
{ }


实验四_第1张图片




#include 
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;
}
 
  
#ifndef ARRAY_INT_H
#define ARRAY_INT_H

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

#endif
#include "arrayInt.h"
#include 
#include 
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)
        p[i] = value;
}

ArrayInt::~ArrayInt() {
    delete[] p;
}

void ArrayInt::print() {
    for(int i=0; i)
        cout << p[i] << " ";
    cout << endl;
}

int &ArrayInt::operator[](int n){
    
    return p[n];
    
}// 补足:将运算符[]重载为成员函数的实现

实验四_第2张图片

1.第一个程序写得挺不容易的,一开始错了很多地方,一直编译错误,也不知道错在哪里,后来在同学的帮助下修改正确,这个程序里运算符重载用了友元函数,其他比较值得注意的地方就是使用了输出流。

2.第二个程序总体来说不难,主要知识点如下:运算符重载为成员函数一般语法形式为:返回值类型 operator 运算符(形参表){函数体}。

运算符重载为非成员函数一般语法形式为:返回值类型 operator 运算符(形参表){函数体}。

 

https://www.cnblogs.com/laboratory-X/p/10896615.html

https://www.cnblogs.com/wjh1022/p/10890196.html

https://www.cnblogs.com/wjh1022/p/10890196.html




 

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