CONSTRUCTOR


CONSTRUCTOR



Constructor, Types of Constructor, Default Constructor, Para-meterized Constructor Copy Constructor, Constructor overloading, Destructor

CONSTURCTOR

It is a member function having same name as it’s class and which is used to initialize the objects of that class type with a legel initial value. Constructor is automatically called when object is created.

Types of Constructor

Default Constructor-: A constructor that accepts no parameters is known as default constructor. If no constructor is defined then the compiler supplies a default constructor.
student :: student()
{
     rollno=0; 
}
Parameterized Constructor -: A constructor that receives rguments/parameters, is called parameterized constructor.

student :: student(int r)
{
     rollno=r; 
}
Copy Constructor-: A constructor that initializes an object using values of another object passed to it as parameter, is called copy constructor. It creates the copy of the passed object.

student :: student(student &s)
{
     rollno = s.rollno; 
}
There can be multiple constructors of the same class, provided they have different signatures.

DESTRUCTOR

A destructor is a member function having sane name as that of its class preceded by ~(tilde) sign and which is used to destroy the objects that have been created by a constructor. It gets invoked when an object’s scope is over.

~student() { }

No comments:

Post a Comment