1.
#include "battery.h" battery::battery(int batterysize0) :batterysize(batterysize0) { }; int battery::showbattery() { return batterysize; }
#ifndef battery_H #define battery_H class battery{ public: battery(int batterysize0=70); int showbattery(); private: int batterysize; }; #endif
#include"car.h" #include<string> #include<iostream> using namespace std; Car::Car(string maker0, string model0, int year0, int odometer0) :maker(maker0), model(model0), year(year0), odometer(odometer0) { }; string Car::showmaker() { return maker; } string Car::showmodel() { return model; } int Car::showyear() { return year; } int Car::showodometer() { return odometer; } ostream &operator<<(ostream &out, Car &c) { out << "maker:" << c.maker << endl << "model:" << c.model << endl << "year:" << c.year << endl << "odometer:" << c.odometer << endl; return out; } int Car::updateOdometer(int a) { if (a < odometer) cout << "the new odometer is wrong" << endl; else odometer = a; return odometer; }
#ifndef CAR_H #define CAR_H #include <string> #include <iostream> using namespace std; class Car { public: Car(string maker0, string model0, int year0, int odometer0 = 0); friend ostream&operator<<(ostream &out, Car &c); int updateOdometer(int a); string showmaker(); string showmodel(); int showyear(); int showodometer(); private: string maker; string model; int year; int odometer; }; #endif
#include <iostream> #include "car.h" #include "battery.h" #include "electricCar.h" using namespace std; ElectricCar::ElectricCar(string maker0, string model0, int year0, int odometer0) :Car(maker0, model0, year0, odometer0) { } std::ostream &operator<<(std::ostream &out, ElectricCar &c) { out << "maker:" << c.showmaker() << endl<< "model:" << c.showmodel() << endl<< "year:" << c.showyear() << endl<< "odometer:" << c.showodometer() << endl<< "batterysize:" << c.showbattery() << "-kwh" << endl; return out; }
#ifndef electric_H #define electric_H #include"battery.h" #include"car.h" class ElectricCar : public Car, public battery { public: ElectricCar(string maker0, string model0, int year0, int odometer0 = 0); friend std::ostream & operator<<(std::ostream &out, ElectricCar &c); private: battery battery; }; #endif
#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; }

2.
#ifndef ARRAY_INT_H #define ARRAY_INT_H class ArrayInt { public: ArrayInt(int n, int value = 0); ~ArrayInt(); int &operator[](int x);// 补足:将运算符[]重载为成员函数的声明 // ××× 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 == 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 x) { return p[x]; }
#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; }

总结:1.通过本次实验,发现自己在第一题综合性问题上做的不是很好,所以前面掌握的都不是很好,遇到报错自己也不能解决需要别人的帮助。2.希望自己能多花点时间去学习自己没有掌握的知识。
https://www.cnblogs.com/Yyaoyyy/p/10887670.html
https://www.cnblogs.com/qsxsc/p/10887283.html
https://www.cnblogs.com/lovecpp/p/10854382.html
浙公网安备 33010602011771号