1.
#ifndef BATTERY_H #define BATTERY_H class Battery { public: Battery(int batterySize0 = 70); int showBattery(); private: int batterySize; }; #endif
#include "battery.h" Battery::Battery(int batterysize0):batterySize(batterysize0){ }; int Battery::showBattery() { return batterySize; }
#ifndef CAR_H #define CAR_H #include <string> #include <iostream> using namespace std; class Car { public: Car(string a, string b, int y,int o=0); friend ostream&operator<<(ostream &out, Car &c); int updateOdometer(int x); string showmaker(); string showmodel(); int showyear(); int showodometer(); private: string maker; string model; int year; int odometer; }; #endif
#include "car.h" #include <iostream> #include <string> using namespace std; Car::Car(string a, string b, int y,int o):maker(a),model(b),year(y),odometer(o) { }; string Car::showmaker() { return maker; } string Car::showmodel() { return model; } int Car::showyear() { return year; } int Car::showodometer() { return odometer; } ostream &operator<<(ostream &out, Car &c) { out << "maker:" << c.maker << endl << "model:" << c.model << endl << "year:" << c.year << endl << "odometer:" << c.odometer << endl; return out; } int Car::updateOdometer(int x) { if (x < odometer) cout << "the new odometer is wrong" << endl; else odometer = x; return odometer; }
#ifndef ELECTRICAR_H #define ELECTRICAR_H #include"battery.h" #include"car.h" class ElectricCar :public Car,public Battery{ public: ElectricCar(string a, string b, int y, int o=0); friend std::ostream & operator<<(std::ostream &out, ElectricCar &c); private: int battery; }; #endif
#include <iostream> #include "car.h" #include "battery.h" #include "electricCar.h" using namespace std; ElectricCar::ElectricCar(string a, string b, int y, int o) :Car(a, b, y, o) { } std::ostream &operator<<(std::ostream &out, ElectricCar &c) { out << "maker:" << c.showmaker() << endl << "model:" << c.showmodel() << endl << "year:" << c.showyear() << endl << "odometer:" << c.showodometer() << endl << "batterysize:" << c.showBattery() << "-kwh" << endl; return out; }
#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 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 == nullptr) { 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; }
总结:对类的继承和派生还是有点生疏
不怎么清楚operotar前为什么一定要加&