int Weight; public:
void SetMaxSpeed(int n){MaxSpeed=n;} int GetMaxSpeed(){ return MaxSpeed;} void SetWeight(int w){Weight=w;} int GetWeight(){return Weight;} virtural void Run()=0; virtural void Stop()=0; };
class bicycle :public vehicle {
protected : int Height; public:
void SetHeight(int H){Height=H;} int GetHeight(){return Height;}
virtual void Run(){cout<<”this bicycle is Running”< class motorcar :public vehicle { protected : int seatNum; public: void SetSeatNum(int H){seatNum=H;} int GetSeatNum(){return seatNum;} virtual void Run(){cout<<”this motorcar is Running”< class motorcycle: public bicycle, public motorcar { public: void Run(){cout<<”this motorcycle is Running”< 如果不把vehicle设置为虚基类,则会出现二义性。 四、 思考题 1、 注意类的继承中,基类与派生类之间的关系是什么,基类与派生类之间成员函数差 别; 2、 C++怎样消除二义性; 实验七 多态性 一、 实验目的 1、掌握运算符重载的方法。 2、学习使用虚函数实现动态多态性。 二、 主要仪器及耗材 PC电脑,VC6.0软件 三、 实验内容和步骤 1、实验内容: 1、声明Point类,有坐标m_x, m_y两个成员变量;对Point类重载“++”(自增)、“- -”(自减)运算符,实现对坐标值的改变。 2、声明一个车(vehicle)基类,有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类。从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。观察虚函数的作用。 2、实验步骤: 1、源程序: #include private: int m_x; int m_y; public: Point(int x, int y){m_x=x; m_y=y;} Point operator++(int) {++m_x; ++m_y; return Point(m_x,m_y); } Point operator- -(int) {--m_x; --m_y; return Point(m_x, m_y); } void disp(){cout<<”X=”< void main() { Point p(12, 26);p.disp(); p++;p.disp(); p--; p.disp();} 2、源程序为: #include class vehicle {protected: int MaxSpeed; int Weight; public: void SetMaxSpeed(int n){MaxSpeed=n;} int GetMaxSpeed(){ return MaxSpeed;} void SetWeight(int w){Weight=w;} int GetWeight(){return Weight;} virtural void Run()=0; virtural void Stop()=0; }; class bicycle :public vehicle {protected : int Height; public: void SetHeight(int H){Height=H;} int GetHeight(){return Height;} virtual void Run(){cout<<”this bicycle is Running”< class motorcar :public vehicle { protected : int seatNum; public: void SetSeatNum(int H){seatNum=H;} int GetSeatNum(){return seatNum;} virtual void Run(){cout<<”this motorcar is Running”< class motorcycle: public bicycle, public motorcar { public: void Run(){cout<<”this motorcycle is Running”< void main() {motorcycle MC; MC.Run(); MC.Stop();} 四、 思考题 1、 在类中怎样实现运算符的重载; 2、 通过重载怎样实现类的多态性; 实验八 实现栈类Stack 一、 实验目的 1、理解并掌握构造函数、析构函数、拷贝构造函数、默认构造函数和缺省参数的构造函数的含义、定义方法以及在对象的构造和撤消中的作用。 2、掌握虚函数和多态性的概念,掌握虚函数的定义方法、调用方法及其在实现多态性方面所起到的作用。了解纯虚函数与抽象基类的概念。 3、 了解类的静态成员(静态数据成员和静态成员函数)的概念、定义方法及其作用。 4、了解友元函数与友元类的概念、定义方法及其作用。 5、 了解运算符重载及在程序中实现运算符重载的方法。 二、 主要仪器及耗材 PC电脑,VC6.0软件 三、 实验内容和步骤 1、实验内容: 定义和实现栈类Stack。栈的规则是数据先进后出,栈的数据结构如下图所示 该类包括栈的常用操作如下所列(公有属性): 构造函数: Stack() 初始化栈顶及栈的长度,使得栈为空,并且接受栈的初始长度,默认值为100; 入栈操作: Push( ); 出栈操作: Pop( ); 清空栈操作: Clear( ); 获取栈顶元素: Peek( ); 678 判断栈空操作: IsEmpty( ); 456 判断栈满操作: IsFull( ); 成员函数的参数及返回值类型根据需要自定义。 123 数据成员(私有属性): 主要有指示栈顶位置变量(可以是数组下标或指针)、栈的成员变量(用数组表示)等; 提示:用数组来表示堆栈,即栈的成员变量可以用数组来表示。 … … Top 2、实验步骤: 1、源程序: #include Public: Stack(); void Push(const int a); int Pop(); void Clear(); int Peek()const; bool IsEmpty()const; bool IsFull() const; private: int List[MaxSize]; int Top; }; Stack::Stack() { Top=-1; } void Stack::Push(const int a) { if(Top==MaxSize-1)return; Top++; List[Top]=a; } int Stack::Pop() { int element; if(Top==-1)return 0; element=List[Top]; Top--; return element; } void Stack::Clear() { Top=-1; } int Stack::Peek()const { if(Top==-1)return 0; return List[Top]; } bool Stack::IsEmpty()const { if(Top==-1)return true; else return false;} bool Stack::IsFull()const { if(Top==MaxSize-1) return true; else return false; } void main() 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库面向对象程序设计实验指导书(5)在线全文阅读。
相关推荐: