c++实验四 类的继承、派生和多态

实验目的

1. 理解类的继承和派生机制

2. 掌握派生类的定义和使用

3. 理解和掌握派生类成员的标识和访问中同名覆盖原则、二元作用域分辨符和虚基类的用法

4. 掌握派生类构造函数和析构函数的定义及调用次序

5. 理解运算符重载的目的,掌握运算符重载函数的编写方法

实验内容

一、ElectricCar类

#ifndef BATTERY_H
#define BATTERY_H
class Battery {
public:
    Battery(int nb = 70);
    int showbatterysize();
    void change(int n);
private:
    int batterysize;
};
#endif

battery.h
battery.h
#include"battery.h"
#include<iostream>
using namespace std;
//构造函数
Battery::Battery(int nb/*=70*/) :batterysize(nb) {}
//返回电池容量
int Battery::showbatterysize() {
    return batterysize;
}
void Battery::change(int n) {
    batterysize = n;
}

battery.cpp
battery.cpp
#ifndef CAR_H
#define CAR_H
#include<string>
#include<iostream>
using namespace std;
class Car {
private:
    string maker;
    string model;
    int year;
    long odometer;
public:
    Car(string n1 = "unknown", string n2 = "unknown", int n3 = 2000,long n4=0);
    void flashodo(long a);
    friend ostream & operator<<(ostream &out, const Car &c);
};
#endif

car.h
car.h
#include"car.h"
#include<iostream>
#include<string>
using namespace std;
Car::Car(string n1/*="unknown"*/, string n2/*="unknown"*/, int n3/*=2000*/, long n4/*=0*/) :maker(n1), model(n2), year(n3), odometer(n4) {}
void Car::flashodo(long no) {
    if (no < odometer)
        cout << "WARNING!" << endl;
    else
        odometer = no;
}
ostream & operator<<(ostream &out, const Car &c) {
    out << "(" << "maker:" << "," << c.maker << "," << endl << "," << "model:" << "," << c.model << "," << endl << "," << "year:" << "," << c.year << "," << endl << "," << "odometer:" << "," << c.odometer << ")";
    return out;
}

car.cpp
car.cpp
#ifndef ELECTRICCAR_H
#define ELECTRICCAR_H
#include<string>
#include<iostream>
using namespace std;
#include"car.h"
#include"battery.h"
class ElectricCar :public Car {
public:

    ElectricCar(string n1 = "unknown", string n2 = "unknown", int n3 = 2000, long n4 = 0, int nb = 70);
    friend ostream & operator<<(ostream &out, const ElectricCar &c);
private:
    Battery battery;
}
electricCar.h
#include<string>
#include<string>
#include<iostream>
#include"car.h"
#include"battery.h"
#include"electricCar.h"
using namespace std;
ElectricCar::ElectricCar(string n1/*="unknown"*/, string n2/*="unknown"*/, int n3/*=2000*/, long n4/*=0*/, int nb/*=70*/) :Car(n1, n2, n3, n4) {
    battery.change(nb);
}
ostream & operator<<(ostream &out, const ElectricCar &c) {
    Battery a = c.battery;
    out << "("<< (Car)c <<"," << endl << "," << "batterysize:" << "," << a.showbatterysize() << ")";
    return out;
}

electricCar.cpp
electricCar.cpp
#include<string>
#include<iostream>
#include<cstdlib>
using namespace std;
#include "car.h"
#include "electricCar.h"
int main() {
    // 测试Car类 
    Car oldcar("Audi", "a4", 2016);
    cout << "--------oldcar's info--------" << endl;
    oldcar.flashodo(25000);
    cout << oldcar << endl;
    // 测试ElectricCar类
    ElectricCar newcar("Tesla", "model s", 2016);
    newcar.flashodo(2500);
    cout << "\n--------newcar's info--------\n";
    cout << newcar << endl;
    system("pause");
    return 0;
}

main.cpp
main.cpp

运行结果:

二、arrayint类

#ifndef ARRAY_INT_H
#define ARRAY_INT_H

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

#endif

arrayInt.h
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 == 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 i){
    return p[i];
}

arrayInt.cpp
arrayInt
#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
main.cpp

运行结果:

猜你喜欢

转载自www.cnblogs.com/sq102217/p/10903338.html