Guest User

Untitled

a guest
Nov 18th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.64 KB | None | 0 0
  1. //header file
  2. #ifndef H_rectangleType
  3. #define H_rectangleType
  4.  
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. template <class Type>
  10. ostream& operator << (ostream& osObject, const rectangleType<Type>& rectangle)
  11. {
  12.     osObject << "Length = "  << rectangle.length
  13.              << "; Width = " << rectangle.width;
  14.  
  15.     return osObject;
  16. }
  17.  
  18. template <class Type>
  19. class rectangleType
  20. {
  21.       //Overload the stream insertion and extraction operators
  22.     friend ostream& operator << (ostream&, const rectangleType<Type> &);
  23.     friend istream& operator >> (istream&, rectangleType<Type> &);
  24.  
  25. public:
  26.     void setDimension(Type l, Type w);
  27.     double getLength() const;
  28.     double getWidth() const;
  29.     double area() const;
  30.     double perimeter() const;
  31.     void print() const;
  32.  
  33.     rectangleType operator+(const rectangleType<Type>&) const;
  34.       //Overload the operator +
  35.     rectangleType operator*(const rectangleType<Type>&) const;
  36.       //Overload the operator *
  37.  
  38.     bool operator==(const rectangleType<Type>&) const;
  39.       //Overload the operator ==
  40.     bool operator!=(const rectangleType<Type>&) const;
  41.       //Overload the operator !=
  42.  
  43.     rectangleType();
  44.     rectangleType(Type l, Type w);
  45.  
  46. private:
  47.     Type length;
  48.     Type width;
  49. };
  50.  
  51. template <class Type>
  52. void rectangleType<Type>::setDimension(Type l, Type w)
  53. {
  54.     if (l >= 0)
  55.         length = l;
  56.     else
  57.         length = 0;
  58.  
  59.     if (w >= 0)
  60.         width = w;
  61.     else
  62.         width = 0;
  63. }
  64.  
  65. template <class Type>
  66. double rectangleType<Type>::getLength() const
  67. {
  68.     return length;
  69. }
  70.  
  71. template <class Type>
  72. double rectangleType<Type>::getWidth()const
  73. {
  74.     return width;
  75. }
  76.  
  77. template <class Type>
  78. double rectangleType<Type>::area() const
  79. {
  80.     return length * width;
  81. }
  82.  
  83. template <class Type>
  84. double rectangleType<Type>::perimeter() const
  85. {
  86.     return 2 * (length + width);
  87. }
  88.  
  89. template <class Type>
  90. void rectangleType<Type>::print() const
  91. {
  92.     cout << "Length = "  << length
  93.          << "; Width = " << width;
  94. }
  95.  
  96. template <class Type>
  97. rectangleType<Type>::rectangleType(Type l, Type w)
  98. {
  99.     setDimension(l, w);
  100. }
  101.  
  102. template <class Type>
  103. rectangleType<Type>::rectangleType()
  104. {
  105.     length = 0;
  106.     width = 0;
  107. }
  108.  
  109. template <class Type>
  110. rectangleType rectangleType<Type>::operator+(const rectangleType<Type>& rectangle) const
  111. {
  112.     rectangleType<Type> tempRect;
  113.  
  114.     tempRect.length = length + rectangle.length;
  115.     tempRect.width = width + rectangle.width;
  116.  
  117.     return tempRect;
  118. }
  119.  
  120. template <class Type>
  121. rectangleType rectangleType<Type>::operator*(const rectangleType<Type>& rectangle) const
  122. {
  123.     rectangleType<Type> tempRect;
  124.  
  125.     tempRect.length = length * rectangle.length;
  126.     tempRect.width = width * rectangle.width;
  127.  
  128.     return tempRect;
  129. }
  130.  
  131. template <class Type>
  132. bool rectangleType<Type>::operator==(const rectangleType<Type>& rectangle) const
  133. {
  134.     return (length == rectangle.length &&
  135.             width == rectangle.width);
  136. }
  137.  
  138. template <class Type>
  139. bool rectangleType<Type>::operator!=(const rectangleType<Type>& rectangle) const
  140. {
  141.     return (length != rectangle.length ||
  142.             width != rectangle.width);
  143. }
  144.  
  145. template <class Type>
  146. istream& operator >> (istream& isObject, rectangleType<Type>& rectangle)
  147. {
  148.     isObject >> rectangle.length >> rectangle.width;
  149.  
  150.     return isObject;
  151. }
  152.  
  153.  
  154. #endif
  155.  
  156.  
  157.  
  158.  
  159. //main.cpp
  160.  
  161. #include <iostream>                                   //Line 1
  162.  
  163. #include "rectangleType.h"                            //Line 2
  164.  
  165. using namespace std;                                  //Line 3
  166.  
  167. int main()                                            //Line 4
  168. {                                                     //Line 5
  169.     rectangleType<int> myRectangle(23, 45);                //Line 6
  170.     rectangleType<double> yourRectangle;                      //Line 7
  171.  
  172.     cout << "Line 8: myRectangle: " << myRectangle
  173.          << endl;                                     //Line 8
  174.  
  175.     cout << "Line 9: Enter the length and width "
  176.          <<"of a rectangle: ";                        //Line 9
  177.     cin >> yourRectangle;                             //Line 10
  178.     cout << endl;                                     //Line 11
  179.  
  180.     cout << "Line 12: yourRectangle: "
  181.          << yourRectangle << endl;                    //Line 12
  182.  
  183.     cout << "Line 13: myRectangle + yourRectangle: "
  184.          << myRectangle + yourRectangle << endl;      //Line 13
  185.     cout << "Line 14: myRectangle * yourRectangle: "
  186.          << myRectangle * yourRectangle << endl;      //Line 14
  187.  
  188.     return 0;                                         //Line 15
  189. }                                                     //Line 16
Add Comment
Please, Sign In to add comment