project1
程序
#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; }
#ifndef ELECTRICCAR_H #define ELECTRICCAR_H #include <iostream> #include "car.h" #include "battery.h" using namespace std; class ElectricCar: public Car,public Battery { public: ElectricCar(string maker0, string model0, int year0, int odometer0 = 0) :Car(maker0, model0, year0, odometer0) {}; friend ostream & operator<<(ostream &out,const ElectricCar &c); private: Battery battery; }; #endif
#include "electricCar.h" #include <cstring> #include <iostream> using namespace std; ostream & operator<<(ostream &out,const ElectricCar &c2){ out<<"maker:"<<c2.maker0<<endl <<"model:"<<c2.model0<<endl <<"year:"<<c2.year0<<endl <<"odometer:"<<c2.odometer0<<endl <<"batterySize:"<<c2.batterysize<<"-kWh"; return out; }
#ifndef CAR_H #define CAR_H #include <iostream> #include <cstring> using namespace std; class Car{ public: Car(string maker0,string model0,int year0,int odometer0=0 ); void updateOdometer(int odometer1); friend ostream &operator<<(ostream &out,const Car &c2); private: string maker; string model; int year; int odometer; }; #endif
#include <iostream> #include <cstring> #include "car.h" using namespace std; Car::Car(string maker0,string model0,int year0,int odometer0){ maker=maker0; model=model0; year=year0; odometer=odometer0; } void Car::updateOdometer(int odometer1){ if(odometer1<odometer) cout<<"更新数值有误"<<endl; else odometer=odometer1; } ostream & operator<<(ostream &out,const Car &c1) { out<<"maker:"<<c1.maker<<endl <<"model:"<<c1.model<<endl <<"year:"<<c1.year<<endl <<"odometer:"<<c1.odometer<<endl; return out; }
#include "battery.h" Battery::int showbatterysize:batterysize(70){ return batterysize; }
#ifndef BATTERY_H #define BATTERY_H class Battery { public: int showbatterysize; int showbattery()const; private: int batterysize; }; #endif
结果
project2
程序
#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; }
#ifndef ARRAY_INT_H #define ARRAY_INT_H class ArrayInt{ public: ArrayInt(int n, int value=0); ~ArrayInt(); ArrayInt& 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; } void ArrayInt::print() { for(int i=0; i<size; i++) cout << p[i] << " "; cout << endl; } ArrayInt& ArrayInt::operator[](int n){ return p[n]; }// 补足:将运算符[]重载为成员函数的实现 // ×××
结果
第一题不是很明白
没有写完,下次搞懂补上
原文:https://www.cnblogs.com/qiuxiuh/p/10903174.html