#include <iostream>
using namespace std;
class Distance
{
private:
int feet;
int inch;
public:
Distance (); //Constructor
void getDist ();
void showDist ();
Distance addDist( Distance d2 );
//declaration for parameterized constructor
Distance(int pfeet, int pinch);
//declaration for copy constructor
Distance(const Distance& obj);
};
//use constructor initializer list to initialize feet and inch
Distance:: Distance(): feet(0), inch(0)
{
//no need to assign here
}
//define the parameterized constructor
Distance::Distance(int pfeet, int pinch): feet(pfeet), inch(pinch)
{
}
//define the copy constructor
Distance::Distance(const Distance& obj): feet(obj.feet), inch(obj.inch)
{
}
void Distance:: getDist()
{
cout << "Enter Value of feets : "; cin >> feet;
cout << "Enter value of inches : "; cin >> inch;
inch = (inch >= 12) ? 12 : inch;
}
void Distance:: showDist()
{
cout << endl << "\tFeets : " << feet;
cout << endl << "\tInches: " << inch;
}
Distance Distance:: addDist( Distance d2 )
{
Distance temp;
temp.feet = feet + d2.feet;
temp.inch = inch + d2.inch;
if( temp.inch >= 12)
{
temp.feet++;
temp.inch -= 12;
}
return temp;
}
int main()
{
Distance d1;
Distance d2;
Distance d3;
Distance d4;
d4 = d3;
cout << "Enter Distance 1 : " << endl;
d1.getDist();
cout << endl << "Enter Distance2 : " << endl;
d2.getDist();
d3 = d1.addDist(d2);
cout << endl << "Distance 1 : " ;
d1.showDist();
cout << endl << "Distance 2 : " ;
d2.showDist();
cout << endl << "Distance 3 = Distance 1 and Distance 2 :" ;
d3.showDist();
d4 = d3;
cout << endl << "Distance 4 Copy constructor allocating ptr." ;
d4.showDist();
cout << endl;
return 0;
}