实验4 多态性和虚函数

淮海工学院计算机科学系

实验报告书

 

课程名:《 C++程序设计(二)》  

题   目:      多态性和虚函数        

                                              

班   级:                   

学   号:                

姓   名:                     

 

 

 

   
   

评语:

   

 

   

 

   

 

   

 

   

 

   

 

   

成绩:                            指导教师:                  

   

 

   

                             批阅时间:    年    月     日

   
   

 

 

 

 

 

 

 

 

 

 

 

 

1. 实验内容或题目

(1)声明二维坐标类作为基类派生圆的 类,把派生类圆作为基类,派生圆柱体类。其中,基类二维坐标类有成员数据:x、y坐标值;有成员函数:构造函数实现对基类成员数据的初始化、输出的成员函数,要求输出坐标位置。派生类圆类有新增成员数据:半径(R);有成员函数:构造函数实现对成员数据的初始化、计算圆面积的成员函数、输出半径的成员函数。派生圆柱体类新增数据有高(H);新增成员函数有构造函数、计算圆柱体体积的函数和输出所有成员的函数。请完成程序代码的编写、调试。

(2)教材393页7-8题。

(3)教材416页1、4、5题。

2. 实验目的与要求

(1)理解继承与派生的概念

(2)掌握通过继承派生出一个新的类的方法

(3)了解多态性的概念

(4)了解虚函数的作用与使用方法

⑵ 源代码

(1)#include<iostream>

using namespace std;

class point 

{ public: 

point(float a=0,float b=0);

void setpoint(float,float); 

float get_x()const{return x;} 

float get_y()const{return y;} 

friend ostream &operator<<(ostream &,  point &);

protected:  float x,y;

};

point::point(float a,float b)

{  x=a;

   y=b;

}

void point::setpoint(float a,float b)

{  x=a; 

   y=b;

}

ostream &operator<<(ostream &output,const point&p)

    output<<"["<<p.get_x()<<","<<p.get_y()<<"]"<<endl; 

    return output;

}

class circle:public point

{ public: 

circle(float a=0,float b=0,float r=0):point(a,b),radius(r){} 

float area()const{return 3.14*radius*radius;}  

float getradius()const{return radius;}

protected:  float radius; };

class cyl:public circle

{ public: 

cyl(float a=0,float b=0,float r=0,float h=0):circle(a,b,r),height(h){} 

float volume()const

{return 3.14*radius*radius*height;}    

friend ostream &operator<<(ostream &,  point &); 

float getheight()const{return height;}

protected:  float height;

};

ostream &operator<<(ostream &output,const  cyl&cy)

{  output<<"["<<cy.get_x()<<","<<cy.get_y()<<"],"<<"h="<<cy.getheight()<<",volumn="<<cy.volume()<<endl;

return output;

}

int main()

    point p(1,3); 

    circle c(1,2,3);    

    cyl cy(1,2,3,4); 

    cout<<"x="<<p.get_x()<<",y="<<p.get_y()<<endl;

    cout<<"area="<<c.area()<<endl;  cout<<"x="<<cy.get_x()<<",y="<<cy.get_y()<<",h="<<cy.getheight()<<",volume="<<cy. volume()<<endl;

    return 0;

}

(2)7

#include <iostream>

using namespace std;

class A

{public:

A(){a=0;b=0;}

A(int i){a=i;b=0;}

A(int i,int j){a=i;b=j;}

void display(){cout<<"a="<<a<<"b="<<b;}

private:

    int a;

    int b;

};

class B : public A

{public:

B(){c=0;}

B(int i):A(i){c=0;}

B(int i,int j):A(i,j){c=0;}

B(int i,int j,int k):A(i,j){c=k;}

void display1()

{display();

cout<<"c="<<c<<endl;

}

private:

    int c;

};

int main()

{

    B b1;

    B b2(1);

    B b3(1,3);

    B b4(1,3,5);

    b1.display1();

    b1.display1();

    b1.display1();

    b1.display1();

    return 0;

}

(2)8

#include<iostream>

using namespace std;

class A

{ public: 

A(){cout<<"constructing A"<<endl;} 

~A(){cout<<"destrucing A"<<endl;}

}; 

class B:public A 

{  public:  

B(){cout<<"constructing B"<<endl;}  

~B(){cout<<"destrucingB"<<endl;}

};

class C:public B 

{  public:  

C(){cout<<"constructing C"<<endl;}  

~C(){cout<<"destrucing C"<<endl;} 

}; 

int main() 

{

    C c1;  

    return 0; 

}

(3) 1

头文件1

class Point 

{ public: 

Point(float a=0,float b=0); 

void setPoint(float,float);

float get_X()const{return x;} 

float get_Y()const{return y;} 

friend ostream &operator<<(ostream &,const  point &);

protected: 

float x,y;

};

头文件2

class Circle:public point

{ public: 

Circle(float a=0,float b=0,float r=0); 

void setradius(float); 

float area()const; 

float getradius()const; 

friend ostream &operator<<(ostream &,const  Circle &);

protected: 

float radius;

 };

头文件3

class Cyl:public circle

{ public: 

Cyl(float a=0,float b=0,float r=0,float h=0); 

void setHeight(float); 

float getHeight()const;    

float area()const; 

float volume()const;    

friend ostream &operator<<(ostream &,  const cyl &); 

protected: 

float height;

};

源文件1

#include<iostream.h>

#include"ex_1_1(head).h"

Point::Point(float a,float b)

{x=a;  y=b; }

void Point::setPoint(float a,float b)

{  x=a;  y=b; }

float point::get_x()const{return x;}

float point::get_y()const{return y;}

ostream &operator<<(ostream &output,const Point&p) {  output<<"["<<p.x()<<","<<p.y()<<"]"<<endl;

  return output;

}

