C++实验四

1. 车辆基本信息管理

问题场景描述如下:
为了对车量基本信息进行管理,对现实世界车量基本信息抽象后,抽象出Car类、ElectricCar类、Battery类,
它们之间的关系描述如下:基于Car类派生出ElectricCar类,派生类ElectricCar中新增数据成员为Battery类
对象。

#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
#include"battery.h"

Battery::Battery(int size):batterySize(size)
{
}
int Battery::rebattery() {
    return batterySize;
}
battery.cpp
#ifndef BATTERY_H
#define BATTERY_H

class Battery {
public:
    Battery(int size = 70);
    int rebattery();
    int batterySize;
};

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

ostream& operator<<(ostream &out, const Car &c) {
    out << "maker:" << c.maker << endl << "moder:" << c.model << endl << "year:" << c.year << endl << "odometer:" << c.odometer << endl;
    return out;
}
void Car::updateOdometer(int o2) {
    if (o2 < odometer) cout << "the number is wrong" << endl;
    else odometer = o2;
}
car.cpp
#ifndef CAR_H
#define CAR_H
#include<iostream>
#include<string>
using namespace std;
class Car {
public:
    Car(string maker0, string model0, int year0, int odometer0 = 0) :maker(maker0), model(model0), year(year0), odometer(odometer0) {}
    friend ostream& operator<<(ostream &out, const Car &c);
    void updateOdometer(int o2);
    string maker, model;
    int year, odometer;
};
#endif
car.h
#include"electricCar.h"
#include"battery.h"
#include"car.h"
#include<iostream>
using namespace std;

ostream& operator<<(ostream &out, const ElectricCar &e) {
    out << "maker:" << e.maker << endl << "moder:" << e.model << endl << "year:" << e.year << endl << "odometer:" << e.odometer << endl << "batterysize:" << e.batterySize << "-kwh" << endl;
    return out;
}
void ElectricCar::updateOdometer(int o2) {
    if (o2 < odometer) cout << "the number is wrong" << endl;
    else odometer = o2;
}
electricCar.cpp
#ifndef ELECTRICCAR_H
#define ELECTRICCAR_H
#include"battery.h"
#include"car.h"
#include<iostream>
#include<string>
class ElectricCar :public Car, public Battery {
public:
    ElectricCar(string maker1, string model1, int year1, int odometer1 = 0, int size1 = 70) :Car(maker1, model1, year1, odometer1), Battery(size1) {}
    friend ostream& operator<<(ostream &outCar, const ElectricCar &e);
    void updateOdometer(int o2);
};
#endif
electric.h

 

2. 补足程序,重载运算符[]为一维动态整形数组类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
#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 a)
{
    return p[a];
}
// 补足:将运算符[]重载为成员函数的实现 
// ×××
arrayInt.cpp
#ifndef ARRAY_INT_H 
#define ARRAY_INT_H

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

转载于:https://www.cnblogs.com/lyc1103/p/10890495.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值