关闭

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

标签: 作业编程iostreamclassincludemath
914人阅读 评论(0) 收藏 举报
分类:

#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;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:82824次
    • 积分:1134
    • 等级:
    • 排名:千里之外
    • 原创:35篇
    • 转载:14篇
    • 译文:0篇
    • 评论:33条
    最新评论