四川师范大学C++实验报告(含截图)(6)

2019-06-17 10:50

{

public: DerivedClass(int a,int b):BaseClass1(a),number1(b) {} ~DerivedClass(){cout<<\析构函数调用!!!\private: BaseClass number; BaseClass1 number1; };

int main() { DerivedClass d(1,2); return 0; }

19) 定义一个车(vehicle)基类,具有MaxSpeed、Weight等成员变量,Run、Stop等成员函数,由此派生出自行车(bicycle)类,汽车(motorcar)类。自行车(bicycle)类有高度(Height)等属性,汽车(motorcycle)类有座位数(SeatNum)等属性。从bicycle和motorcycle派生出摩托车(Motorcar)类,在继承过程中,注意把vehicle设置为虚基类。如果不把vehicle 设置为虚基类,会有什么问?编程实验及分析原因。

#include using namespace std; class vehicle {

public: vehicle(int m,int w):MaxSpeed(m),Weight(w){cout<<\构造函数调用!!!\ void Run(){cout<<\车 开始以最大速度开始行驶\

注:实验成绩分为(90——100分)优,(80——89分)良,(70——79)中,(60——69分)及格,(59分)不及格。

void Stop(){cout<<\车 停止 \ ~vehicle(){cout<<\析构函数调用!!!\ int MaxSpeed; int Weight; };

class bicycle:virtual public vehicle {

public: bicycle(int m,int w,int h):vehicle(m,w),Hight(h){cout<<\构造函数调用!!!\ void Run(){cout<<\自行车高度是\ void Stop(){cout<<\自行车停止 \ ~bicycle(){cout<<\析构函数调用!!!\ int Hight; };

class motorcar:virtual public vehicle {

public: motorcar(int m,int w,int s):vehicle(m,w),SeatNum(s){cout<<\构造函数调用!!!\ void Run(){cout<<\汽车座位数是\ void Stop(){cout<<\汽车停止 \ ~motorcar(){cout<<\析构函数调用!!!\ int SeatNum; };

class motorcycle:public bicycle,public motorcar {

public: motorcycle(int m,int w,int h,int s):vehicle(m,w),bicycle(m,w,h),motorcar(m,w,s){} void Run(){cout<<\摩托车的速度:\ 重量:\ 高度:\ 座位数为:\ void Stop(){cout<<\摩托车停止 \ ~motorcycle(){cout<<\析构函数调用!!!\};

int main() { vehicle v(2,4); v.Run(); v.Stop(); bicycle b(2,5,7); b.Run(); b.Stop(); motorcar mc(7,9,4); mc.Run();

注:实验成绩分为(90——100分)优,(80——89分)良,(70——79)中,(60——69分)及格,(59分)不及格。

}

mc.Stop();

motorcycle m(20,300,4,1); m.Run(); m.Stop(); return 0;

20) 思考并回答:继承,派生,子类对基类成员的访问权限,继承方式,继承时的构造函数和析构函数的调用顺序,虚基类

继承:从基类继承所有的属性(构造函数和析构函数除外) 派生:产生新类的过程

子类对基类成员的访问权限:不能访问私有成员。共有继承:不变;私有继承:共有和保护作为私有出现;保护继承:共有和保护作为保护出现 继承方式:共有、私有、保护继承

继承时的构造函数和析构函数的调用顺序:完全相反

注:实验成绩分为(90——100分)优,(80——89分)良,(70——79)中,(60——69分)及格,(59分)不及格。

构造:被继承时的声明顺序 -> 内嵌对象的定义顺序 析构:新增的类型的成员 -> 内嵌对象 -> 继承的

虚基类:解决二义性(菱形结构)问题的(同一基类继承多次而产生的二义性问题)

实验五 多态和运算符重载 ( 设计性实验 2学时)

1. 目的要求:

掌握运算符重载的方法;学习使用虚函数实现动态多态性。

2. 实验内容:

21) 定义Point类,有坐标x,y两个私有成员变量;对Point类重载“+”(相加)、“-”(相减)和“==”(相等)运算符,实现对坐标的改变,要求用友元函数和成员函数两种方法实现。对Point类重载<<运算符,以使得代码 Point p; cout<

#include using namespace std; class Point {

private: int x,y; public:

Point(int x,int y):x(x),y(y){

cout<<\构造函数调用---\endl; }

~Point(){cout<<\析构函数调用---\endl;} int getX() const {return x;} int getY() const {return y;}

//Point operator + (Point &point2); //Point operator - (Point &point2); //Point operator == (Point &point2); inline void Display(){

cout<<\x<<\y<<\endl;

注:实验成绩分为(90——100分)优,(80——89分)良,(70——79)中,(60——69分)及格,(59分)不及格。

}

friend Point operator + (Point &point1, Point &point2); friend Point operator - (Point &point1, Point &point2); friend Point operator == (Point &point1, Point &point2); friend ostream & operator << (ostream &out,Point &point); }; /*

Point Point:: operator + (Point &point2) {

Point temp(0,0);

temp.x = this->x + point2.x; temp.y = this->y + point2.y; return temp; }

Point Point:: operator - (Point &point2) {

Point temp(0,0);

temp.x = this->x - point2.x; temp.y = this->y - point2.y; return temp; }

Point Point:: operator == (Point &point2) {

if ((this->x == point2.x) && (this->y == point2.y)) {

return point2; }

else cout<<\ }*/

Point operator + (Point &point1, Point &point2){ Point temp(0,0);

temp.x = point1.x + point2.x; temp.y = point1.y + point2.y; return temp; }

Point operator - (Point &point1, Point &point2){ Point temp(0,0);

temp.x = point1.x - point2.x; temp.y = point1.y - point2.y; return temp; }

Point operator == (Point &point1, Point &point2){

if ((point1.x == point2.x) && (point1.y == point2.y)) {

注:实验成绩分为(90——100分)优,(80——89分)良,(70——79)中,(60——69分)及格,(59分)不及格。


四川师范大学C++实验报告(含截图)(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:番茄的桶式气雾栽培法 - 图文

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: