第四次实验
1.类的继承和派生
#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 CAR_H #define CAR_H #include<iostream> #include<string> using namespace std; class Car { public: Car(string a=" ",string b=" ",int c=0,const int d=0):marker(a),model(b),year(c),odometer(d){} void updateOdometer(int t) { if (odometer > t) cout << "更新数值有误" << endl; else odometer = t; } friend ostream & operator<<(ostream &out,Car &c1); string getm() { return marker; } string getmo() { return model; } int gety() { return year; } int geto() { return odometer; } void geto1(int t) { odometer = t; } private: string marker; string model; int year; int odometer; }; ostream & operator<<(ostream &out,Car &c1){ cout << "marker:" << c1.marker << endl; cout << "model:" << c1.model << endl; cout << "year:" << c1.year << endl; cout << "odometer:" << c1.odometer << endl; return out; } #endif
#ifndef ELECTRICCAR_H #define ELECTRICCAR_H #include<iostream> #include"battery.h" using namespace std; class ElectricCar:public Car{ public: ElectricCar(string a,string b,int c, int d=70,const int e=0):Car(a, b, c, e) { battery = d; } friend ostream & operator<<(ostream &out, ElectricCar &c1); void updateOdometer(int t) { if (geto() > t) cout << "更新数值有误" << endl; else geto1(t); } private: Battery battery; }; ostream & operator<<(ostream &out, ElectricCar &c1) { cout << "marker:" << c1.getm()<< endl; cout << "model:" << c1.getmo() << endl; cout << "year:" << c1.gety() << endl; cout << "odometer:" << c1.geto() << endl; cout << "batterySize:" << c1.battery.show()<< "-kWh"<<endl; return out; } #endif
#ifndef BATTERY_H #define BATTERY_H class Battery { public: Battery(int a=70):batterySize(a){} int show() { return batterySize; } private: int batterySize; }; #endif
2. 运算符重载
#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; }
#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 n) { return p[n]; }
#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
实验总结与体会:
1.通过这次实验,我理解了类的继承和派生机制,掌握派生类的定义和使用。
2.在这次实验中遇到了一些问题,一个是派生类构造函数的语法形式用错了,另一个是[]运算符重载时b[0]=2一直跳出表达式必须是可修改的左值,卡了许久,最后用了引用才搞定。