标签:ctr ring array exit date hide friend for end
project1:
#ifndef BATTERY_H #define BATTERY_H class Battery { public: Battery(int batterySize0 = 70); Battery(const Battery& b0); int getbattery(); private: int batterySize; }; #endif // !BATTERY_H
#ifndef CAR_H #define CAR_H #include<iostream> using namespace std; class Car { public: Car(string maker0, string model0, int year0, int odometer0=0); Car(const Car &car0); friend ostream&operator<<(ostream &out,const Car &car0); void updateOdometer(int odmt); private: string maker; string model; int year; int odometer; }; #endif // !CAR_H
#ifndef ELECTRICCAR_H #define ELECTRICCAR_H #include"car.h" #include"battery.h" class ElectricCar :public Car { public: ElectricCar(string maker0, string model0, int year0,Battery battery0=70, int odometer0 = 0); friend ostream&operator<<(ostream &out, const ElectricCar &car0); Battery battery; }; #endif // ELECTRICCAR_H
#include "battery.h" Battery::Battery(int batterySize0):batterySize(batterySize0) { } Battery::Battery(const Battery & b0):batterySize(b0.batterySize) { } int Battery::getbattery() { return batterySize; }
#include"car.h" #include<iostream> #include<string> using namespace std; Car::Car(string maker0, string model0, int year0, int odometer0):maker(maker0),model(model0),year(year0),odometer(odometer0) { } Car::Car(const Car & car0): maker(car0.maker), model(car0.model), year(car0.year), odometer(car0.odometer) { } void Car::updateOdometer(int odmt) { odometer = odmt; } ostream & operator<<(ostream & out, const Car & car0) { out << "maker:" << car0.maker << endl << "model:" << car0.model << endl << "year:" << car0.year << endl << "odometer:" << car0.odometer; return out; // TODO: 在此处插入 return 语句 }
#include "electricCar.h" using namespace std; #include<iostream> ElectricCar::ElectricCar(string maker0, string model0, int year0, Battery battery0, int odometer0):Car(maker0,model0,year0,odometer0),battery(battery0) { } ostream & operator<<(ostream & out, const ElectricCar & car0) { Car car1(car0); Battery b1(car0.battery); int s = b1.getbattery(); out << car1 << endl; out <<"Battery:"<< s; 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; }
project2:
#ifndef ARRAY_INT_H #define ARRAY_INT_H class ArrayInt{ public: ArrayInt(int n, int value=0); ~ArrayInt(); int &operator[](int n); // 补足:将运算符[]重载为成员函数的声明 // ××× 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; } int &ArrayInt::operator[](int n) { return p[n]; } void ArrayInt::print() { for(int i=0; i<size; i++) cout << p[i] << " "; cout << endl; } // 补足:将运算符[]重载为成员函数的实现 // ×××
#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; }
标签:ctr ring array exit date hide friend for end
原文地址:https://www.cnblogs.com/Yyaoyyy/p/10887670.html