码迷,mamicode.com
首页 > 其他好文 > 详细

实验4

时间:2019-05-22 17:34:55      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:maker   opera   delete   end   pen   close   namespace   val   code   

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

技术图片

实验4

标签:maker   opera   delete   end   pen   close   namespace   val   code   

原文地址:https://www.cnblogs.com/yinyinzuinihai/p/10907045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!