实验四
第一题
//battery.h
#ifndef BATTERY_H #define BATTERY_H class Battery { public: Battery(int batterysize0 = 70); int showbattery()const{ return batterysize; } private: int batterysize; }; #endif
//battery.cpp
#include"battery.h" #include<iostream> using namespace std; Battery::Battery(int batterysize0):batterysize(batterysize0){}
//car.h
#ifndef CAR_H #define CAR_H #include<iostream> using namespace std; class Car { public: Car(string maker0, string model0, int year0 = 0, const int odometer0 = 0) :maker(maker0), model(model0), year(year0), odometer(odometer0) {} friend ostream &operator <<(ostream &out,Car &a); void updateodometer(int t)
{ if (odometer > t) cout << "wrong" << endl; else odometer = t; } string getmaker()
{ return maker; } string getmodel() { return model; } int getyear() { return year; } int getodo() { return odometer; } void getodo1(int n) { odometer = n; } private: string maker; string model; int year; int odometer; }; #endif
//car.cpp
#include"car.h" #include<iostream> #include<string> using namespace std; ostream & operator<<(ostream &out,Car & a) { out << "maker:" << a.getmaker() << endl; //或者是直接输出a.maker out << "model:" << a.getmodel() << endl; //同上,直接输出a.model out << "year:" << a.getyear() << endl; //同上 out << "odometer:" << a.getodo() << endl; //同上 return out; }
//electriccar.h
#ifndef ELECTRICCAR_H #define ELECTRICCAR_H #include"battery.h" #include"car.h" class ElectricCar :public Car,public Battery { public: ElectricCar(string a, string b, int c, int e = 70 ,const int d = 0):Car(a,b,c,d),battery(e){} friend ostream &operator<<(ostream &out, ElectricCar & a); void updateodometer(int m) { if (getodo() > m) cout << "wrong" << endl; else getodo1(m); } private: Battery battery; }; #endif
//electriccar.cpp
#include"electriccar.h" #include<iostream> #include<string> using namespace std; ostream &operator<<(ostream &out, ElectricCar & a) { out << "maker:" << a.getmaker() << endl; out << "model:" << a.getmodel() << endl; out << "year:" << a.getyear() << endl; out << "odometer:" << a.getodo() << endl; out << "batterysize:" << a.battery.showbattery() << "-kWh" << endl; return out; }
//main.cpp
#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; }
运行结果如图

第二题
//arrayInt.h
#ifndef ARRAY_INT_H #define ARRAY_INT_H class ArrayInt{ public: ArrayInt(int n, int value=0); ~ArrayInt(); // 补足:将运算符[]重载为成员函数的声明 int & operator[](int y); void print(); private: int *p; int size; }; #endif
//arrayInyt.cpp
#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 y) { return p[y]; }
//main.cpp
#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.battery.h与battery.cpp没有什么难点,注意Battery类的构造函数有默认参数值。
2.car.h中对于运算符“<<”的重载函数的声明定义为友元函数,不是类的成员函数,写法可以模仿书上的例题,在car.h的前几行还要#include<iostream>,否则ostrem就是一个未定义的标志符。
3.在car.cpp中对重载函数定义的时候注意不用再添上friend了,它不允许定义在类外,输出car的四个私有成员时,以输出maker为例,可以写“out<<a.getmaker()<<endl;”也可以是“out<<a.maker<<endl;”友元函数虽不是类的成员函数,但它是可以通过对象来访问类的私有成员的,所以这里既可以访问对象的public成员函数,也可以直接访问私有成员。还要注意#include<string>,否则会导致maker和model的类型未定义。
4.在electriccar.h中,一开始我只把Car类作为ElectricCar的基类,没有想到还要把Battery也作为ElectricCar的基类,导致在这里卡了挺长时间的,定义<<的重载函数的时候,电车类的对象一直访问不了它自己的私有成员battery,后来把这里改了还是发现访问不了,最后求助同学,俩人找了二十多分钟愣是没找到哪里错了,最后终于发现,重载函数声明和定义的时候,形参中电车类对象我一个定义成const,一个没定义成const,就导致参数没办法传递了,改掉后就可以使用a.battery.showbattery()啦。
关于第二题
1.第二题周日写的挺快的,形参是一个整形,以这个整形作为动态数组p[]的下标返回,这样就可以通过下标来访问动态数组元素了。
2.[]必须要声明为类的成员函数,这样第一个操作数就是类的对象了

浙公网安备 33010602011771号