C++ Constructor Initialize objects using default copy constructor

Description

C++ Constructor Initialize objects using default copy constructor

#include <iostream>
using namespace std;
class Measure// w w w.j ava  2 s .c om
{
   private:
   int feet;
   float inches;
   public:
   Measure() : feet(0), inches(0.0){}
   //Note: no one-arg constructor
   Measure(int ft, float in)  : feet(ft), inches(in){}
   void getdist()              //get length from user
   {
      cout << "\nEnter feet: ";  cin >> feet;
      cout << "Enter inches: ";  cin >> inches;
   }
   void showdist()
   { 
       cout << feet << "\'-" << inches << '\"'; 
   }
};
int main()
{
   Measure dist1(11, 6.25);
   Measure dist2(dist1);
   Measure dist3 = dist1;
   cout << "\ndist1 = ";  dist1.showdist();
   cout << "\ndist2 = ";  dist2.showdist();
   cout << "\ndist3 = ";  dist3.showdist();
   cout << endl;
   return 0;
}



PreviousNext

Related