面向对象编程技术(作业题二)

#include <iostream>

#define _USE_MATH_DEFINES
#include <cmath>

using namespace std;

class CShape
{
public:
 virtual double Area() = 0;  // 求面积
 virtual double Perimeter() = 0; // 求周长
};

// 圆
class CCircle : public CShape
{
protected:
 double m_nRadius;  // 圆的半径

public:
 CCircle(double nRadius)
  : m_nRadius(nRadius)
 {
 }

 virtual ~CCircle()
 {
 }

 virtual double Area()
 {
  return M_PI * m_nRadius * m_nRadius;
 }

 virtual double Perimeter()
 {
  return 2 * M_PI * m_nRadius;
 }
};

// 圆内接正方形
class CSquareInCircle : public CShape
{
protected:
 double m_nRadius;  // 圆的半径

public:
 CSquareInCircle(double nRadius)
  : m_nRadius(nRadius)
 {
 }

 virtual ~CSquareInCircle()
 {
 }

 virtual double Area()
 {
  return (2 * m_nRadius * m_nRadius);
 }

 virtual double Perimeter()
 {
  return (4 * sqrt(2.0) * m_nRadius);
 }
};

// 圆外接正方形
class CSquareOutCircle : public CShape
{
protected:
 double m_nRadius;  // 圆的半径

public:
 CSquareOutCircle(double nRadius)
  : m_nRadius(nRadius)
 {
 }

 virtual ~CSquareOutCircle()
 {
 }

 virtual double Area()
 {
  return (2 * m_nRadius * 2 * m_nRadius);
 }

 virtual double Perimeter()
 {
  return (4 * 2 * m_nRadius);
 }
};

///
int main(int argc, char* argv[])
{
 CShape* pShape = NULL;
 CCircle circle(3);
 CSquareInCircle square1(3);
 CSquareOutCircle square2(3);

 pShape = &circle;
 cout<<"圆的面积: "<<pShape->Area()<<endl;
 cout<<"圆的周长: "<<pShape->Perimeter()<<endl;

 pShape = &square1;
 cout<<"圆内接正方形的面积: "<<pShape->Area()<<endl;
 cout<<"圆内接正方形的周长: "<<pShape->Perimeter()<<endl;

 pShape = &square2;
 cout<<"圆外接正方形的面积: "<<pShape->Area()<<endl;
 cout<<"圆外接正方形的周长: "<<pShape->Perimeter()<<endl;

 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值