标签:maker opera delete end pen close namespace val code
#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; }
#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
#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() { }
#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]; }
#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
#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; }
标签:maker opera delete end pen close namespace val code
原文地址:https://www.cnblogs.com/yinyinzuinihai/p/10907045.html