实验4
1.车
#ifndef BATTERY_H #define BATTERY_H class Battery { public: Battery(int marker = 70); int showbattery(); int batterySize; }; #endif
#include "battery.h" Battery::Battery(int marker) :batterySize(marker) { } int Battery::showbattery() { return batterySize; }
#ifndef CAR_H #define CAR_H #include <string> #include <iostream> using namespace std; class Car { public: Car(string marker, string model, int year, int odometer = 0); friend ostream&operator<<(ostream &out, Car &year); void updateOdometer(int marker); string maker, model; int year, odometer; }; #endif
#include <iostream> using namespace std; #include <string> #include "Car.h" Car::Car(string marker, string model, int year, int odometer) :maker(marker), model(model), year(year), odometer(odometer) { } ostream &operator<<(ostream &out, Car &year) { out << "maker:" << year.maker << endl << "model:" << year.model << endl << "year:" << year.year << endl << "odometer:" << year.odometer << endl; return out; } void Car::updateOdometer(int marker) { if (marker < odometer) cout << "the new odometer is wrong!" << endl; else odometer = marker; odometer = marker; }
#ifndef ELECTRICCAR_H #define ELECTRICCAR_H #include "battery.h" #include "car.h" class ElectricCar :public Car, public Battery { public: ElectricCar(string marker, string model, int year, int odometer = 0, int Batterysize = 70); friend ostream&operator<<(ostream &out, ElectricCar &year); private: Battery battery; }; #endif
#include "ElectricCar.h" #include "Car.h" #include "Battery.h" #include <iostream> using namespace std; ElectricCar::ElectricCar(string marker, string model, int year, int odometer,int Batterysize) :Car(marker,model,year,odometer), Battery(Batterysize) { } ostream &operator<<(ostream &out, ElectricCar &year) { out << "maker:" << year.maker << endl << "model:" << year.model << endl << "year:" << year.year << endl << "odometer:" << year.odometer << endl << "batterysize:" << year.batterySize << "-kwh" << endl; return out; }
#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.重载运算符
#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]; }
#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 <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.https://www.cnblogs.com/lovecpp/p/10854382.html
2.https://www.cnblogs.com/DADABu-21/p/10864980.html
3.https://www.cnblogs.com/sqcmxg/p/10862028.html
浙公网安备 33010602011771号