Cześć! Potrzebuję pomocy przy zadaniu w c++. W programie utworzyłem tablicę wskaźników na klasę bazową Shape z funkcją virtual Total(suma pól i obwodów figur) , ustawiłem ją na 9 obiektów Circle oraz Rectangle, teraz potrzebuję za jej pomocą policzyć sumę pól i obwodów figur z tej tablicy za pomocą pętli oraz metod (w funkcji main), natomiast niestety nie wiem, jak to zrobić. Oto kod:
#include <iostream>
#include <math.h>
class Shape {
public:
virtual float Total() {
return 0;
}
};
class Point2D {
float x;
float y;
public:
Point2D(float x, float y) {
this->x = x;
this->y = y;
}
int getX() {
return x;
}
int getY() {
return y;
}
void setX(float x) {
this->x = x;
}
void setY(float y) {
this->y = y;
}
};
class Circle : public Shape {
float r;
Point2D center;
public:
Circle(float x, float y, float r): center(x,y) {
center.setX(x);
center.setY(y);
this->r = r;
}
void print() {
std::cout << "Wlasnosci obiektu Circle: " << std::endl;
std::cout << "Srodek: " << center.getX() << " " << center.getY() << std::endl;
std::cout << "Dlguosc promienia: " << this->r << std::endl;
}
};
class Rectangle : public Shape{
private:
int firstSize;
int secondSize;
public:
Rectangle() {
firstSize = 1;
secondSize = 1;
}
Rectangle(int first, int second):firstSize(first>=0?first:1), secondSize(second>=0?second:1) {
}
Rectangle(const Rectangle& rect1) {
firstSize = rect1.firstSize;
secondSize = rect1.secondSize;
}
int getFirstSize() {
return firstSize;
}
int getSecondSize() {
return secondSize;
}
void setFirstSize(int size) {
if(size > 0) {
firstSize = size;
}
else {
std::cout << "Podales niepoprawna wartosc - bok nie moze wynosic 0 ani byc ujemny!" << std::endl;
}
}
void setSecondSize(int size) {
if(size > 0) {
secondSize = size;
}
else {
std::cout << "Podales niepoprawna wartosc - bok nie moze wynosic 0 ani byc ujemny!" << std::endl;
}
}
float area() {
float area;
area = firstSize * secondSize;
return area;
}
float perimeter() {
float perimeter;
perimeter = (2 * firstSize) + (2 * secondSize);
return perimeter;
}
float diagonal() {
float diagonal;
diagonal = sqrt((firstSize * firstSize) + (secondSize * secondSize));
return diagonal;
}
float volume (float height) {
float volume;
volume = firstSize * secondSize * height;
return volume;
}
};
int main() {
int n;
Rectangle r1(3,5);
Rectangle r2(7,3);
Rectangle r3(6,8);
Circle c1(3,5,4);
Circle c2(8,6,2);
Shape*tab[9] = {&r1, &r2,&r3, &c1, &c2, new Rectangle(7,4), new Rectangle(9,7), new Circle(4,8,3), new Circle(1,3,4)};
for(int i=0; i<n; i++) {
tab[i]->Total();
}
return 0;
}
Z góry dzięki za pomoc :)