Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class Figure {
- public:
- virtual double area() = 0;
- virtual double perimeter() = 0;
- };
- class Rectangle : public Figure{
- protected:
- int w; // Ширина
- int h; // Высота
- public:
- Rectangle(int w = 0, int h = 0) {
- this->w = w;
- this->h = h;
- }
- int width() {
- return w;
- }
- int height() {
- return h;
- }
- double area() {
- return w * h;
- }
- double perimeter() {
- return (w + h) * 2;
- }
- friend istream& operator>> (istream&, Rectangle&);
- friend ostream& operator<< (ostream&, const Rectangle&);
- };
- istream& operator>>(istream& in, Rectangle& a) {
- in >> a.w >> a.h;
- return in;
- }
- ostream& operator<<(ostream& on, const Rectangle& a) {
- on << a.w << " " << a.h;
- return on;
- }
- class Circle : public Figure {
- protected:
- const double p = 3.14159265;
- int r;
- public:
- Circle(int r = 0) {
- this->r = r;
- }
- int radius() {
- return r;
- }
- double area() {
- return p * r * r;
- }
- double perimeter() {
- return 2 * p * r ;
- }
- friend istream& operator>> (istream&, Circle&);
- friend ostream& operator<< (ostream&, const Circle&);
- };
- istream& operator>>(istream& in, Circle& a) {
- in >> a.r;
- return in;
- }
- ostream& operator<<(ostream& on, const Circle& a) {
- on << a.r;
- return on;
- }
- double array_area(Figure** array, int n) {
- double sum = 0;
- for (int i = 0; i < n; i++) {
- sum += array[i]->area();
- }
- return sum;
- }
- double array_perimeter(Figure** array, int n) {
- double sum = 0;
- for (int i = 0; i < n; i++) {
- sum += array[i]->perimeter();
- }
- return sum;
- }
- int main() {
- int n;
- cin >> n;
- string tip;
- Figure** a = new Figure * [n];
- for (int i = 0; i < n; i++){
- cin >> tip;
- if (tip == "Rectangle"){
- int w, h;
- cin >> w >> h;
- a[i] = new Rectangle(w,h);
- }
- else{
- int r;
- cin >> r;
- a[i] = new Circle(r);
- }
- }
- cout << array_area(a,n) << " " << array_perimeter(a,n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement