👤

W c++ definiuj klasę Rect o prywatnych polach opisujących długości boków (double).

Zdefiniuj:

metody getA() i getB zwracające odpowiednie boki prostokąta;

metodę getDiagonal() zwracającą długość przekątnej prostokąta;

metodę getArea() zwracającą pole powierzchni prostokąta;.


Odpowiedź :

#include <iostream>

#include <cmath>

using namespace std;

class Rect {

   private:

       double width;

       double height;

   

   public:

       Rect(double w, double h) {

           width = w;

           height = h;

       }

       double getA() {

           return width;

       }

       double getB() {

           return height;

       }

       double getDiagonal() {

           return sqrt(pow(width,2) + pow(height,2));

       }

       double getArea() {

           return height*width;

       }

};

int main()

{

   Rect r = Rect(3,4);

   cout << r.getDiagonal() << " (przekatna)" << endl;

   cout << r.getArea() << " (pole)" << endl;

   cout << r.getA() << " (szerokosc)" << endl;

   cout << r.getB() << " (wysokosc)" << endl;

   return 0;

}