Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- ////////////////////////////////////////////////////////////////
- // ФИГУРА
- // Базовый (Абстрактный) класс
- class figure {
- protected:
- // ...
- //--------------------------------------------------------------
- public:
- // Конструктор без аргументов
- figure(void) { /* ... */ }
- //--------------------------------------------------------------
- virtual float area() const =0; // Возвращает площадь
- virtual float perimeter() const =0; // Возвращает периметр
- virtual void print() const =0; // Выводит значения
- // p.s. Если добавить в абстрактный класс поле,
- // то print() стоит сделать НЕчистой виртуальной функцией,
- // т.е. добавить в нее вывод нового поля/полей
- // и вызывать ( как figure::print() ) в перегрузках
- };
- ////////////////////////////////////////////////////////////////
- // КРУГ
- // Дочерний класс (класса ФИГУРА)
- class circle : public figure {
- private:
- float radius;
- //--------------------------------------------------------------
- public:
- // Конструктор с 1-arg: радиусом
- circle( const float& rad ) : figure() , radius(rad) { /* ... */ }
- //--------------------------------------------------------------
- float area() const
- { return ( 3.14159 * radius * radius ); }
- float perimeter() const
- { return ( 2 * 3.14159 * radius ); }
- //--------------------------------------------------------------
- void print() const {
- cout << "| Information - Circle |" << endl
- << "Area: " << area() << endl
- << "Perimeter: " << perimeter() << endl
- << "Radius: " << radius << endl
- << endl;
- }
- };
- ////////////////////////////////////////////////////////////////
- // ТРАПЕЦИЯ
- // Дочерний класс (класса ФИГУРА)
- class trapeze : public figure {
- private:
- float horizontal_top,
- horizontal_lower,
- vertical_left,
- vertical_right,
- height;
- //--------------------------------------------------------------
- public:
- // Конструктор с 2-arg: горизонталью и вертикалью
- trapeze
- ( const float& horiz_top, const float& horiz_low,
- const float& vert_left, const float& vert_right,
- const float& _height ) :
- figure() , horizontal_top(horiz_top) , horizontal_lower(horiz_low) ,
- height(_height), vertical_left(vert_left) , vertical_right(vert_right) { /* ... */ }
- //--------------------------------------------------------------
- float area() const
- { return height * ( ( horizontal_top + horizontal_lower ) / 2 ); }
- float perimeter() const
- { return horizontal_top + horizontal_lower + vertical_left + vertical_right; }
- //--------------------------------------------------------------
- void print() const {
- cout << "| Information - Trapeze |" << endl
- << "Area: " << area() << endl
- << "Perimeter: " << perimeter() << endl
- << "Horizontal - Top: " << horizontal_top << endl
- << "Horizontal - Lower: " << horizontal_lower << endl
- << "Vertical - Left: " << vertical_left << endl
- << "Vertical - Right: " << vertical_right << endl
- << "Height: " << height << endl
- << endl;
- }
- };
- ////////////////////////////////////////////////////////////////
- // ПРЯМОУГОЛЬНИК
- // Дочерний класс (класса ФИГУРА)
- class rectangle : public figure {
- private:
- float horiz, vert;
- //--------------------------------------------------------------
- public:
- // Конструктор с 2-arg: горизонталью и вертикалью
- rectangle( const float& horizontal, const float& vertical ) :
- figure() , horiz(horizontal) , vert(vertical) { /* ... */ }
- //--------------------------------------------------------------
- float area() const
- { return ( horiz * vert ); }
- float perimeter() const
- { return ( 2 * horiz + 2 * vert ); }
- //--------------------------------------------------------------
- void print() const {
- cout << "| Information - Rectangle |" << endl
- << "Area: " << area() << endl
- << "Perimeter: " << perimeter() << endl
- << "Horizontal: " << horiz << endl
- << "Vertical: " << vert << endl
- << endl;
- }
- };
- ////////////////////////////////////////////////////////////////
- template<class ... T>
- auto sum_area(T ... obj) {
- return (obj.area() + ...);
- }
- ////////////////////////////////////////////////////////////////
- int main() {
- vector<figure*> vecFigure;
- vecFigure.push_back(new circle(5));
- vecFigure.push_back(new rectangle(5, 3));
- vecFigure.push_back(new trapeze(3, 5, 10, 10, 7));
- for (auto iter : vecFigure) {
- iter -> print();
- }
- auto result = sum_area
- (rectangle(1, 1), rectangle(1, 2), rectangle(1, 3),
- rectangle(2, 1), rectangle(2, 2), rectangle(2, 3),
- rectangle(3, 1), rectangle(3, 2), rectangle(3, 3),
- rectangle(4, 1));
- cout << "Sum area: " << result << endl;
- return 0;
- }
- ////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement