实验四:类的继承派生和多态
【实验结论】
#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 BATTERY_H #define BATTERY_H #include<iostream> using namespace std; class Battery{ public: Battery(int batterySize0=70); int x(); private: int batterySize; }; #endif
#include"Battery.h" #include<iostream> using namespace std; Battery::Battery(int batterySize0):batterySize(batterySize0){ } int Battery::x(){ return batterySize; }
#ifndef CAR_H #define CAR_H #include<iostream> using namespace std; class Car{ public: Car(string maker0="",string model0="",int year0=2000,int odometer0=0); friend ostream &operator<<(ostream& out,const Car &c); int updateOdometer(int newdodmeter); private: string maker; string model; int year; int odometer; }; #endif
#include"Car.h" #include"Battery.h" #include"ElectricCar.h" #include<iostream> using namespace std; Car::Car(string maker0,string model0,int year0,int odometer0):maker(maker0),model(model0),year(year0),odometer(odometer0){ } ostream &operator<<(ostream& out,const 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 newodometer){ if(newodometer>odometer){ odometer=newodometer; return odometer; } else cout<<"newodometer is error"<<endl; }
#ifndef ELECTRICCAR_H #define ELECTRICCAR_H #include<iostream> #include"Car.h" #include"Battery.h" using namespace std; class ElectricCar:public Car{ public: ElectricCar(string maker0="",string model0="",int year0=2000,int odometer0=0); friend ostream &operator<<(ostream &out,const ElectricCar &m); private: Battery battery; int batterySize; }; #endif
include"Car.h" #include"Battery.h" #include"ElectricCar.h" #include<iostream> using namespace std; ElectricCar::ElectricCar(string maker0,string model0,int year0,int odometer0):Car(maker0,model0,year0,odometer0){ batterySize=battery.x(); } ostream &operator<<(ostream &out,const ElectricCar &m){ out<<Car(m) <<"batterySize"<<":"<<m.batterySize<<"-kWh"<<endl; return out; }
#2.重载运算符[ ]
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include "arrayInt.h" 3 using namespace std; 4 int main() { 5 // 定义动态整型数组对象a,包含2个元素,初始值为0 6 ArrayInt a(2); 7 a.print(); 8 9 // 定义动态整型数组对象b,包含3个元素,初始值为6 10 ArrayInt b(3, 6); 11 b.print(); 12 13 // 通过对象名和下标方式访问并修改对象元素 14 b[0] = 2; 15 cout << b[0] << endl; 16 b.print(); 17 18 system("pause"); 19 20 return 0; 21 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "arrayInt.h" 2 #include <iostream> 3 #include <cstdlib> 4 using std::cout; 5 using std::endl; 6 7 ArrayInt::ArrayInt(int n, int value): size(n) { 8 p = new int[size]; 9 10 if (p == 0) { 11 cout << "fail to mallocate memory" << endl; 12 exit(0); 13 } 14 15 for(int i=0; i<size; i++) 16 p[i] = value; 17 } 18 19 ArrayInt::~ArrayInt() { 20 delete[] p; 21 } 22 23 void ArrayInt::print() { 24 for(int i=0; i<size; i++) 25 cout << p[i] << " "; 26 cout << endl; 27 } 28 29 int &ArrayInt::operator[](int n){ 30 return p[n]; 31 32 }// 补足:将运算符[]重载为成员函数的实现
实验总结:
1.第二个程序很简单,基本上完全可以参照书上的自增自减,结合一下老师上课讲的加减,甚至比这些都简单。运算符重载为成员函数一般语法形式为:返回值类型 operator 运算符(形参表){函数体}。运算符重载为非成员函数一般语法形式为:返回值类型 operator 运算符(形参表){函数体}。有些加减运算要根据程序具体要求进行改进,而不是直接拿来使用,例如时钟这类程序,时分秒皆有其变化的范围。
2.第一个程序综合了很多知识点,类似于继承派生,以及运算符重载,此程序中运算符重载运用了友元函数来实现,友元函数属于非成员函数,一般用友元函数输出较为特别,使用“out”,并且返回值为“out"。当一个函数继承另一个函数时,也可以继承其友元函数的运行过程,并且只继承其public。此程序中的electriccar与battery是组合关系,electriccar中的battery即来自battery类。
评论地址:
https://www.cnblogs.com/yidaoyigexiaopenyou/p/10902587.html
https://www.cnblogs.com/lyc1103/p/10890495.html
https://www.cnblogs.com/charlotte00/p/10889286.html