Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Figure {
- protected:
- float s;
- public:
- void show() {
- cout << "Info about figure:" << endl;
- calculate();
- info();
- }
- virtual void calculate() = 0;
- virtual void info() = 0;
- };
- class Rectangle : public Figure {
- private:
- float a;
- float b;
- public:
- Rectangle() {}
- Rectangle(float _a, float _b) : a(_a), b(_b) {}
- void calculate() {
- s = a * b;
- cout << "Area of rectangle = " << s << endl;
- cout << endl;
- }
- void info() {
- cout << "Sides of rectangle:" << endl;
- cout << "\ta = " << a << endl;
- cout << "\tb = " << b << endl;
- cout << endl;
- }
- friend istream& operator >> (istream& is, Rectangle& x);
- bool operator > (const Rectangle& x) { return a > x.a && b > x.b; }
- };
- class Trapeze : public Figure {
- private:
- float a;
- float b;
- float h;
- public:
- Trapeze() {}
- Trapeze(float _a, float _b, float _h) : a(_a), b(_b), h(_h) {}
- void calculate() {
- s = h * (a + b) / 2;
- cout << "Area of trapeze = " << s << endl;
- cout << endl;
- }
- void info() {
- cout << "Sides of trapeze:" << endl;
- cout << "\ta = " << a << endl;
- cout << "\tb = " << b << endl;
- cout << "\th = " << h << endl;
- cout << endl;
- }
- friend istream& operator >> (istream& is, Trapeze& x);
- bool operator > (const Trapeze& x) { return a > x.a && b > x.b && h > x.h; }
- };
- istream& operator >> (istream& is, Rectangle& x) {
- cout << "Enter sides of rectangle:" << endl;
- cout << "\ta = "; is >> x.a;
- cout << "\tb = "; is >> x.b;
- cout << endl;
- return is;
- }
- istream& operator >> (istream& is, Trapeze& x) {
- cout << "Enter sides of trapeze:" << endl;
- cout << "\ta = "; is >> x.a;
- cout << "\tb = "; is >> x.b;
- cout << "\th = "; is >> x.h;
- cout << endl;
- return is;
- }
- int main() {
- cout << "===Work with arrays===" << endl << endl;
- int n;
- cout << "Enter count of objects:" << endl;
- cin >> n;
- cout << endl;
- Rectangle rectArr[n];
- for (int i = 0; i < n; i++) {
- cout << "Enter info about " << i + 1 << " rectangle:" << endl;
- cin >> rectArr[i];
- }
- for (int i = 1; i < n; i++) {
- cout << "Comparing (operator >) " << i << " & " << i + 1 << " rectangles: "<< endl;
- cout << "result: " << (rectArr[i - 1] > rectArr[i]) << endl;
- cout << endl;
- }
- Trapeze trapzArr[n];
- for (int i = 0; i < n; i++) {
- cout << "Enter info about " << i + 1 << " trapeze:" << endl;
- cin >> trapzArr[i];
- }
- for (int i = 1; i < n; i++) {
- cout << "Comparing (operator >) " << i << " & " << i + 1 << " trapezes: "<< endl;
- cout << "result: " << (trapzArr[i - 1] > trapzArr[i]) << endl;
- cout << endl;
- }
- cout << "===Work using the base class pointer===" << endl << endl;
- Figure* figr = new Rectangle(3, 4);
- figr->show();
- figr = new Trapeze(3, 4, 2);
- figr->show();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement