实验六 多态性与虚函数

本文通过四个C++程序实例,详细分析了多态性和虚函数的运用,包括重载、覆盖的概念及实际效果。程序涉及基类与派生类之间的成员函数调用,展示了动态绑定和静态绑定的不同。通过对输出结果的解释,深入理解多态在面向对象编程中的作用。

实验目的和要求

     了解静态联编的动态联编的概念。掌握动态联编的条件。
实验内容
1.分析并调试下列程序。
[cpp]  view plain  copy
  1. //sy6_1.cpp  
  2. #include<iostream>  
  3. using namespace std;  
  4. class Base  
  5. {  
  6.     public:  
  7.         virtual void f(float x){cout<<"Base::f(float)"<<x<<endl;}  
  8.         void g(float x){cout<<"Base::g(float)"<<x<<endl;}  
  9.         void h(float x){cout<<"Base::h(float)"<<x<<endl;}  
  10. };  
  11. class Derived:public Base  
  12. {  
  13.    public:  
  14.         virtual void f(float x){cout<<"Derived::f(float}"<<x<<endl;}  
  15.         void g(int x){cout<<"Derived::g(int)"<<x<<endl;}  
  16.         void h(float x){cout<<"Derived::h(float)"<<x<<endl;}  
  17. };  
  18. int main()  
  19. {  
  20.     Derived d;  
  21.     Base *pb=&d;  
  22.     Derived *pd=&d;  
  23.     pb->f(3.14f);  
  24.     pd->f(3.14f);  
  25.     pb->g(3.14f);  
  26.     pb->h(3.14f);  
  27.     pd->h(3.14f);  
  28.     return 0;  
  29. }  
(1)找出以上程序中使用了重载和覆盖函数。
(2)写出程序的输出结果,并解释输出结果

程序的输出结果如下:
2 . 分析并调试下列程序
[cpp]  view plain  copy
  1. //sy6_2.cpp  
  2. #include<iostream>  
  3. using namespace std;  
  4. class Base  
  5. {  
  6.     public:  
  7.         void f(int x){cout<<"Base::f(int)"<<x<<endl;}  
  8.         void f(float x){cout<<"Base::f(float)"<<x<<endl;}  
  9.        virtual void g(void){cout<<"Base::g(void)"<<endl;}  
  10. };  
  11. class Derived:public Base  
  12. {  
  13.     public:  
  14.         virtual void g(void){cout<<"Derived::g(void}"<<endl;}  
  15. };  
  16. int main()  
  17. {  
  18.     Derived d;  
  19.    Base *pb=&d;  
  20.     pb->f(42);  
  21.     pb->f(3.14f);  
  22.     pb->g();  
  23.     return 0;  
  24. }  
(1)找出以上程序中使用了重载和覆盖函数。
(2)写出程序的输出结果,并解释输出结果。

程序的输出结果如下:

3. 分析并调试下列程序
[cpp]  view plain  copy
  1. //sy6_3.cpp  
  2. #include<iostream>  
  3. using namespace std;  
  4. class Point  
  5. {  
  6.     public:  
  7.         Point(double i,double j){x=i;y=j;}  
  8.         double Area(){return 0.0;}  
  9.     private:  
  10.         double x,y;  
  11. };  
  12. class Rectangle:public Point  
  13. {  
  14.     public:  
  15.        Rectangle(double i,double j,double k,double l):Point(i,j){w=k;h=l;}  
  16.         double Area(){return w*h;}  
  17.     private:  
  18.         double w,h;  
  19. };  
  20. int main()  
  21. {  
  22.     Point p(3.5,7);  
  23.     double A=p.Area();  
  24.     cout<<"Area= "<<A<<endl;  
  25.     Rectangle r(1.2,3,5,7.8);  
  26.     A=r.Area();  
  27.     cout<<"Area= "<<A<<endl;  
  28.    return 0;  
  29. }  


写出程序的输出结果,并解释输出结果。



4. 分析并调试下列程序
[cpp]  view plain  copy
  1. //sy6_4.cpp  
  2. #include<iostream>  
  3. using namespace std;  
  4. const double PI=3.1415;  
  5. class Shap  
  6. {  
  7.     public:  
  8.         virtual double Area()=0;  
  9. };  
  10. class Triangle:public Shap  
  11. {  
  12.     public:  
  13.         Triangle(double h,double w){H=h;W=w;}  
  14.        double Area(){return 0.5*H*W;}  
  15.     private:  
  16.         double H,W;  
  17. };  
  18. class Rectangle:public Shap  
  19. {  
  20.   
  21.     public:;  
  22.         Rectangle(double h,double w){H=h;W=w;}  
  23.        double Area(){return H*W;}  
  24.     private:  
  25.         double H,W;  
  26. };  
  27. class Circle:public Shap  
  28. {  
  29.     public:  
  30.         Circle(double r){R=r;}  
  31.         double Area(){return PI*R*R;}  
  32.     private:  
  33.         double R;  
  34. };  
  35. class Square:public Shap  
  36. {  
  37.     public:  
  38.        Square(double s){S=s;}  
  39.         double Area(){return S*S;}  
  40.     private:  
  41.         double S;  
  42. };  
  43. double Total(Shap *s[],int n)  
  44. {  
  45.     double sum=0;  
  46.     for(int i=0;i<n;i++)  
  47.         sum+=s[i]->Area();  
  48.     return sum;  
  49. }  
  50. int main()  
  51. {  
  52.    Shap *s[5];  
  53.    s[0]=new Square(8.0);  
  54.    s[1]=new Rectangle(3.0,8.0);  
  55.    s[2]=new Square(12.0);  
  56.    s[3]=new Circle(8.0);  
  57.    s[4]=new Triangle(5.0,4.0);  
  58.    double sum=Total(s,5);  
  59.    cout<<"SUM = "<<sum<<endl;  
  60.     return 0;  
  61. }  

程序的输出结果如下:

(1)指出抽象类。
(2)指出纯虚函数,并说明它的作用。
(3)每个类的作用是什么?整个程序的作用是什么?

5. 某学校对教师每个月工资的计算规定如下:固定工资+课时补贴;教授的固定工资为5000元,每个课时补贴50;副教授的固定工资为3000,每个课时补贴30元;讲师的固定工资为2000元,每个课时补贴20元。定义教师抽象类,派生不同职称的教师类,编写程序求若干个教师的月工资。(sy6_5.cpp)

6. 把实验5中的第4题的Shape类定义为抽象类,提供共同操作界面的纯虚函数。TwoDimShape类和ThreeDimShape类仍然抽象类,第3层具体类才能提供全部函数的实现。在测试函数中,使用基类指针实现不同派生类对象的操作。

分析与讨论

1.结合实验内容中第1题和第2题,说明重载与覆盖的区别。

2.总结静态联编和动态联编的区别和动态联编的条件。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值