C++ 프로그래밍 vector 관련 질문드려요ㅠㅠ
#include <iostream>
#include <vector>
using namespace std;
class Shape {
protected:
int x, y;
public:
virtual void draw() = 0;
virtual double getArea() {
return 0;
}
};
class Rectangle : public Shape {
int width, height;
public:
Rectangle() {}
virtual ~Rectangle() {}
Rectangle(int x, int y, int width, int height) {
this->x = x;
this->y = y;
this->width = width;
this->height = height;
}
virtual void draw() {
cout << "Rectangle drawn at : (" << x << ", " << y << ")" << endl;
}
virtual double getArea() {
return width * height;
}
};
class Circle : public Shape {
int radius;
public:
Circle() {}
virtual ~Circle() {}
Circle(int x, int y, int radius) {
this->x = x;
this->y = y;
this->radius = radius;
}
virtual void draw() {
cout << "Circle drawn at : (" << x << ", " << y << ")" << endl;
}
virtual double getArea() {
return radius radius 3.14;
}
};
int main() {
vector<Shape*> vShape;
vShape.push_back(new Rectangle(10, 10, 30, 40)); // 10,10 위치에 가로세로 30x40
vShape.push_back(new Circle(30, 30, 5)); // 30,30 위치에 반지름 5인 원
vShape.push_back(new Rectangle(20, 30, 10, 10));
// 여기에 순차 출력 및 면적 총합계 계산 코드를 추가한다
for (auto it = vShape.begin(); it != vShape.end(); it++) {
Shape v = it;
cout << v->draw() << endl;
}
}
제가 여기까지는 해봤는데 메인에서 for 문 여기서 자꾸 막히더라구요..ㅠㅠ
출력결과가
Rectangle: (30, 40) drawn at : (10, 10)
Circle: (5) drawn at : (30, 30)
Rectangle: (10, 10) drawn at : (20, 30)
Total area: 1378.5
이렇게 나와야하는데 방법 좀 알 수 있을까요??
마지막 합계도 알려주시면 감사하겠습니다..ㅠㅠ
[소스 수정]
#include <iostream>
#include <vector>
using namespace std;
class Shape {
protected:
int x, y;
public:
virtual void draw() = 0;
virtual double getArea() {
return 0;
}
};
class Rectangle: public Shape {
int width, height;
public:
Rectangle() {}
virtual ~Rectangle() {}
Rectangle(int x, int y, int width, int height) {
this->x = x;
this->y = y;
this->width = width;
this->height = height;
}
virtual void draw() {
cout << "Rectangle:("<< width <<","<< height <<") drawn at:(" <<x<< "," <<y<< ")" << endl;
}
virtual double getArea() {
return width * height;
}
};
class Circle : public Shape {
int radius;
public:
Circle() {}
virtual ~Circle() {}
Circle(int x, int y, int radius) {
this->x = x;
this->y = y;
this->radius = radius;
}
virtual void draw() {
cout << "Circle:("<<radius<<") drawn at:(" << x << "," << y << ")" << endl;
}
virtual double getArea() {
return radiusradius3.14;//원의면적구하는 공식:반지름반지름pi
}
};
int main() {
vector<Shape*>vShape;
vShape.push_back(new Rectangle(10, 10, 30, 40));//10,10 위치에 가로세로 30x40
vShape.push_back(new Circle(30, 30, 5));//30,30 위치에 반지름 5인원
vShape.push_back(new Rectangle(20, 30, 10,10));//20,30 위치에 가로세로 10x10
//여기에 순차 출력및 면적 총합계 계산 코드를 추가한다.
double getAreaTotal = 0.;
for (auto it = vShape.begin(); it != vShape.end();it++) {
Shape v = it;
v->draw();
getAreaTotal += v->getArea();
}
cout << "Total area:" << getAreaTotal << endl;
system("pause");
return 0;
}
/*
* 출력 결과가
* Rectangle:(30,40) drawn at:(10,10)
* Circle:(5) drawn at:(30,30)
* Rectangle:(10,10) drawn at:(20,30)
* Total area: 1378.5
*/
[실행결과 캡쳐]