1. 车辆基本信息管理 #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.updat
1. 车辆基本信息管理
#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.update(25000); cout << oldcar << endl; // 测试ElectricCar类 electricCar newcar("Tesla", "model s", 2016); newcar.update(2500); cout << "\n--------newcar‘s info--------\n"; cout << newcar << endl; system("pause"); return 0; }main
#ifndef CAR_H #define CAR_H #include<iostream> #include<string> using namespace std; class car{ public: car(string ma, string mo, int ye, int odo = 0) :maker(ma), model(mo), year(ye), odometer(odo) {} friend ostream & operator<<(ostream &out,const car &c); void update(int odo1); string maker,model; int year,odometer; }; #endifcar.h
#include"car.h" #include<iostream> #include<string> using std::cout; using std::endl; 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::update(int odo1) { if (odo1 < odometer) cout << "the number is wrong" << endl; else odometer = odo1; }car.cpp
#ifndef BATTERY_H #define BATTERY_H class battery{ public: battery(int size = 70); int fh(); int batterySize; }; #endifbattery.h
#include"battery.h" #include<iostream> using std::cout; using std::endl; battery::battery(int size) { batterySize = size; } int battery::fh() { return batterySize; }battery.cpp
#ifndef ELECTRICCAR_H #define ELECTRICCAR_H #include"car.h" #include"battery.h" #include<iostream> #include<string> class electricCar :public car, public battery { public: electricCar(string ma2, string mo2, int ye2, int odo2 = 0, int size1 = 70) :car(ma2, mo2, ye2, odo2), battery(size1) {} friend ostream& operator<<(ostream &outCar, const electricCar &a); void updateOdometer(int odo1); }; #endifelectricCar.h
#include"electricCar.h" #include"battery.h" #include"car.h" #include<iostream> using namespace std; ostream& operator<<(ostream &out, const electricCar &a) { out << "maker:" << a.maker << endl << "moder:" << a.model << endl << "year:" << a.year << endl << "odometer:" << a.odometer << endl << "batterySize:" << a.batterySize << "-kWh" << endl; return out; } void electricCar::updateOdometer(int odo1) { if (odo1 < odometer) cout << "the number is wrong" << endl; else odometer = odo1; }electricCar.cpp
2. 补足程序,重载运算符[]为一维动态整形数组类ArrayInt的成员函数,使得通过动态整形数组对象名和下标可以 访问对象中具体元素。
#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; }main.cpp
#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 == nullptr) { 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]; }ArrayInt.cpp
#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; }; #endifArrayInt.h
结论:
重载函数不能继承private里的变量,所以都放在public里定义了。
car函数定义变量比较多,前后一定要一致,我把main里的"electricCar" 改成了 "ElectricCar",“car”改成了“Car”所以总是运行不出结果。