源文件2

#include<iostream.h>

#include"ex_1_1(head).h"

#include"ex_1_1(head1).h"

Circle::Circle(float a ,float b ,float r ):Point(a,b),radius(r){}

void Circle::setRadius(float r){radius=r;}

float Circle::getRadius( )const{return radius;}

float Circle::area()const{return 3.14*radius*radius;}

ostream &operator<<(ostream  &output,const Circle &c)

output<<"Center ["<<c.x<<","<<c.y<<"],r="<<c.radius<<",area="<<c.area()<<endl;  return output;

源文件3

#include<iostream.h>

#include"ex_1_1(head).h"

#include"ex_1_1(head1).h"

#include"ex_1_1(head2).h"

Cylinder:: Cylinder (float a ,float b ,float r ,float h ):circle(a,b,r),height(h){}

void Cylinder::setHeight(float h){height=h;}

float Cylinder:: getHeight()const{return height;}     

float Cylinder::area()const{return 2*Circle::area()+2*3.14*radius*height;}

float Cylinder::volume()const

{return Circle::area()*height;}

ostream &operator<<(ostream &output,const  Cyl&cy) {  output<<"Center="<<cy.x<<","<<cy.y<<"],"<<"h="<<cy.height<<"area="<<cy.area()<<",volumn="<<cy.volume()<<endl;

 return output;

主函数

#include<iostream.h>

#include "ex_1_1(head).h"

#include "ex_1_1(head1).h"

#include "ex_1_1(head2).h"

int main()

{

    Cylinder cy1 (1,2,3,4)

        cout<<"\noriginal cylinder:\nx="<<cy1.getX()<<"y="<<cy1.getY()<",r="<<

            <<cy1.getRadius()<<",h="<<cy1.getHeight()<<"\narea="<<cy1.area()<<",volume="<<cy1.volume()<<endl;

cy1.setHeught(15)

cy1.setRadius(7.5)

cy1.setPoint(5,5)

cout<<"\npref cylinder:\n"<<cy1;

Point &pRef=cy1;

cout<<"\npRef as Point:"<<pRef;

Circle &Ref=cy1;

cout<<"\npRef as Circle:"<<cRef;

return 0;

}

(3) 1

#include<iostream.h>

#include "ex_1_1(head).h"

#include "ex_1_1(head1).h"

#include "ex_1_1(head2).h"

int main()

{  point p(1,3); 

circle c(1,2,3);    

cyl cy(1,2,3,4); 

cout<<"x="<<p.get_x()<<",y="<<p.get_y()<<endl; 

cout<<"area="<<c.area()<<endl;  cout<<"x="<<cy.get_x()<<",y="<<cy.get_y()<<",h="<<cy.getheight()<<",area="<<cy.area()<<",volume="<<cy. volume()<<endl; 

return 0;

}

(3)4

#include<iostream>

using namespace std;

class shape

{ public: 

virtual float PrintArea()

const{return 0.0;}

}; 

// http://www.pprar.com

class circle:public shape

{ public:

circle(float r=0); 

void setradius(float); 

float getradius( )const; 

virtual float  PrintArea()const;

protected:  float radius;

};

circle::circle(float r){radius=r;}

void circle::setradius(float r) {radius=r;}

float circle::getradius()const{return radius;}

float circle:: PrintArea() const {return 3.14*radius*radius;}  

int main()

{

    circle cir(5); 

    cir.PrintArea();  

    shape *pt;  pt=&cir ;     

    pt->PrintArea(); 

    cout<<pt->PrintArea()<<endl; 

    return 0;

}

(3)5

#include<iostream>

#include<cmath>

using namespace std;

class shape

{ public: 

virtual float PrintArea() const{return 0.0;}

};  

class circle:public shape { public:  circle(float r=0); 

virtual float  PrintArea()const;

protected:  float radius;

};

circle::circle(float r) {radius=r;}

float circle:: PrintArea()

const {return 3.14*radius*radius;} 

class  rectangle:public shape

{ public:  

rectangle(float a=0,float b=0);

virtual float  PrintArea()const;

protected:  float  x,y;

};

rectangle:: rectangle(float a,float b)

{ x=a;y=b;}

float  rectangle:: PrintArea() const {return  x*y;} 

class  triangle:public shape

{ public:  

triangle(float a=0,float b=0,float c=0); 

virtual float  PrintArea()const;

protected:  float  x,y, z ;

};

triangle::triangle (float a,float b,float c) 

{ x=a;y=b;z=c;}

float  triangle:: PrintArea() const {return   x*y*sqrt(1-(x*x+y*y-z*z)*(x*x+y*y-z*z)/4*x*x*y*y)/2;}  

int main()

{

    circle cir(5);    

    rectangle rec(1,2); 

    triangle  tri(3,4,5); 

    shape *pt[3]; 

    pt[0]=&cir;  

    pt[1]=&rec; 

    pt[2]=&tri;    

    cout<<pt[0]->PrintArea()<<endl;    

    cout<<pt[1]->PrintArea()<<endl;    

    cout<<pt[2]->PrintArea()<<endl; 

    return 0;

}

测试数据与实验结果(可以抓图粘贴)

(1)                        

(2)7

(2)8

(3)1

  (3)4、 、

(3)5

结果分析与实验体会

这次作业运用书上给的例题加以结合便可轻而易举的完成,但是第二题我做了很长时间都没有做上来,每次改正之后都会残留一些小小的错误,最后用尽各种手段,终于把他搞了出来,但是我还是有一些地方不明白,都是自己不够用心,不够认真才坐不上来,今后要勤加学习,把不明白的搞懂。

 

转载于:https://www.cnblogs.com/php0368/p/3501677.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值