Advertisement
DanikKUL

lab2.1

Sep 29th, 2022 (edited)
1,324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. // +            (Складывает площади и возвращает полученный квадрат)
  7. // &            (Умножает стороны и возвращает квадрат с полученной стороной)
  8.  
  9. class Square{
  10.     double length;
  11. public:
  12.     explicit Square(double = 0);
  13.  
  14.     double getPerimeter() const;
  15.     double getArea() const;
  16.     double getLength() const;
  17.     void setLength(double);
  18.     void setLength(int);
  19.     void printAll() const;
  20.     void printAll(int) const;
  21.  
  22.     Square operator + (Square) const;
  23.     Square& operator = (Square);
  24.     Square operator & (Square) const;
  25. };
  26.  
  27. Square::Square(double length){
  28.     if (length < 0){
  29.         cout << "Невозможно создать объект с отрицательной длиной стороны"
  30.                 "\nСоздан нулевой объект" << endl;
  31.         length = 0;
  32.     }
  33.     this -> length = length;
  34. }
  35.  
  36. double Square::getPerimeter() const{
  37.     return length * 4;
  38. }
  39.  
  40. double Square::getArea() const{
  41.     return length * length;
  42. }
  43.  
  44. double Square::getLength() const{
  45.     return length;
  46. }
  47.  
  48. void Square::setLength(double len){
  49.     if (len < 0) len = 0;
  50.     this -> length = len;
  51. }
  52.  
  53. void Square::setLength(int len){
  54.     if (len < 0) len = 0;
  55.     this -> length = len;
  56. }
  57.  
  58. void Square::printAll() const{
  59.     cout << "Длина: " << getLength() << endl;
  60.     cout << "Периметр: " << getPerimeter() << endl;
  61.     cout << "Площадь: " << getArea() << endl;
  62. }
  63.  
  64. void Square::printAll(int param) const{
  65.     if (!(param == 1 || param == 2)){
  66.         param = 3;
  67.     }
  68.     char* ans[3] = {(char*)"Длина: ", (char*)"Периметр: ", (char*)"Площадь: "};
  69.     double val[3] = {length, getPerimeter(), getArea()};
  70.     for (int i = 0; i < param; i++) cout << ans[i] << val[i] <<  endl;
  71. }
  72.  
  73. Square Square::operator + (Square sq) const{
  74.     Square sq1;
  75.     sq1.setLength(sqrt(this -> getArea() + sq.getArea()));
  76.     sq1.printAll();
  77.     return sq1;
  78. }
  79.  
  80. Square& Square::operator = (Square sq){
  81.     this -> length = sq.getLength();
  82.     return *this;
  83. }
  84.  
  85. Square Square::operator & (Square sq) const {
  86.     Square sq1;
  87.     sq1.setLength(this -> getLength() * sq.getLength());
  88.     return sq1;
  89. }
  90.  
  91. int main() {
  92.     Square sq(3);
  93.     Square sq1(3);
  94.     Square sq2 = sq1 + sq;
  95.     cout << sq2.getLength() << endl;
  96.     cout << sq2.getArea() << endl;
  97.     Square sq3 = sq & sq1;
  98.     sq3.printAll();
  99.     return 0;
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement