C++

[C++] 명품 C++ 프로그래밍 실습문제 9장 8번

Hs’s Coding vlog 2023. 1. 14. 15:33

[C++]명품 C++ 프로그래밍 실습문제 9장 8번

[C++]명품 C++ programming 실습문제 9장 8번

명품 C++ 프로그래밍실습문제/연습문제 /C++

 

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
//순수가상함수를 만들면 된다.
class Shape{
protected:
    string name;
    int width, heigth;
public:
    Shape(string n = " " ,int W = 0,int h=0){name = n ; width = W; heigth =h;}
    string getName(){return name; }
    virtual double getArea(){return 0;}
};
class Oval : public Shape{
public:
    Oval(string n, int w, int h) : Shape(n,w,h) { }
    string getName(){return name; }
    virtual double getArea(){return  width*heigth*3.14 ;}
};
class Rect : public Shape{
public:
    Rect(string n, int w, int h) : Shape(n,w,h) { }
    string getName(){return name; }
    virtual double getArea(){return width*heigth;}
    
};
class Triangular : public Shape{
public:
    Triangular(string n, int w, int h) : Shape(n,w,h) { }
    string getName(){return name; }
    virtual double getArea(){return (width*heigth)/(double)2;}
    
};


int main(){
    Shape *p[3];
    p[0] =new Oval("빈대떡",10,20);
    p[1] =new Rect("찰떡",30,40);
    p[2] =new Triangular("토스트",30,40);
    
    for(int i=0;i<3;i++){
        cout << p[i]->getName() << "넓이는 " << p[i]->getArea() << endl;
        
    }
    
    for(int i=0 ; i<3 ; i++) delete p[i];

    return 0;
};