Defualt Copy Constructors in C++


Defualt Copy constructor

A no argument constructor can initialize data members to constant values ,and a multi argument constructor can initialize data members to values passed as arguments.    you can initialize it with another object of the same type. Surprisingly , you dont need to creat a special constructor for this ; one is already built into all classes.it is called defualt copy constructor.
It 's a one argument constructor whose argument is an object of the same class as the constructor.This example tells us how to use this constructor.
It will look like

in main

rational r1(3,5);
rational r2(r1);
rational r3 = r1;




Defualt Copy Constructor


#include <iostream>
using namespace std;
class Distance
{
private:
            int feet;
            float inches;
public:
            Distance():feet(0),inches(0.0)
            {}
            Distance(int f,float i):feet(f),inches(0.0)
            {}
            void get()
            {
                        cout<<"enter feet";
                        cin>>feet;
                        cout<<"enter inches";
                        cin>>inches;
            }
            void print()
            {
                        cout<<feet <<" /"<< inches;
            }
};

void main ()
{
            Distance d1(12,7.3);
            Distance d2(d1);                // one argument constructor
            Distance d3 =d1;               // also one argument constructor
            d1.print();
            d2.print();
            d3.print();
}