Part 1. Car
车辆基本信息管理 问题场景描述如下: 为了对车量基本信息进行管理,对现实世界车量基本信息抽象后,抽象出Car类、ElectricCar类、Battery类, 它们之间的关系描述如下:基于Car类派生出ElectricCar类,派生类ElectricCar中新增数据成员为Battery类对象。
程序源码:
1 #ifndef CAR_H 2 #define CAR_H 3 #include <iostream> 4 #include <string> 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 #include <iostream> 3 #include <string> 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:"<<c.maker<<endl; 11 out<<"model:"<<c.model<<endl; 12 out<<"year:"<<c.year<<endl; 13 out<<"odometer:"<<c.odometer<<endl; 14 return out; 15 } 16 17 int Car::updateOdometer(int newodometer){ 18 if(newodometer<odometer) 19 cout<<"error!"<<endl; 20 else 21 odometer=newodometer; 22 return odometer; 23 }
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; 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; 8 }
1 #ifndef ELECTRICAR_H 2 #define ELECTRICAR_H 3 #include "battery.h" 4 #include "car.h" 5 #include <string> 6 #include <iostream> 7 using namespace std; 8 class Battery; 9 10 11 class ElectricCar:public 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; 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(); 7 } 8 9 ostream & operator<< (ostream &out,ElectricCar &c1){ 10 out<<"maker:"<<c1.getmaker()<<endl; 11 out<<"model:"<<c1.getmodel()<<endl; 12 out<<"year:"<<c1.getyear()<<endl; 13 out<<"odometer:"<<c1.getodometer()<<endl; 14 out<<"batterySize:"<<c1.batterysize<<"-kWh"<<endl; 15 return out; 16 }
1 #include <iostream> 2 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 #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 int& ArrayInt::operator[](int position){ 30 return p[position]; 31 }
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 }
运行截图:
评论链接:
https://www.cnblogs.com/DADABu-21/p/10732017.html#4260437
https://www.cnblogs.com/lszz/p/10727553.html#4260428
https://www.cnblogs.com/Yyaoyyy/p/10751510.html#4260404