

#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; }


#ifndef BATTERY_H #define BATTERY_H class Battery{ public: Battery(int b=70); int getbatterySize(); int batterySize; }; #endif


#ifndef CAR_H #define CAR_H #include<iostream> #include<string> using namespace std; class Car{ public: Car(string x,string y,int z,int h=0):maker(x),model(y),year(z),odometer(h){} friend ostream& operator<<(ostream &out, const Car &c); void updateOdometer(int a); string maker,model; int year,odometer; }; #endif


#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 x,string y,int z,int h=0,int n=70):Car(x,y,z,h),Battery(n){} friend ostream& operator<<(ostream &outCar, const ElectricCar &e); void updateOdometer(int a); }; #endif


#include"battery.h" Battery::Battery(int b){ batterySize=b; } int Battery::getbatterySize(){ return batterySize; }


#include"car.h" #include<string> #include<iostream> using namespace std; ostream& operator<<(ostream &out, const Car &c){ out << "maker:" << c.maker << endl ; out << "model:" << c.model << endl ; out << "year:" << c.year << endl ; out << "odometer:" << c.odometer << endl; return out; } void Car::updateOdometer(int a){ if(a<odometer) cout<<"warning:wrong number"<<endl; odometer=a; }


#include"electricCar.h" #include<iostream> #include"car.h" #include"battery.h" using namespace std; ostream& operator<<(ostream &out, const ElectricCar &e) { out << "maker:" << e.maker << endl; out << "model:" << e.model << endl; out << "year:" << e.year << endl; out << "odometer:" << e.odometer << endl; out << "batterySize:" << e.batterySize << "-kWh" << endl; return out; } void ElectricCar::updateOdometer(int a) { if(a<odometer) cout<<"Warning!"<<endl; else odometer=a; }


#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; }


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


#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) { 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 m){ return p[m]; }
通过翻书查找大体可以做出来,但是在函数的实现里部分细节还是不太理解,包括引用到的函数头文件和派生类的运用还不够熟。
https://www.cnblogs.com/knight04/p/10853219.html
https://www.cnblogs.com/yfwg/p/10886264.html
https://www.cnblogs.com/99563220-fhy/p/10902912.html