Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //header file
- #ifndef H_rectangleType
- #define H_rectangleType
- #include <iostream>
- using namespace std;
- template <class Type>
- ostream& operator << (ostream& osObject, const rectangleType<Type>& rectangle)
- {
- osObject << "Length = " << rectangle.length
- << "; Width = " << rectangle.width;
- return osObject;
- }
- template <class Type>
- class rectangleType
- {
- //Overload the stream insertion and extraction operators
- friend ostream& operator << (ostream&, const rectangleType<Type> &);
- friend istream& operator >> (istream&, rectangleType<Type> &);
- public:
- void setDimension(Type l, Type w);
- double getLength() const;
- double getWidth() const;
- double area() const;
- double perimeter() const;
- void print() const;
- rectangleType operator+(const rectangleType<Type>&) const;
- //Overload the operator +
- rectangleType operator*(const rectangleType<Type>&) const;
- //Overload the operator *
- bool operator==(const rectangleType<Type>&) const;
- //Overload the operator ==
- bool operator!=(const rectangleType<Type>&) const;
- //Overload the operator !=
- rectangleType();
- rectangleType(Type l, Type w);
- private:
- Type length;
- Type width;
- };
- template <class Type>
- void rectangleType<Type>::setDimension(Type l, Type w)
- {
- if (l >= 0)
- length = l;
- else
- length = 0;
- if (w >= 0)
- width = w;
- else
- width = 0;
- }
- template <class Type>
- double rectangleType<Type>::getLength() const
- {
- return length;
- }
- template <class Type>
- double rectangleType<Type>::getWidth()const
- {
- return width;
- }
- template <class Type>
- double rectangleType<Type>::area() const
- {
- return length * width;
- }
- template <class Type>
- double rectangleType<Type>::perimeter() const
- {
- return 2 * (length + width);
- }
- template <class Type>
- void rectangleType<Type>::print() const
- {
- cout << "Length = " << length
- << "; Width = " << width;
- }
- template <class Type>
- rectangleType<Type>::rectangleType(Type l, Type w)
- {
- setDimension(l, w);
- }
- template <class Type>
- rectangleType<Type>::rectangleType()
- {
- length = 0;
- width = 0;
- }
- template <class Type>
- rectangleType rectangleType<Type>::operator+(const rectangleType<Type>& rectangle) const
- {
- rectangleType<Type> tempRect;
- tempRect.length = length + rectangle.length;
- tempRect.width = width + rectangle.width;
- return tempRect;
- }
- template <class Type>
- rectangleType rectangleType<Type>::operator*(const rectangleType<Type>& rectangle) const
- {
- rectangleType<Type> tempRect;
- tempRect.length = length * rectangle.length;
- tempRect.width = width * rectangle.width;
- return tempRect;
- }
- template <class Type>
- bool rectangleType<Type>::operator==(const rectangleType<Type>& rectangle) const
- {
- return (length == rectangle.length &&
- width == rectangle.width);
- }
- template <class Type>
- bool rectangleType<Type>::operator!=(const rectangleType<Type>& rectangle) const
- {
- return (length != rectangle.length ||
- width != rectangle.width);
- }
- template <class Type>
- istream& operator >> (istream& isObject, rectangleType<Type>& rectangle)
- {
- isObject >> rectangle.length >> rectangle.width;
- return isObject;
- }
- #endif
- //main.cpp
- #include <iostream> //Line 1
- #include "rectangleType.h" //Line 2
- using namespace std; //Line 3
- int main() //Line 4
- { //Line 5
- rectangleType<int> myRectangle(23, 45); //Line 6
- rectangleType<double> yourRectangle; //Line 7
- cout << "Line 8: myRectangle: " << myRectangle
- << endl; //Line 8
- cout << "Line 9: Enter the length and width "
- <<"of a rectangle: "; //Line 9
- cin >> yourRectangle; //Line 10
- cout << endl; //Line 11
- cout << "Line 12: yourRectangle: "
- << yourRectangle << endl; //Line 12
- cout << "Line 13: myRectangle + yourRectangle: "
- << myRectangle + yourRectangle << endl; //Line 13
- cout << "Line 14: myRectangle * yourRectangle: "
- << myRectangle * yourRectangle << endl; //Line 14
- return 0; //Line 15
- } //Line 16
Add Comment
Please, Sign In to add comment