实验4

1
#include"battery.hpp"
#include<iostream>
using namespace std;
//构造函数
Battery::Battery(int bts) :batterysize(bts) {}
//返回电池容量
int Battery::showbatterysize() {
    return batterysize;
}
void Battery::change(int n) {
    batterysize = n;
}
battery.cpp
#ifndef battery_hpp
#define battery_hpp
#include <stdio.h>
class Battery {
public:
    Battery(int bts = 70);
    int showbatterysize();
    void change(int n);
private:
    int batterysize;
};
#endif
battery.hpp
#include<iostream>
#include"ElectriCar.hpp"
#include"car.hpp"
#include"battery.hpp"
using namespace std;

ElectriCar::ElectriCar(string maker0,string model0,int year0):car(maker0,model0,year0)
{
  battery=battery.showbatterysize();
}

ostream &operator<<(ostream &out,const ElectriCar &e)
{
    out<<"maker:  "<<e.getmaker()<<endl
    <<"model:  "<<e.getmodel()<<endl
    <<"year:  "<<e.getyear()<<endl
    <<"odometer:  "<<e.getodometer()<<endl
    <<"batterySize:  "<<e.batterysize<<"-kWh"<<endl;
    return out;
}

ElectriCar::~ElectriCar()
{ }
electricar.cpp
electrical.hpp
car.hpp
main.cpp
2
#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::show()
{
    for(int i=0;i<size;i++) cout<<p[i]<<" ";
    cout<<endl;
}

int &ArrayInt::operator[](int a)
{
    return p[a];
}
arraying.cpp
#ifndef ARRAY_INT_H
#define ARRAY_INT_H

class ArrayInt{
    public:
        ArrayInt(int n, int value=0);
        ~ArrayInt();
    int &operator[](int a);
    void show(); 
    private:
        int *p;
        int size;
};

#endif
arraying.hpp
#include <iostream>
using namespace std;

#include "arrayInt.h"

int main() {
    
    ArrayInt a(2);
    a.show();
    
    
    ArrayInt b(3, 6);
    b.show();

    b[0] = 2;
    cout << b[0] << endl;
    b.show();

    system("pause");

    return 0;
}
main.cpp

转载于:https://www.cnblogs.com/yinyinzuinihai/p/10907045.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值