1. 车辆基本信息管理
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#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; }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#ifndef BATTERY_H #define BATTERY_H class Battery { public: Battery(int size = 70); int rebattery(); int batterySize; }; #endif
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#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; }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#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; }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#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
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#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
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include"battery.h" Battery::Battery(int size) { batterySize = size; } int Battery::rebattery() { return batterySize; }
2.补足程序
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#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
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#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; }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#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; }