C上机实验报告实验六_精品文档Word格式.doc

上传人:b****2 文档编号:13874827 上传时间:2022-10-14 格式:DOC 页数:9 大小:132.50KB
下载 相关 举报
C上机实验报告实验六_精品文档Word格式.doc_第1页
第1页 / 共9页
C上机实验报告实验六_精品文档Word格式.doc_第2页
第2页 / 共9页
C上机实验报告实验六_精品文档Word格式.doc_第3页
第3页 / 共9页
C上机实验报告实验六_精品文档Word格式.doc_第4页
第4页 / 共9页
C上机实验报告实验六_精品文档Word格式.doc_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

C上机实验报告实验六_精品文档Word格式.doc

《C上机实验报告实验六_精品文档Word格式.doc》由会员分享,可在线阅读,更多相关《C上机实验报告实验六_精品文档Word格式.doc(9页珍藏版)》请在冰豆网上搜索。

C上机实验报告实验六_精品文档Word格式.doc

程序名:

1ab8_1.Cpp

#include<

iostream>

usingnamespacestd;

classPoint

{

public:

Point();

Point(intx,inty);

~Point(){}//Point类析构函数

Point&

operator++();

//公有成员函数

Pointoperator++(int);

operator--();

Pointoperator--(int);

voidShow();

private:

int_x;

//私有数据成员

int_y;

};

Point:

:

Point(){//Point类构造函数

_x=0;

_y=0;

}

Point(intx,inty)//Point类构造函数

_x=x;

_y=y;

Point&

Point:

operator++()//重载后置++运算符为Point类成员函数

_x++;

_y++;

PointPoint:

operator++(int)//重载前置++运算符为Point类成员函数

Pointold=*this;

++(this->

_x);

_y);

returnold;

operator--()//重载后置--运算符为Point类成员函数

_x--;

_y--;

operator--(int)//重载前置--运算符为Point类成员函数

--(this->

voidPoint:

Show()//输出Point的坐标值

{

cout<

<

_x<

"

"

_y<

)"

endl;

intmain()

Pointa(2,3);

//定义一个Point类对象a

Pointb=a++;

//定义Point类对象b并用a++初始化b

Pointc=++a;

//定义Point类对象c并用++a初始化c

Pointd=--a;

//定义Point类对象d并用--a初始化d

Pointe=a--;

//定义Point类对象e并用a--初始化e

cout<

Pointa("

;

a.Show();

//输出a的坐标

Pointb("

b.Show();

//输出b的坐标

Pointc("

c.Show();

//输出c的坐标

Pointd("

d.Show();

//输出d的坐标

Pointe("

e.Show();

//输出e的坐标

return0;

运行结果:

2编写程序定义一个车(vehicle)基类,有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。

在main()函数中定义vehicle、bicycle、motorcar、motorcycle的对象,调用其Run()、Stop()函数,观察其执行情况。

再分别用vehicle类型的指针来调用这几个对象的成员函数,看看能否成功;

把Run、Stop定义为虚函数,再试试看。

lab8_2.cpp

classvehicle//基类vehicle

vehicle(){}

~vehicle(){}

virtualvoidRun(){cout<

Thevehicleisrunning!

}//定义虚函数Run()

virtualvoidStop(){cout<

Thevehiclehasstopped!

}//定义虚函数Stop()

classbicycle:

virtualpublicvehicle//定义派生类bicycle,声明基类为派生类的虚基类

voidRun(){cout<

Thebicycleisrunning!

voidStop(){cout<

Thebicyclehasstopped!

bicycle(){}

~bicycle(){}

classmotorcar:

virtualpublicvehicle//定义派生类motorcar,声明基类为派生类的虚基类

Themotorcarisrunning!

Themotorcarhasstopped!

motorcar(){}

~motorcar(){}

classmotorcycle:

publicbicycle,publicmotorcar//以bicycle类和motorcar类作为基类派生新类motorcycle

Themotorcycleisrunning!

Themotorcyclehasstopped!

vehiclea;

//定义vehicle类的一个对象a

vehicle*p;

//定义一个vehicle类的指针

bicycleb;

//定义bicycle类的对象b

motorcarc;

//定义motorcar类的对象c

motorcycled;

//定义motorcycle类的对象d

a.Run();

a.Stop();

b.Run();

b.Stop();

c.Run();

c.Stop();

d.Run();

d.Stop();

p=&

a;

//使指针p指向vehicle类对象a

p->

Run();

//通过指针调用vehicle类成员函数

Stop();

b;

//使指针p指向bicycle类对象b

//通过指针调用bicycle类成员函数

c;

//使指针p指向motorcar类对象c

//通过指针调用motorcar类成员函数

d;

//使指针指向motorbicycle类对象d

//通过指针调用motorcycle类成员函数

源程序:

string>

classBirthday{

Birthday(intYear,intMonth,intDay);

//构造函数

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 求职职场 > 笔试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1