개발자
Rectangle 클래스를 상속받은 파생클래스 Cube 클래스를 추가하여 프로그램 완성시켜야 합니다. Cube 클래스는 Rectangle 클래스에서 정의된 멤버와 멤버함수를 사용하여 클래스를 작성 main 함수를 통한 결과는 Volumn : 60 입니다. 일하면서 학점은행제 수업을 듣는데 내용이 점점 어려워지니 집중도 안되고 졸다보니 무슨말이지도 잘 모르겠어요...... 자랑은 아니지만 한번만 살려주세요 ㅠ #include <iostream> using namespace std; class Rectangle { int width, length; protected: Rectangle(int width, int length) { this->width = width; this->length = length; } int getArea() { return width * length; } }; int main() { Cube cube(3, 4, 5); cout << "Volumn : " << cube.getVolumn() << endl; }
답변 1
먼저 C가 아니라 C++구요, 요렇게 하시면 될 거 같아요 :) 그리고 int main()에는 return 0; 로 끝내주는게 관례입니다.
1#include <iostream>
2using namespace std;
3
4class Rectangle {
5private:
6 int width, length;
7protected:
8 Rectangle(int width, int length) { this->width = width; this->length = length; }
9 int getArea() { return width * length; }
10};
11
12class Cube: Rectangle {
13private:
14 int height;
15public:
16 Cube(int width, int length, int height): Rectangle(width, length) {this->height = height;}
17 int getVolumn() {return getArea()*height;}
18};
19
20
21int main() {
22 Cube cube(3, 4, 5);
23 cout << "Volumn : " << cube.getVolumn() << endl;
24 return 0;
25}
지금 가입하면 모든 질문의 답변을 볼 수 있어요!
현직자들의 명쾌한 답변을 얻을 수 있어요.
이미 회원이신가요?
지금 가입하면 모든 질문의 답변을 볼 수 있어요!