thespeedracer38

Height using Operator Overloading

Jan 28th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. /** Height program has been edited to facilitate
  2.     adding two objects of Height class using operator
  3.     overloading. This is done using an overloaded implementation of
  4.     + which is a member function of Height
  5. */
  6.  
  7. #include <iostream>
  8. #include <cstdlib>
  9.  
  10. using namespace std;
  11.  
  12.  
  13. class Height{
  14.     int meter, centi_meter;
  15. public:
  16.     inline Height();
  17.     inline Height(int ,int);
  18.     inline Height(const Height&);
  19.     inline ~Height();
  20.     inline void input();
  21.     inline void display();
  22.     inline Height operator +(Height&);
  23. };
  24.  
  25. Height::Height(){
  26.     meter = 0;
  27.     centi_meter = 0;
  28. }
  29.  
  30. Height::Height(int meter, int centi_meter){
  31.     Height::meter = meter;
  32.     Height::centi_meter = centi_meter;
  33. }
  34.  
  35. Height::Height(const Height &copy){
  36.     meter = copy.meter;
  37.     centi_meter = copy.centi_meter;
  38. }
  39.  
  40. Height::~Height(){
  41.    
  42. }
  43.  
  44. Height Height::operator +(Height &height){
  45.     int c_m = (centi_meter + height.centi_meter) % 100;
  46.     int m = (meter + height.meter) + ((centi_meter + height.centi_meter) / 100);
  47.     return Height(m, c_m);
  48. }
  49.  
  50. void Height::input(){
  51.     cout << "Meter = ";
  52.     cin >> meter;
  53.     cout << "Centi Meter = ";
  54.     cin >> centi_meter;
  55. }
  56.  
  57. void Height::display(){
  58.     cout << meter << "m " << centi_meter << "cm" << endl;
  59. }
  60.  
  61. int main() {
  62.     system("cls");
  63.     Height obj1, obj2;
  64.     cout << "Enter the first height" << endl;
  65.     obj1.input();
  66.     cout << "Enter the second height" << endl;
  67.     obj2.input();
  68.     Height result = obj1 + obj2;
  69.     cout << " ";
  70.     obj1.display();
  71.     cout << "+";
  72.     obj2.display();
  73.     cout << "=";
  74.     result.display();
  75.     cin.ignore();
  76.     cin.get();
  77.     return 0;
  78. }
Add Comment
Please, Sign In to add comment