三、实验任务
1.定义一个基类Animal,有私有整形成员变量age,构造其派生类dog,在其成员函数SetAge(int n)中直接给age赋值,看看会有什么问题,把age改为公有成员变量,还会有问题吗?改为保护类型呢?改变继承方式呢?
2.通过在上例的构成函数、析构函数中添加提示信息,或用debug调试功能观察构造函数和析构函数的执行情况,并分析原因。
3.定义一个车(vehicle)基类,具有MaxSpeed、Weight等成员变量,Run、Stop等成员函数,由此派生出自行车(bicycl)类、汽车(motorcar)类。自行车类有高度(Height)等属性,汽车类有座位数(SeatNum)等属性。从和派生出摩托车(motorcycle)类,在继承过程中注意把设置为虚基类。如果不把vehice设置为虚基类,会有什么问题?并分析原因。 四、实验步骤
1.编写程序定义基类Animal,成员变量age定义为私有的。构造派生类dog,在其成员函数SetAge(int n)中直接对age赋值时,会出现错误提示。试着将age分别改为公有的、保护类型,再看看会有什么不同。试着在main函数中直接给age赋值,看看又会有什么问题?怎么才能避免这种问题的出现。深入认识三种访问控制的异同。示例程序Lab7_1.cpp代码如下:
#include
class Animal {
public: //可修改为private,protected int age; public:
Animal(){}; ~Animal(){}; };
class Dog : private Animal //可修改为public,protected { public:
35
Dog(){}; ~Dog(){};
void SetAge(int n){ age = n;} };
void main() {
Dog a;
a.SetAge(10); }
2.修改上例,以方便跟踪基类和派生类的构造函数、析构函数的执行情况。 3.按任务3要求编写程序代码。分别测试vehicle定义为虚基类与非虚基类的情况。用debug跟踪基类和派生类的构造函数、析构函数的执行情况。示例程序Lab7_2.cpp代码如下:
#include
private:
int MaxSpeed; int Weight; public:
vehicle(){MaxSpeed=0; Weight=0;}; ~vehicle(){};
void Run() {cout << \ void Stop() {cout << \};
class bicycle : virtual public vehicle {
private:
int Height; public:
bicycle(){}; ~bicycle(){}; };
class motorcar : virtual public vehicle {
private:
36
int SeatNum; public:
motorcar(){}; ~motorcar(){}; };
class motorcycle : public bicycle , public motorcar { public:
motorcycle (){}; ~motorcycle (){}; };
void main() {
vehicle a; bicycle b; motorcar c; motorcycle d; a.Run(); a.Stop(); b.Run(); b.Stop(); c.Run(); c.Stop(); d.Run(); d.Stop(); }
实验八 多态性
一、实验目的
1.掌握运算符重载的方法。 2.学习使用虚函数实现动态多态性。 二、实验课时
4学时 三、实验任务
1.定义Point类,有坐标x,y两个成员变量;对Point类重载+、-、++(前置)、++(后置)四种运算符,实现对坐标值的运行与改变。
37
2.修改程序lab7_2.cpp,给bicycle、motorcar、motorcycle这三个类均定义Run、Stop等成员函数,观察虚函数的作用。 四、实验步骤
1.编写程序定义Point类,在类中定义整形的私有成员变量x、y,定义成员函数 Point operator + (Point p2)重载“+”运算符,定义成员函数 Point operator - (Point p2)重载“-”运算符,定义成员函数Point operator++()重载“++(前置)”运算符,定义成员函数Point operator++(int)重载“++(后置)”运算符,实现对坐标值的改变。注意观察++(前置)与++(后置)运算的结果是否有区别,试着重载--(前置)与--(后置)。示例程序lab8_1.cpp代码如下:
#include
class Point {
private: int x, y; public:
Point(int xx=0,int yy=0) { x=xx; y=yy;} Point operator+(Point p2); Point operator-(Point p2); Point operator++();
Point operator++(int); int getx() { return x; } int gety() { return y; } };
Point Point::operator+(Point p2) {
Point p; p.x=x+p2.x; p.y=y+p2.y; return p; }
Point Point::operator-(Point p2) {
return Point(x-p2.x,y-p2.y); }
Point Point::operator++()
38
{
x++; y++;
return Point(x,y); }
Point Point::operator++( int) {
Point temp(x,y); x++; y++;
return temp; }
void main() {
Point A,B(3,4),C,D; A++;
cout << \的值为:\ ++B;
cout << \的值为:\ C=A+B;
cout << \的值为:\ D=A-B;
cout << \的值为:\}
2.修改程序lab7_2.cpp,给bicycle、motorcar、motorcycle这三个类均定义Run、Stop等成员函数,而main函数不变,编译运行,观察结果并作分析。
3.修改上面的main函数,定义一个基类的指针,再用这个指针分别调用这几个类的对象的成员函数,编译运行,观察结果并分析原因;把基类的Run、Stop定义为虚函数,再编译运行,观察结果并分析原因;把其他类的Run、Stop也定义为虚函数,看看是否有变化。示例程序lab8_2.cpp代码如下:
#include
class vehicle {
private:
int MaxSpeed;
39
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库《C++与面向对象程序设计》实验指导书(9)在线全文阅读。
相关推荐: