第四次实验

类的继承派生与运算符重载实验总结
本次实验围绕类的继承和派生、运算符重载展开。通过实验,理解了类的继承和派生机制,掌握了派生类的定义和使用。实验中遇到派生类构造函数语法用错、[]运算符重载表达式问题,最终用引用解决。

1.类的继承和派生

#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
#ifndef CAR_H
#define CAR_H
#include<iostream>
#include<string>
using namespace std;
class Car {
public:
    Car(string a=" ",string b=" ",int c=0,const int d=0):marker(a),model(b),year(c),odometer(d){}
    void updateOdometer(int t) {
        if (odometer > t)
            cout << "更新数值有误" << endl;
        else
            odometer = t;
        
    }
    friend ostream & operator<<(ostream &out,Car &c1);
    string getm() {
        return marker;
    }
    string getmo() {
        return model;
    }
    int gety() {
        return year;
    }
    int geto() {
        return odometer;
    }
    void geto1(int t) {
        odometer = t;
        
    }
private:
    string marker;
    string model;
    int year;
    int odometer;
};
ostream & operator<<(ostream &out,Car &c1){
    cout << "marker:" << c1.marker << endl;
    cout << "model:" << c1.model << endl;
    cout << "year:" << c1.year << endl;
    cout << "odometer:" << c1.odometer << endl;
    return out;
}
#endif
car.h
#ifndef ELECTRICCAR_H
#define ELECTRICCAR_H
#include<iostream>
#include"battery.h"
using namespace std;
class ElectricCar:public Car{
public:
    ElectricCar(string a,string b,int c, int d=70,const int e=0):Car(a, b, c, e) {
        battery = d;
    }
    friend ostream & operator<<(ostream &out, ElectricCar &c1);
    void updateOdometer(int t) {
        if (geto() > t)
            cout << "更新数值有误" << endl;
        else
            geto1(t);
        
    }
private:
    Battery battery;
};
ostream & operator<<(ostream &out, ElectricCar &c1) {
    cout << "marker:" << c1.getm()<< endl;
    cout << "model:" << c1.getmo() << endl;
    cout << "year:" << c1.gety() << endl;
    cout << "odometer:" << c1.geto() << endl;
    cout << "batterySize:" << c1.battery.show()<< "-kWh"<<endl;
    return out;
}
#endif 
electricCar.h
#ifndef BATTERY_H
#define BATTERY_H
class Battery {
public:
    Battery(int a=70):batterySize(a){}
    int show() {
        return batterySize;
    }
private:
    int batterySize;
};
#endif
battery.h

2. 运算符重载

#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
#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 n) {
    return p[n];
}
arrayint.cpp
#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
arrayint.h

实验总结与体会:

1.通过这次实验,我理解了类的继承和派生机制,掌握派生类的定义和使用。

2.在这次实验中遇到了一些问题,一个是派生类构造函数的语法形式用错了,另一个是[]运算符重载时b[0]=2一直跳出表达式必须是可修改的左值,卡了许久,最后用了引用才搞定。

转载于:https://www.cnblogs.com/linjiahao1035/p/10894919.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值