Part 1. Car
车辆基本信息管理 问题场景描述如下: 为了对车量基本信息进行管理,对现实世界车量基本信息抽象后,抽象出Car类、ElectricCar类、Battery类, 它们之间的关系描述如下:基于Car类派生出ElectricCar类,派生类ElectricCar中新增数据成员为Battery类对象。
程序源码:
1 #ifndef CAR_H 2 #define CAR_H 3 #include4 #include 5 using namespace std; 6 7 class Car{ 8 public: 9 Car(string maker0,string model0,int year0,int odometer0=0);10 friend ostream & operator<< (ostream &out,const Car &c); //重载运算符<<11 int updateOdometer(int newodometer);12 string getmaker(){ return maker;}13 string getmodel(){ return model;}14 int getyear(){ return year;}15 int getodometer(){ return odometer;}16 private:17 string maker;18 string model;19 int year;20 int odometer;21 };22 23 #endif
1 #include "car.h" 2 #include3 #include 4 using namespace std; 5 6 Car::Car(string maker0,string model0,int year0,int odometer0):maker(maker0),model(model0),year(year0),odometer(odometer0){ 7 } 8 9 ostream & operator<< (ostream &out,const Car &c){10 out<<" maker:"< <
1 #ifndef BATTERY_H 2 #define BATTERY_H 3 4 class Battery{ 5 public: 6 Battery(int batterySize0=70); 7 int getbatterySize(); 8 friend class ElectricCar; //将ElectricCar类设为友元 9 private:10 int batterySize;11 };12 13 #endif
1 #include "battery.h"2 3 Battery::Battery(int batterySize0):batterySize(batterySize0){4 }5 6 int Battery::getbatterySize(){7 return batterySize; //返回Battery类的batterySize值8 }
1 #ifndef ELECTRICAR_H 2 #define ELECTRICAR_H 3 #include "battery.h" 4 #include "car.h" 5 #include6 #include 7 using namespace std; 8 class Battery; 9 10 11 class ElectricCar:public Car{ //公有继承car类12 public:13 ElectricCar(string maker0,string model0,int year0,int odometer0=0,int battery0=70);14 friend ostream & operator<< (ostream &out,ElectricCar &c1); //使用友元重载二元运算符<<15 friend class Battery; //将Battery类设为友元16 private:17 Battery b;18 int batterysize;19 };20 21 #endif
1 #include "electricCar.h" 2 using namespace std; 3 4 ElectricCar::ElectricCar(string maker0,string model0,int year0,int odometer0,int battery0):Car(maker0,model0,year0,odometer0){ 5 b.batterySize=battery0; 6 batterysize=b.getbatterySize(); //将battery类里的batterysize的值赋给electricCar类里的batterysize 7 } 8 9 ostream & operator<< (ostream &out,ElectricCar &c1){10 out<<"maker:"<<
1 #include2 using namespace std; 3 4 #include "car.h" 5 #include "electricCar.h" 6 7 int main() { 8 Car oldcar("Audi","a4",2016); 9 cout << "--------oldcar's info--------" << endl;10 oldcar.updateOdometer(25000);11 cout << oldcar << endl;12 13 ElectricCar newcar("Tesla","model s",2016);14 newcar.updateOdometer(2500);15 cout << "\n--------newcar's info--------\n"; 16 cout << newcar << endl;17 18 system("pause");19 20 return 0;21 }
运行截图:
Part 2. Arraylnt
重载运算符[]为一维动态整形数组类ArrayInt的成员函数,使得通过动态整形数组对象名和下标可以访问对象中具体元素。
程序源码:
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 position); 9 void print();10 private:11 int *p;12 int size;13 };14 15 #endif
1 #include "arrayInt.h" 2 #include3 #include 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
1 #include2 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个元素,初始值为612 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.对类有了新的认识,巩固了C++区别于C的区别于string字符串的使用
2.初步掌握了运算符重载、友元类、类的继承时基类与派生类的访问权限与语法格式
3.运算符重载时有些运算符是特殊的,有一元运算符或二元运算符等等,如:
(1)不可以使用全局函数以及其对应的友元函数来重载的操作符有 =、 ()、 [ ]、 ->
(2)必须使用全局函数以及其对应的友元函数来重载的操作符为 <<
评论链接:
https://www.cnblogs.com/DADABu-21/p/10732017.html#4260437https://www.cnblogs.com/lszz/p/10727553.html#4260428
https://www.cnblogs.com/Yyaoyyy/p/10751510.html#4260404