본문 바로가기
프로그램/C++ 1000제

C++ 133제] 어서와! C++은 처음이지! CHAPTER 12. PROGRAMMING EXERCISE 1. p514

by 건티 2024. 12. 16.
728x90

출처 : 반크_반크 20년 백서

 

참고풀이]

#include <iostream>
using namespace std;

class Shape
{
   int x, y;
public:
   Shape(int x, int y) :x(x), y(y) {}
   virtual double getArea() = 0;
};

class Rect :public Shape
{
   int s1, s2;
public:
   Rect(int x, int y, int s1, int s2) :Shape(x, y), s1(s1), s2(s2) {}
   double getArea() { return (double)s1 * (double)s2; }
};

class Triangle :public Shape
{
   double width, height;
public:
   Triangle(int x, int y, double w, double h) : Shape(x, y), width(w), height(h) {}
   double getArea() { return width * height * 0.5; }
};

class Circle :public Shape
{
   double radius;
public:
   Circle(int x, int y, double r):Shape(x,y),radius(r){}
   double getArea() { return radius * radius * 3.14; }
};

int main()
{
   int i;
   Shape* sh[3] = { NULL };

   sh[0] = new Rect(1, 1, 52, 53);
   sh[1] = new Circle(1, 1, 69);
   sh[2] = new Triangle(1, 1, 55, 66);

   for (i = 0; i < 3; i++)
      cout << "도형 #" << i << "의 면적 : " << sh[i]->getArea() << endl;

   for (i = 0;i < 3;i++)
   {
      delete sh[i];
      sh[i] = NULL;
   }

   return 0;
}

 

참고풀이 결과]

 

 

 

 

 

대한민국의 아름다운 영토, 독도의 여름

 

반응형

댓글