Advertisement
andruhovski

Class demo

Oct 9th, 2014
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. // sp0203.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <cstdlib>
  7. #include <cmath>
  8.  
  9. using namespace std;
  10.  
  11. class Rectangle {
  12.     double sideA, sideB;
  13. public:
  14.     Rectangle() :sideA(0), sideB(0){};
  15.     Rectangle(double a, double b) : sideA(a), sideB(b) {}
  16.     Rectangle(const Rectangle &r);
  17.     void get_sides(double *a, double *b);
  18.     void set_sides(double a, double b);
  19.     double get_area() { return sideA*sideB; }
  20.     double get_perimeter() { return 2*(sideA+sideB); }
  21.     void show();
  22.     Rectangle operator+(Rectangle &Rect);
  23.     Rectangle operator-(Rectangle &Rect);
  24.     Rectangle operator=(const Rectangle &Rect);
  25.     bool operator<(Rectangle &Rect)
  26.     {      
  27.         return get_area() < Rect.get_area();
  28.     }
  29.     bool operator>(Rectangle &Rect)
  30.     {
  31.         return get_area() > Rect.get_area();
  32.     }
  33.     bool operator==(Rectangle &Rect)
  34.     {
  35.         return get_area() == Rect.get_area();
  36.     }
  37.     bool operator!=(Rectangle &Rect)
  38.     {
  39.         return get_area() == Rect.get_area();
  40.     }
  41. };
  42.  
  43. Rectangle::Rectangle(const Rectangle &r)
  44. {
  45.     sideA = r.sideA;
  46.     sideB = r.sideB;
  47. }
  48. void Rectangle::get_sides(double *a, double *b)
  49. {
  50.     *a = sideA;
  51.     *b = sideB;
  52. }
  53. void Rectangle::set_sides(double a, double b)
  54. {
  55.     if (a < 0 || b < 0)
  56.         throw "Side is negative";
  57.     sideA = a;
  58.     sideB = b;
  59. }
  60. void Rectangle::show()
  61. {
  62.     std::cout << "Side A=" << sideA << ", side B=" << sideB << std::endl;
  63. }
  64. Rectangle Rectangle::operator+(Rectangle &Rect)
  65. {
  66.     return Rectangle(fmax(sideA, Rect.sideA), fmax(sideB, Rect.sideB));
  67. }
  68.  
  69. Rectangle Rectangle::operator-(Rectangle &Rect)
  70. {
  71.     return Rectangle(fmin(sideA, Rect.sideA), fmin(sideB, Rect.sideB));
  72. }
  73.  
  74. Rectangle Rectangle::operator=(const Rectangle &Rect)
  75. {
  76.     if (this == &Rect)
  77.         return *this;      
  78.     sideA = Rect.sideA;
  79.     sideB = Rect.sideB;
  80.     return *this;  
  81. }
  82.  
  83.  
  84.  
  85. int _tmain(int argc, _TCHAR* argv[])
  86. {
  87.     Rectangle r1(20, 10),r2(r1);
  88.     r1.show();
  89.     cout << r1.get_area();
  90.     if (r1 == r2) r2.show();
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement