[实验6]类的继承与多态

一、实验目的

1.理解类的继承和派生机制

2.掌握派生类的定义和使用

3.掌握派生类成员的标识与访问中同名覆盖原则、二元作用域分辨符和虚基类的用法

4.掌握派生类构造函数和析构函数的定义及调用次序

5.理解运算符重载的目的,掌握运算符重载函数的编写方法

二、实验准备

1.  类的继承和派生

请结合第 7 章课件和教材复习以下内容:

1)引入继承和派生机制的目的

2)基本概念:继承、派生、基类、直接基类、间接基类、派生类

3)语法:

① 派生类定义的语法格式(单重继承、多重继承);

② 派生类构造函数极其初始化列表写法

4)派生类成员的标识与访问

① 同名覆盖指的是什么?

② 二元作用域分辨符在什么情况下使用?

③ 什么是虚基类?引入虚基类的目的是什么?如何使用?

 

2. 运算符重载

   请结合第 8 章课件和教材学习以下内容:

1)运算符重载的目的

2)运算符重载的规则和限制

3)运算符重载函数的语法格式

4)运算符重载时,究竟重载为类的成员函数,还是友元,还是普通函数,需要综合考虑哪些因素?

三、实验内容

1.某计算机硬件系统,为了实现特定的功能,在某个子模块设计了 ABC 三款芯片用于数字计算。各个芯片的计算功能如下:

A 芯片:计算两位整数的加法(m+n)、计算两位整数的减法(m-n)

B 芯片:计算两位整数的加法(m+n)、计算两位整数的乘法(m*n)

C 芯片:计算两位整数的加法(m+n)、计算两位整数的除法(m/n)

ABC 三个芯片分别定义类,描述上述芯片的功能,并在 main 函数中测试这三个类。

(提示:利用类的继承和派生,抽象出共有属性和操作作为基类。)

 

2.定义一个车(vehicle)基类,具有数据成员 maxspeed, weight(均为 int  型), 函数成员 run(), stop(),由此派生出自行车(bicycle)类、汽车(motorcar)类。其中, bicycle  类新增数据成员高度(height),motorcar  类新增数据成员座位数(seatnum)属性。再从 bicycle和 motorcar  派生出摩托车(motorcycle)类,并在主程序中测试这个类。(每个类都要求定义构造函数和析构函数)(提示: ① 注意把 vehicle 设置为虚基类; ② run(), stop()函数体,通过输出字符串 run, stop 简单模拟。)

 

3.基于「实验 4 类和对象-2」中设计并实现的类 Fraction,创建派生类 iFraction,用以描述如下形式的分数:

2

1 3

要求:

1) 更新 Fraction

Fraction 类编写运算符+-*/重载函数,实现在 main 函数中直接用+-

*/进行 Fraction 类运算。

2)设计并实现派生 iFraction

① 为派生类 iFraction 定义构造函数,实现 iFraction 对象的初始化

② 为派生类 iFraction 增加一个成员函数,用于在屏幕上显示 iFraction 对象

3)设计一个普通函数 convertF()用于对 iFraction 类对象进行规范化处理。(*选做*

(提示:把 convertF()设计为 Fraction 类和 iFraction 类的友元函数)例如:(更多情形请自行考虑)

 

5 2

3 → 1 3

4)以多文件结构方式编写(fraction.h, fraction.cpp, ifraction.h, ifraction.cpp, main.cpp

 

4. ***选做***

基于提供的程序文件,补足并扩充程序,实现一个多类型玩家角色扮演游戏。在本次实验附件包 ex4 中有如下文件:

container.h, container.cpp, player.h, player.cpp, swordsman.h, swordsman.cpp, main.cpp

1)阅读源码,理解并补足程序,让程序运行生效。

其中,程序中出现有????????之处,是需要补足的部分。

2)画出这个角色扮演游戏 UML 类图,尤其是类和类之间的关系

3)设计并实现 archer 类和 mage 类。在UML 类图中也加进这两个新类。

4)修改 main 函数,随机生成不同角色的敌人,并保证程序正常运行。

5)为游戏增加其它元素,完善游戏的可玩性、趣味性,等。

(说明:这道涉及虚函数、运行时多态。你可以在第 8 章学完后尝试编写,或者,

在尝试编写这道题的过程中,学习第 8 章虚函数和运行时多态的知识。)

四、实验结论

1.实验内容 1

采用多文件结构,给出每个文件完整源代码,及运行测试截图。

 base.h:

#include<iostream>
using namespace std;
class base{
    public:
        base(int a,int b):m(a),n(b){}
        int add(){
            cout<<"m+n="<<m+n<<endl;
        }
    private:
        int m,n;
};

 

A.h:

#include<iostream>
using namespace std;
class A:public base{
    public:
        A(int a,int b):base(a,b){
        m=a;
        n=b;
        }
        int minus(){
            cout<<"m-n="<<m-n<<endl;
        }
    private:
        int m,n;
};

 

B.h:

#include<iostream>
using namespace std;
class B:public base{
    public:
        B(int a,int b):base(a,b){
        m=a;
        n=b;
        }
        int mul(){
            cout<<"m*n="<<m*n<<endl;
        }
    private:
        int m,n;
};

 

C.h:

#include<iostream>
using namespace std;
class C:public base{
    public:
        C(int a,int b):base(a,b){
        m=a;
        n=b;
        }
        int div(){
            cout<<"m/n="<<m/n<<endl;
        }
    private:
        int m,n;
};

 

main.cpp:

#include<iostream>
#include"base.h"
#include"A.h"
#include"B.h"
#include"C.h"
using namespace std;
int main()
{
    A a(15,8);
    a.add();
    a.minus();
    B b(6,5);
    b.add();
    b.mul();
    C c(20,4);
    c.add();
    c.div();
    return 0;
 } 

 

 

2.实验内容 2

vehicle.h

 

#include<iostream>
using namespace std;
class vehicle{
    public: 
        vehicle(int m,int w):maxspeed(m),weight(w){
        cout<<"maxspeed:"<<maxspeed<<endl;
        cout<<"weight:"<<weight<<endl;
        }
        ~vehicle(){}
        void run(){
            cout<<"run"<<endl;
        };
        void stop(){
            cout<<"stop"<<endl<<endl;
        };
    private:
        int maxspeed,weight;
}; 

 

 

bicycle.h

#include<iostream>
using namespace std;
class bicycle:virtual public vehicle{
    public: 
        bicycle(int m,int w,int h):vehicle(m,w){
        height=h;
        cout<<"bicycle is coming"<<endl;
        
        cout<<"height:"<<height<<endl;
        }
        ~bicycle(){}
    private:
        int height;
}; 

 

motorcar.h

#include<iostream>
using namespace std;
class motorcar:virtual public vehicle{
    public: 
        motorcar(int m,int w,int s):vehicle(m,w){
        seatnum=s;
        cout<<"seatnum:"<<seatnum<<endl;
        }
        ~motorcar(){}
    private:
        int seatnum;
}; 

 

motorcycle.h

#include<iostream>
using namespace std;
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){}
        ~motorcycle(){}
}; 

 

main.cpp

#include<iostream>
#include"vehicle.h"
#include"bicycle.h"
#include"motorcar.h"
#include"motorcycle.h"
using namespace std;
int main(){
vehicle a(200,100);
a.run();
a.stop();
bicycle b(50,20,1);
b.run();
b.stop();
motorcar c(150,60,2);
c.run();
c.stop();
motorcycle d(100,40,1,2);
d.run();
d.stop();
return 0;
}

 

 

 

3.实验内容 3

采用多文件结构,给出每个文件完整源代码,及运行测试截图。

 

Fraction.h

 

#include<iostream>
class Fraction
{
    public:
        Fraction(int t,int b);
        Fraction(int t);
        Fraction();
        void show();
        int zc(int t,int b);
        void simplify();
        Fraction operator+(Fraction &b);
        Fraction operator-(Fraction &b);
        Fraction operator*(Fraction &b);
        Fraction operator/(Fraction &b);
    protected:
        int top;
        int bottom;
};

 

 

Fraction.cpp

#include"Fraction.h"
#include<iostream>
#include<cmath>
using namespace std;
Fraction::Fraction(int t,int b)
{
    top=t;
    bottom=b;
}
Fraction::Fraction(int t)
{
    top=t;
    bottom=1;
}
Fraction::Fraction()
{
    top=0;
    bottom=1;
}
int Fraction::zc(int t,int b)
{
    if(t%b==0)
        return b;
    else
        zc(b,t%b);
}
void Fraction::simplify()
{
    if(bottom<0)
    {
        top=-top;
        bottom=-bottom;
    }
    int z=zc(abs(top),abs(bottom));
    top=top/z;
    bottom=bottom/z;
}
void Fraction::show(){
cout<<top<<"/"<<bottom<<endl;
}
Fraction Fraction::operator+(Fraction &b)
{
    return Fraction(top*b.bottom+b.top*bottom,bottom*b.bottom);
}
Fraction Fraction::operator-(Fraction &b)
{
    return Fraction(top*b.bottom-b.top*bottom,bottom*b.bottom);
}
Fraction Fraction::operator*(Fraction &b)
{
    return Fraction(top*b.top,bottom*b.bottom);
}
Fraction Fraction::operator/(Fraction &b)
{
    return Fraction(top*b.bottom,bottom*b.top);
}

 

iFraction.h

#include"Fraction.h"
class iFraction:public Fraction
{
    private:
        int i;
    public:
        iFraction(int t=0,int b=1,int z=0):Fraction(t,b),i(z){}
        void print();
        friend iFraction convertF(iFraction &p); 
};

 

Fraction.cpp

#include"iFraction.h"
#include<iostream>
using namespace std;
void iFraction::print()
{
    if(top==0)
        cout<<i<<endl;
    else if(i==0)
    {
        cout<<top<<endl;
        cout<<"-"<<endl;
        cout<<bottom<<endl;
    }
    else
    {
        cout<<" "<<top<<endl;
        cout<<i<<"-"<<endl;
        cout<<" "<<bottom<<endl;
    }
}
iFraction convertF(iFraction &p)
{
    p.simplify(); 
    p.i+=p.top/p.bottom;
    p.top%=p.bottom;
    return p;
}

 

main.cpp

#include <iostream>
#include "iFraction.h"
using namespace std;
int main()
{
    Fraction a(-2,6);
    cout<<"a=";
    a.show();
    Fraction b(3);
    cout<<"b=";
    b.show();
    Fraction c;
    cout<<"c=";
    c.show();
    c=a+b;
    cout<<"a+b=";
    c.show();
    c=a-b;
    cout<<"a-b=";
    c.show();
    c=a*b;
    cout<<"a*b=";
    c.show();
    c=a/b;
    cout<<"a/b=";
    c.show();
    iFraction d(15,6,2);
    cout<<"d="<<endl;
    d.print();
    d=convertF(d);
    cout<<"convertF d="<<endl;
    d.print(); 
    return 0;
}

 

 

 

4.  实验内容 4 ***选做***

1)简洁陈述自己实现的游戏功能;

2)给出UML 类图

3)采用多文件结构,给出每个文件完整源代码。

(这个程序源代码文件比较多,如有 github 账号,也可以直接附上游戏项目文档文件github 页面地址)

 

posted @ 2018-06-06 11:07  酥茶  阅读(984)  评论(0编辑  收藏  举报