123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
#include <iostream> using namespace std; class Square { private: double side; public: double getArea() { return side * side; } void setSide( double s ) { side = s; } // Overload + operator to add two Square objects. Square operator+(const Square& b) { Square square; square.side = side + b.side; return square; } Square operator-(const Square& b) { Square square; square.side = side - b.side; return square; } Square operator*(const Square& b) { Square square; square.side = side * b.side; return square; } Square operator/(const Square& b) { Square square; square.side = side / b.side; return square; } }; // Main function int main( ) { Square Square1; Square Square2; Square Square3; double area = 0.0; Square1.setSide(2.0); Square2.setSide(5.0); // Square1 area area = Square1.getArea(); cout << "Area of Square1 : " << area <<endl; // Square2 area area = Square2.getArea(); cout << "Area of Square2 : " << area <<endl; char op; cout << "Enter the operator: "; cin >> op; switch (op) { case '+': Square3 = Square1 + Square2; cout << Square3; break; case '-': Square3 = Square1 - Square2; cout << Square3; break; case '*': Square3 = Square1 * Square2; cout << Square3; break; default: cout << "No a valid operator"; } return 0; }
cout << Square3.getArea();
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
#include <iostream> using namespace std; class Square { private: double side; public: double getArea() { return side * side; } void setSide( double s ) { side = s; } // Overload + operator to add two Square objects. Square operator+(const Square& b) { Square square; square.side = side + b.side; return square; } Square operator-(const Square& b) { Square square; square.side = side - b.side; return square; } Square operator*(const Square& b) { Square square; square.side = side * b.side; return square; } Square operator/(const Square& b) { Square square; square.side = side / b.side; return square; } // This line needed to get at any private variables for a Square (e.g. side) friend ostream &operator<<( ostream &stream, Square sq ); }; // An operator to << a square ostream &operator<<( ostream &stream, Square sq ) { stream << "The user still hasn't said what he wants, so ...\n"; stream << "... HERE'S A SQUARE!!!\n"; stream << endl; stream << " *******\n"; stream << " * *\n"; stream << " * * " << "Side length: " << sq.side << endl; stream << " * *\n"; stream << " * * " << "Area : " << sq.getArea() << endl; stream << " * *\n"; stream << " *******\n"; stream << endl; return stream; } // Main function int main( ) { Square Square1; Square Square2; Square Square3; double area = 0.0; Square1.setSide(2.0); Square2.setSide(5.0); // Square1 area area = Square1.getArea(); cout << "Area of Square1 : " << area <<endl; // Square2 area area = Square2.getArea(); cout << "Area of Square2 : " << area <<endl; char op; cout << "Enter the operator: "; cin >> op; switch (op) { case '+': Square3 = Square1 + Square2; cout << Square3; break; case '-': Square3 = Square1 - Square2; cout << Square3; break; case '*': Square3 = Square1 * Square2; cout << Square3; break; default: cout << "No a valid operator"; } return 0; }