Constructors:
Constructor is used for Initializing the values to the data members of the Class.
2) Constructor is that whose name is same as name of class.
3) Constructor gets Automatically called when an object of class is created.
4) Constructors never have a Return Type even void.
5) Constructor are of Default , Parameterized and Copy Constructors. Constructor is used for Initializing the values to the data members of the Class.
2) Constructor is that whose name is same as name of class.
3) Constructor gets Automatically called when an object of class is created.
4) Constructors never have a Return Type even void.
5) Constructor are of Default , Parameterized and Copy Constructors. Constructor is used for Initializing the values to the data members of the Class.
>> The various types of Constructor are as follows:-
1. Default Constructor:-
Default Constructor is also called as Empty Constructor which has no
arguments and It is Automatically called when we creates the object of
class but Remember name of Constructor is same as name of class and
Constructor never declared with the help of Return Type. Means we cant
Declare a Constructor with the help of void Return Type. , if we never
Pass or Declare any Arguments then this called as the Copy Constructors.
2. Parameterized Constructor :-
This is Another type Constructor which has some Arguments and same
name as class name but it uses some Arguments So For this We have to
create object of Class by passing some Arguments at the time of creating
object with the name of class. When we pass some Arguments to the
Constructor then this will automatically pass the Arguments to the
Constructor and the values will retrieve by the Respective Data Members
of the Class.
***********************************************************************************
Simple Example is as:
#include<iostream.h>
#include<conio.h>
class Mxg // Class with name "Mxg"
{
public:
Mxg( ) // Constructor
{
cout<<"This is a Constructor. It Runs when Object is Created"<<endl;
}
}; // Class Ends
void main()
{
clrscr();
Mxg a,b,c; // a,b and c are Objects of Class Mxg.
getch();
}
0 comments: