C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 Q&A
C++Builder Programming Q&A
[72684] Re:클래스에 관한 질문입니다(꼭 도와주세요!)
bugfree [bugfree] 3904 읽음    2015-05-26 12:59
안상형 님이 쓰신 글 :
: switch 문에서 case로 도형을 선택하여 해당 도형에 관한 부피와 넓이를 구하는 식을 구하려고 합니다.
: 여기서 각각의 case 에다가 해당 클래스를 입력해줘야하는데, 자꾸 막힌다고 해야할까요?
: 코드 수정좀 부탁드리겠습니다.
: 죄송합니다.
:
:
: #include
: using namespace std;
: class Rectangle
: {
: protected:
: 	float lengtha,lengthb, height;
: public:
: 	Rectangle() : lengtha(0.0),lengthb(0.0), height(0.0)
: 	{
: 		cout << "가로를 입력하시오: ";
: 		cin >> lengtha;
: 		cout << "세로를 입력하시오";
: 		cin >> lengthb;
: 		cout << "높이를 입력하시오";
: 		cin >> height;
: 	}
: };
: class Recvolume : public Rectangle
: {
: public:
: 		float volume()
: 		{
: 			return lengtha*lengthb*height;
: 		}
: };
: class RecArea : public Rectangle
: {
: public:
: 	float area()
: 	{
: 		return (lengtha*height) * 2 + (lengtha*lengthb) * 2 + (lengthb*height) * 2;
: 	}
: };
: class Square
: {
: protected:
: 	float lengtha, lengthb, height;
: public:
: 	Square() : lengtha(0.0), lengthb(0.0), height(0.0)
: 	{
: 		cout << "가로를 입력하시오: ";
: 		cin >> lengtha;
: 		cout << "세로를 입력하시오";
: 		cin >> lengthb;
: 		cout << "높이를 입력하시오";
: 		cin >> height;
: 	}
: };
: class Squavolume : public Square
: {
: public:
: 	float volume()
: 	{
: 		return lengtha*lengthb*height;
: 	}
: };
: class SquaArea : public Square
: {
: public:
: 	float area()
: 	{
: 		return (lengtha*height) * 2 + (lengtha*lengthb) * 2 + (lengthb*height) * 2;
: 	}
: };
: class Pitch
: {
: protected:
: 	float r  ;
: public:
: 	Pitch() : r(0.0)
: 	{
: 		cout << "반지름을 입력하시오(π=3.14로 계산) ";
: 		cin >> r;
: 	}
: };
: class Pitvolume : public Pitch
: 
: {
: public:
: 	double volume()
: 	{
: 		return 4 / 3 * 3.14*r*r;
: 	}
: };
: 
: class PitArea : public Pitch
: {
: public:
: 	double area()
: 	{
: 		return 4 * 3.14 * r * r;
: 	}
: };
: class Cone
: {
: protected:
: 	float r ,height, l;
: public:
: 	Cone(): r(0.0), height(0.0), l(0.0)
: 	{
: 		cout << "반지름을 입력하시오(π=3.14로 계산) ";
: 		cin >> r;
: 		cout << "높이를 입력하시오: ";
: 		cin >> height;
: 		cout << "호의 길이를 구하시오: ";
: 		cin >> l;
: 	}
: };
: class ConeVolume : public Cone
: {
: public:
: 	double volume()
: 	{
: 		return 1 / 3 * 3.14*r*r*height;
: 	}
: };
: class ConeArea : public Cone
: {
: public:
: 	double area()
: 	{
: 		return 3.14*r*r + 3.14*r*l;
: 	}
: };
: 
: int main()
: {
: 	char menu;
: 
: 
: 	while (true)
: 	{
: 		//메뉴출력
: 		cout << "1.직육면체 부피, 넓이 구하기\n";
: 		cout << "2.정육면체 부피, 넓이 구하기\n";
: 		cout << "3.구   부피, 넓이 구하기\n";
: 		cout << "4.원뿔 부피, 넓이 구하기\n";
: 		cout << "5.종료\n";
: 		cout << "메뉴 선택: ";
: 		cin  >> menu;
: 
: 		if (menu == '5')
: 			break;
: 
: 		switch (menu)
: 		{
: 			
: 		case '1':{
: 			Rectangle b;
: 			cout << "Enter data for first rectangle to find area.\n";
: 			Recvolume a;
: 			cout << "직육면체의 부피는: "<<  << "입니다";
: 			
: 			cout << "직육면체의 넓이는: ";
: 		}
: 				 break;
: 		case '2':{
: 			
: 			cout << "정육면체의 부피는: ";
: 
: 			cout << "정육면체의 넓이는: ";
: 		}
: 				 break;
: 		case'3':{
: 
: 			cout << "구의 부피는: ";
: 
: 			cout << "구의 넓이는: ";
: 		}
: 				break;
: 		case '4':{
: 
: 			cout << "원뿔의 부피는: ";
: 
: 			cout << "원뿔의 넓이는: ";
: 		}
: 				 break;
: 		}
: 	}
: 
: 	return 0;
: }
: 



#include < iostream >

using namespace std;

class Shape
{
public:
    Shape() : lenA(0), lenB(0), height(0) {}
    virtual char *type() = 0;
    virtual float volume() = 0;
    virtual float area() = 0;
    virtual void input() = 0;
protected:
    float lenA, lenB, height;
};

class Rectangle : public Shape
{
public:
    char* type() { return "Rectangle "; }
    void input()
    {
        cout << "lenA: "; cin >> lenA;
        cout << "lenB: "; cin >> lenB;
        cout << "height: "; cin >> height;
    }
    float volume() { return lenA * lenB * height; }
    float area()
    {
        return ((lenA * height) + (lenA * lenB) + (lenB * height)) * 2;
    }
};

class Square : public Rectangle
{
public:
    char* type() { return "Square "; }
};

class Pitch : public Shape
{
public:
    Pitch() : r(0) {}
    char* type() { return "Pitch" ; }
    void input() { cout << "r: "; cin >> r; }
    float volume() { return 4 / 3 * 3.14 * r * r; }
    float area() { return 4 * 3.14 * r * r; }
protected:
    float r;
};

class Cone : public Pitch
{
public:
    Cone() : l(0) {}
    char* type() { return "Cone "; }
    void input()
    {
        cout << "r: "; cin >> r;
        cout << "height: "; cin >> height;
        cout << "ho: "; cin >> l;
    }
    float volume() { return 1 / 3 * 3.14 *  r * r * height; }
    float area() { return 3.14 * r * r + 3.14 * r * l; }
protected:
    float l;
};

Shape* CreateObject(int index)
{
    Shape *obj;

    switch(index)
    {
    case 1: obj = new Rectangle(); break;
    case 2: obj = new Square(); break;
    case 3: obj = new Pitch(); break;
    case 4: obj = new Cone(); break;
    }

    return obj;
}

int main(int argc, char* argv[])
{
    int menu;

    while (true)
    {
        // menu
        cout << endl;
        cout << "1. Rectangle" << endl;
        cout << "2. Square" << endl;
        cout << "3. Pitch" << endl;
        cout << "4. Cone" << endl;
        cout << "5. Quit" << endl;
        cout << "select menu: " << endl;
        cin  >> menu;

        if (menu == 5)
            break;

        Shape *shape = CreateObject(menu);
        shape->input();
        cout << "type: "<< shape->type() << endl;
        cout << "volume: " << shape->volume() << endl;
        cout << "area: " << shape->area() << endl;
        delete shape;
    }

    return 0;
}



+ -

관련 글 리스트
72683 C++객체 안상형 3991 2015/05/25
72684     Re:클래스에 관한 질문입니다(꼭 도와주세요!) bugfree 3904 2015/05/26
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.