1、
#ifndef CAR_H #define CAR_H #include <string> using std::string; class Car{ public: Car(string newma,string newmo,int newy,int newo=0); friend std::ostream &operator <<(std::ostream &out,Car &c); int updateOdometer(int newo);//更新里程数 string getMaker(); string getModel(); int getYear(); int getOdometer(); private: string maker; string model; int year; int odometer; }; #endif
#include "car.h" #include <iostream> #include <string> using std::cin; using std::cout; using std::endl; using std::string; Car::Car(string newma,string newmo,int newy,int newo):maker(newma),model(newmo),year(newy),odometer(newo){ }; string Car::getMaker(){ return maker; } string Car::getModel(){ return model; } int Car::getYear(){ return year; } int Car::getOdometer(){ return odometer; } int Car::updateOdometer(int newo){ if(odometer<=newo) odometer=newo; else { cout<<"wrong !"<<endl; cin>>odometer; } return odometer; } std :: ostream & operator << (std::ostream &out,Car &c){ out<<"maker: "<<c.maker<<endl <<"model: "<<c.model<<endl <<"year: "<<c.year<<endl <<"odometer: "<<c.odometer<<endl; return out; }
#ifndef ELECTRICCAR_H #define ELECTRICCAR_H #include "car.h" #include "battery.h" class ElectricCar:public Car,public Battery { public: ElectricCar(string newma,string newmo,int newy,int newo=0); friend std::ostream & operator<<(std::ostream &out,ElectricCar &c); }; #endif
#include "car.h" #include "battery.h" #include "electricCar.h" #include <iostream> using namespace std; ElectricCar::ElectricCar(string newma,string newmo,int newy,int newo):Car(newma,newmo,newy,newo){ } std::ostream & operator<<(std::ostream &out,ElectricCar &c){ out<<"maker: "<<c.getMaker()<<endl <<"model: "<<c.getModel()<<endl <<"year: "<<c.getYear()<<endl <<"odomter: "<<c.getOdometer()<<endl <<"batterySize: "<<c.getbattery()<<"-KWh"<<endl; return out; }
#ifndef BATTERY_H #define BATTERY_H class battery{ public: battery(int newb=70); int getbattery(); private: int batterySize; }; #endif
#include "battery.h" #include <iostream> using namespace std; battery::battery(int newb):batterySize(newb){ }; int battery::getbattery(){ return batterySize; }
#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、
#ifndef ARRAYINT_H #define ARRAYINT_H class ArrayInt{ public: ArrayInt(int n, int value=0); ~ArrayInt(); int & operator [](int k); 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 k){ return p[k]; }
#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; }
结果:
实验小结:
1、<<的重载不太熟练,反复看了书;
2、p==nullptr或者p==NULL或者!p都可以。