实验四
1.car
#ifndef BATTERY_H #define BATTERY_H class Battery{ public: Battery(int batterySize0=70); int show(); int batterySize; }; #endif
#include "battery.h" Battery::Battery(int batterySize0){ batterySize=batterySize0; } int Battery::show(){ return batterySize; }
#ifndef CAR_H #define CAR_H #include <string> #include <iostream> using namespace std; class Car{ public: Car(string maker0,string model0,int year0,int odometer0=0):maker(maker0),model(model0),year(year0),odometer(odometer0){}; friend ostream& operator<<(ostream &out,const Car &c); void updateOdometer(int newodometer); string maker,model; int year,odometer; }; #endif
#include "car.h" #include <string> #include <iostream> using namespace std; ostream& operator<<(ostream &out,const Car &c){ out<<"maker:"<<c.maker<<endl<<"model:"<<c.model<<endl<<"year:"<<c.year<<endl<<"odometer:"<<c.odometer<<endl; return out; } void Car::updateOdometer(int newodometer){ if(newodometer<odometer) cout<<"Error updating"<<endl; else odometer=newodometer; }
#ifndef ELECTRICCAR_H #define ELECTRICCAR_H #include "battery.h" #include "car.h" #include <string> #include <iostream> class ElectricCar:public Car,public Battery{ public: ElectricCar(string maker1,string model1,int year1,int odometer1=0,int batterySize1=70):Car(maker1,model1,year1,odometer1),Battery(batterySize1){}; friend ostream& operator<<(ostream &outCar,const ElectricCar &e); void updateOdometer(int newodometer1); }; #endif
#include "electriccar.h" #include "car.h" #include "battery.h" #include <iostream> using namespace std; ostream& operator<<(ostream &out,const ElectricCar &e){ out << "maker:" << e.maker << endl << "model:" << e.model << endl << "year:" << e.year << endl << "odometer:" << e.odometer << endl << "batterySize:" << e.batterySize <<"-kWh" <<endl; return out; } void ElectricCar::updateOdometer(int newodometer1){ if(newodometer1<odometer) cout<<"Error updating"<<endl; else odometer=newodometer1; }
#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.array
#ifndef ARRAY_INT_H #define ARRAY_INT_H class ArrayInt{ public: ArrayInt(int n, int value=0); ~ArrayInt(); // 补足:将运算符[]重载为成员函数的声明 // ××× int & operator[](int a); void print(); private: int *p; int size; }; #endif
#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 == NULL) { 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 a){ return p[a]; }
#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; }
第一题对于运算符“<<”的重载是按照书上的模仿写出来的,自己上手写还是有点困难。在car类声明中我将制造商,型号,生产年份和行车里程数声明为私有成员时,派生类还是不能访问,设为公有成员后程序编译是没问题,但不知道会不会带来问题。
第二题和上课老师讲的自增类似,运算符重载分为两类:重载为成员函数/非成员函数,需要注意的就是重载为成员函数时,函数的参数个数比原来的操作数个数少一个(后置++,--除外),而重载为非成员函数时,参数个数与原操作个数相同。
https://www.cnblogs.com/lovecpp/p/10854382.html
https://www.cnblogs.com/qiuqiuwr/p/10883165.html
https://www.cnblogs.com/zuiyankh/p/10884276.html