Monday, 21 August 2017

CONSTRUCTORS

class student
 {
  private:
             int rollno;
            char name[20];
            float marks;
 public:
            student( )                          // Default Constructor
            { rollno=0; marks=0.0; }
            student(int roll, char nam[20], float mar)   // Parametrized Constructor
            { rollno=roll;
            strcpy(name, nam);
            marks =mar;
            }          

student( student & s)   // Copy Constructor
            { rollno= s.rollno;
            strcpy(name, s.name);
            marks =s.marks;
            }
}
void main()
{
student s1; //statement1
student s2( 1, “RAJESH”, 450); // statemet2
student s3(s2);    //statement3
student s4(2, “MOHIT”,320); //statement4
Student s5=s4;      //statement5
}

Explanation:
Statement1: default constructor is called
Statement2: parametrized constructor is called
Statement 3: copy constructor is called
statement4: Parametrized constructor is called
statement5: Copy constructor is called

No comments:

Post a Comment