实验四
实验四
1. 车辆基本信息管理问:基于Car类派生出ElectricCar类,派生类ElectricCar中新增数据成员为Battery类对象。
1 #ifndef CAR_H 2 #define CAR_H 3 #include<iostream> 4 using namespace std; 5 6 class Car{ 7 public: 8 Car(string maker0=" ",string model0=" ",int year0=2000); 9 void updateOdometer(int newOdometer); 10 string Getmaker()const; 11 string Getmodel()const; 12 int Getyear()const; 13 int Getodometer()const; 14 friend ostream & operator<<(ostream &out,const Car &t); 15 private: 16 string maker; 17 string model; 18 int year; 19 int odometer; 20 }; 21 22 #endif
1 #include "car.h" 2 #include<iostream> 3 using namespace std; 4 5 Car::Car(string maker0,string model0,int year0){ 6 maker=maker0; 7 model=model0; 8 year=year0; 9 odometer=0; 10 } 11 12 string Car::Getmaker()const{ 13 return maker; 14 } 15 16 string Car::Getmodel()const{ 17 return model; 18 } 19 20 int Car::Getyear()const{ 21 return year; 22 } 23 24 int Car::Getodometer()const{ 25 return odometer; 26 } 27 28 void Car::updateOdometer(int newOdometer){ 29 if(odometer<odometer) 30 cout<<"Warning!Update data error."<<endl; 31 else 32 odometer=newOdometer; 33 } 34 35 ostream & operator<<(ostream &out,const Car &t){ 36 out<<"Maker:"<<t.maker<<endl<<"Model:"<<t.model<<endl<<"Year:"<<t.year<<endl<<"Odometer:"<<t.odometer<<endl; 37 return out; 38 }
1 #ifndef ELECTRIC_CAR_H 2 #define ELECTRIC_CAR_H 3 #include "battery.h" 4 #include "car.h" 5 6 class ElectricCar:public Car{ 7 public: 8 ElectricCar(string maker0=" ",string model0=" ",int year0=2000,int odometer0=0); 9 friend ostream & operator<<(ostream &out,const ElectricCar &m); 10 private: 11 Battery battery; 12 int batterysize; 13 }; 14 15 #endif
1 #include "electriccar.h" 2 #include<iostream> 3 #include "car.h" 4 #include "battery.h" 5 using namespace std; 6 7 ElectricCar::ElectricCar(string maker0,string model0,int year0,int odometer):Car(maker0,model0,year0){ 8 batterysize=battery.GetbatterySize(); 9 } 10 11 ostream & operator<<(ostream &out,const ElectricCar &m) 12 { 13 out<<"Maker:"<<m.Getmaker()<<endl 14 <<"Model:"<<m.Getmodel()<<endl 15 <<"Year:"<<m.Getyear()<<endl 16 <<"Odometer:"<<m.Getodometer()<<endl 17 <<"BatterSize:"<<m.batterysize<<"kWh"<<endl; 18 return out; 19 }
1 #ifndef BATTERY_H 2 #define BATTERY_H 3 4 class Battery{ 5 public: 6 Battery(int batterySize0=70); 7 int GetbatterySize(); 8 private: 9 int batterySize; 10 }; 11 12 #endif
1 #include "battery.h" 2 #include<iostream> 3 using namespace std; 4 5 Battery::Battery(int batterySize0){ 6 batterySize=batterySize0; 7 } 8 9 int Battery::GetbatterySize(){ 10 return batterySize; 11 }
1 #include <iostream> 2 #include<cstdlib> 3 using namespace std; 4 5 #include "car.h" 6 #include "electricCar.h" 7 8 int main() { 9 // 测试Car类 10 Car oldcar("Audi","a4",2016); 11 cout << "--------oldcar's info--------" << endl; 12 oldcar.updateOdometer(25000); 13 cout << oldcar << endl; 14 15 // 测试ElectricCar类 16 ElectricCar newcar("Tesla","model s",2016); 17 newcar.updateOdometer(2500); 18 cout << "\n--------newcar's info--------\n"; 19 cout << newcar << endl; 20 21 system("pause"); 22 23 return 0; 24 }
实验结果如下:
2.2. 补足程序,重载运算符[]为一维动态整形数组类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 // 将运算符[]重载为成员函数的声明 9 int& operator[](int x); 10 void print(); 11 private: 12 int *p; 13 int size; 14 }; 15 16 #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 == 0) { 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 int &ArrayInt::operator[](int x){ 31 return p[x]; 32 }
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 }
运行结果如下:
说一说实验中遇到的三个令我头大的点,一是程序头文件的包含与被包含,这个可以自己慢慢熟悉记忆,二是在vs2010环境下,对于派生出来的类中添加的重载运算符部分的定义不明确,在devc环境中自身优化后就没有这样的问题,这个仍然不明确原因,三是构造函数的初始化列表,看了几个博文,其中作小推荐:http://www.cnblogs.com/kaituorensheng/p/3477630.html,以及这位博客:https://www.cnblogs.com/kaituorensheng/category/465181.html
互评地址:
https://www.cnblogs.com/jyf13/
https://www.cnblogs.com/aitxy899/
https://www.cnblogs.com/mzy-1229