实验四

1.代码如下:

#ifndef CAR_H
#define CAR_H
#include<string>
using std::string;
class Car{
    private:
        string maker;
        string model;
        int year;
        int odometer;
    public:
        Car(string maker1,string model1,int year1,int odometer1=0);
        int updateOdometer(int a);
        friend std::ostream & operator<<(std::ostream &os, Car &c);
        string getMaker();
        string getModel();
        int getYear();
        int getOdometer();
}; 
#endif
#include"car.h"
#include<iostream>
#include<string>
using namespace std;

Car::Car(string maker1,string model1,int year1,int odometer1):maker(maker1),model(model1),year(year1),odometer(odometer1){
};

string Car::getMaker(){
    return maker;
}

string Car::getModel(){
    return model;
}

int Car::getYear(){
    return year;
}

int Car::getOdometer(){
    return odometer;
}
int Car::updateOdometer(int a){
    if(odometer<=a)
    odometer=a;
    else{
    cout << "更新数值有误,请重新输入:(要比 " << odometer << "大)";
    cin >> odometer;}
    return odometer;
}

std::ostream & operator<<(std::ostream &os,Car &c){
    os << "maker: " << c.maker << endl
       << "model: " << c.model << endl
       << "year: "  << c.year << endl
       << "odometer: " << c.odometer << endl;
    return os;
}
#ifndef BATTERY_H
#define BATTERY_H
class Battery{
    private:
        int batterySize;
    public:
        Battery(int batterySize1=70);
        int getBattery();
};
#endif
#include"battery.h"

Battery::Battery(int batterySize1):batterySize(batterySize1){
};
int Battery::getBattery(){
    return batterySize;
}
#ifndef ELECTRICCAR_H
#define ELECTRICCAR_H
#include"car.h"
#include"battery.h"
class ElectricCar : public Car,public Battery{
    private:
        Battery battery;
    public:
        ElectricCar(string maker1,string model1,int year1,int odometer1=0);
        friend std::ostream & operator<<(std::ostream &os, ElectricCar &c);
        
};
#endif
#include"car.h"
#include"battery.h"
#include"electricCar.h"
#include<iostream>
using namespace std;
ElectricCar::ElectricCar(string maker1,string model1,int year1,int odometer1):Car( maker1, model1,year1, odometer1){
}
std::ostream & operator<<(std::ostream &os,ElectricCar &c){
    os << "maker: " << c.getMaker() << endl
       << "model: " << c.getModel() << endl
       << "year: "  << c.getYear() << endl
       << "odometer: " << c.getOdometer() << endl
       << "batterySize: " << c.getBattery()<< "-kWh" << endl;
    return os;
}
#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;
}

运行结果如下:

 

2.代码如下:

#ifndef ARRAY_INT_H
#define ARRAY_INT_H

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

#endif
#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 a){
    return p[a];
}

// 补足:将运算符[]重载为成员函数的实现
// ×××
#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;
}

运行结果如下:

 总结:1.第一个程序写的时候,有很多的错误:car.cpp刚开始头文件没写string,重载<<时候Car类前面写了const(原来写const的时候运行不了,删除const就能运行了),还有派生类electriccar的构造函数要与car的构造函数相对应

electriccar中不能直接访问car中的私有成员,所以在car中写了四个函数分别得到它的私有成员。

2.第二个程序要注意的是它的返回值是int,然后&放在arrayint前面。

3.对于重载的时候要注意不要和其他的符号冲突,就比如重载<<时候它的return 为cout。

posted @ 2019-05-16 19:59  Joey_Yan  阅读(136)  评论(2)    收藏  举报