继承和派生试题

声明一个Shape(形状)基类,它有两个派生类:Circle(圆)和Square(正方形)

要求:
(1)根据给出的圆心坐标和半径计算圆的面积;
(2)根据给出的正方形中点坐标和一个顶点坐标计算正方形的面积;
(3)定义拷贝构造函数。提示:Shape类的数据成员包括中心点的坐标,Circle类中新增一个数据成员,即圆的半径,Square类新增一个顶点的坐标

#include <iostream>
using namespace std;
#define PI 3.14
class shape
{
 public:
   shape( ){   }
   shape(int a,int b) {x=a;y=b;}
   shape( shape &s) {x=s.x;y=s.y;}
   void showme( ){ cout<<"x="<<x<<endl<<"y="<<y<<endl;}
   int getx( ) {return x;}
   int gety( ) { return y;}
 private:
   int x,y;
};
class circle:public shape
{
 public:
  circle( ) {  }
  circle( int a,int b,int c):shape(a,b),r(c) {  }
  circle( circle &c):shape( c) {r=c.r;}
  double area( ) { return PI*r*r; }
  void showme( ) {shape::showme( ); cout<<"r="<<r<<endl; }
 private:
  int r;
};
 class square:public shape
{
  public:
  square( ) {   }
  square( int a,int b,int c, int d):shape(a,b),xx(c),yy(d) { }
  square( square &s):shape( s),xx(s.xx),yy(s.yy) {   }
  double area( ) { return (xx-getx( ))*(yy-gety( )); }
  void showme( ) { shape::showme( ); 
cout<<"xx="<<xx<<endl<<"yy="<<yy<<endl;
}
 private:
  int xx,yy;
};
int main( )
{
  shape s( 4,5);
  s.showme( );
  circle c(5,6,7);
  c.showme( );
  cout<<c.area( )<<endl;
  square ss(3,4,7,8);
  ss.showme( );
 cout<<ss.area( )<<endl;
 square ss1(ss);
 ss1.showme( );
 return -1;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃冰粉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值