实验四
1.car
#ifndef BATTERY_H #define BATTERY_H class Battery { public: Battery(int batterySize0= 70); int showbattery(); int batterySize; }; #endif
1 #ifndef CAR_H 2 #define CAR_H 3 #include <string> 4 #include <iostream> 5 using namespace std; 6 class Car { 7 public: 8 Car(string m, string t, int y, int d = 0); 9 friend ostream& operator<<(ostream& out, Car& a); 10 void updateOdometer(int x); 11 string maker; 12 string model; 13 int year; 14 int odometer; 15 16 }; 17 #endif
1 #ifndef ELECTRICCAR_H 2 #define ELECTRICCAR_H 3 #include "battery.h" 4 #include "car.h" 5 class ElectricCar :public Car, public Battery { 6 public: 7 ElectricCar(string m, string t, int y, int d = 0, int c = 70); 8 friend ostream& operator<<(ostream& out, ElectricCar& a); 9 private: 10 Battery battery; 11 12 }; 13 #endif
1 #include "battery.h" 2 Battery::Battery(int batterySize0) :batterySize(batterySize0) 3 { 4 }; 5 int Battery::showbattery() { 6 return batterySize; 7 }
1 #include <iostream> 2 using namespace std; 3 #include <string> 4 #include "car.h" 5 Car::Car(string m, string t, int y, int d) :maker(m), model(t), year(y), odometer(d) { 6 7 } 8 ostream& operator<<(ostream& out, Car& a) { 9 out << "maker:" << a.maker << endl 10 << "model:" << a.model << endl 11 << "year:" << a.year << endl 12 << "odometer:" << a.odometer << endl; 13 return out; 14 } 15 void Car::updateOdometer(int x) { 16 if (x < odometer) 17 cout << "please try again!" << endl; 18 else 19 odometer = x; 20 21 }
1 #include "battery.h" 2 #include <iostream> 3 using namespace std; 4 ElectricCar::ElectricCar(string m, string t, int y, int d, int c) :Car(m, t, y, d), Battery(c) { 5 6 } 7 ostream& operator<<(ostream& out, ElectricCar& a) { 8 out << "maker:" << a.maker << endl 9 << "model:" << a.model << endl 10 << "year:" << a.year << endl 11 << "odometer:" << a.odometer << endl 12 << "batterysize:" << a.batterySize << "-kwh" << endl; 13 return out; 14 }
1 #include <iostream> 2 using namespace std; 3 4 #include "car.h" 5 #include "electriCcar.h" 6 7 int main() { 8 // 测试Car类 9 Car oldcar("Audi","a4",2016); 10 cout << "--------oldcar's info--------" << endl; 11 oldcar.updateOdometer(25000); 12 cout << oldcar << endl; 13 14 // 测试ElectricCar类 15 ElectricCar newcar("Tesla","model s",2016); 16 newcar.updateOdometer(2500); 17 cout << "\n--------newcar's info--------\n"; 18 cout << newcar << endl; 19 20 system("pause"); 21 22 return 0; 23 }

2.Array
1 #include <iostream> 2 using namespace std; 3 4 #include "arrayInt.h" 5 6 int main() { 7 // 定义动态整型数组对象a,包含2个元素,初始值为0 8 ArrayInt a(2); 9 a.print(); 10 11 // 定义动态整型数组对象b,包含3个元素,初始值为6 12 ArrayInt b(3, 6); 13 b.print(); 14 15 // 通过对象名和下标方式访问并修改对象元素 16 b[0] = 2; 17 cout << b[0] << endl; 18 b.print(); 19 20 system("pause"); 21 22 return 0; 23 }
1 #include "arrayInt.h" 2 #include <iostream> 3 #include <cstdlib> 4 using std::cout; 5 using std::endl; 6 7 ArrayInt::ArrayInt(int n, int value): size(n) { 8 p = new int[size]; 9 10 if (p == nullptr) { 11 cout << "fail to mallocate memory" << endl; 12 exit(0); 13 } 14 15 for(int i=0; i<size; i++) 16 p[i] = value; 17 } 18 19 ArrayInt::~ArrayInt() { 20 delete[] p; 21 } 22 23 void ArrayInt::print() { 24 for(int i=0; i<size; i++) 25 cout << p[i] << " "; 26 cout << endl; 27 } 28 29 // 补足:将运算符[]重载为成员函数的实现 30 // ××× 31 int& ArrayInt::operator[](int x) 32 { 33 return p[x]; 34 }
1 #ifndef ARRAY_INT_H 2 #define ARRAY_INT_H 3 4 class ArrayInt { 5 public: 6 ArrayInt(int n, int value = 0); 7 ~ArrayInt(); 8 int& operator[](int x);// 补足:将运算符[]重载为成员函数的声明 9 // ××× 10 void print(); 11 private: 12 int *p; 13 int size; 14 }; 15 16 #endif

https://www.cnblogs.com/shenqidetao/p/10850282.html

浙公网安备 33010602011771号