Thursday, March 28, 2013

Constructor, Types and a Simple Example

By Unknown   Posted at  2:18 AM   C++ Programming No comments

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.
 
3.                Copy Constructor:- This is also Another type of Constructor. In this Constructor we pass the object of class into the Another Object of Same Class. As name Suggests you Copy, means Copy the values of one Object into the another Object of Class .This is used for Copying the values of class object into an another object of class So we call them as Copy Constructor and For Copying the values We have to pass the name of object whose values we wants to Copying and When we are using or passing an Object to a Constructor then we must have to use the & Ampersand or Address Operator
***********************************************************************************

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();
}


 

About the Author

Nulla sagittis convallis arcu. Sed sed nunc. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.
View all posts by: BT9

0 comments:

Back to top ↑
Connect with Us

    What's Trending in Videoism

    Total Pageviews

    Copy of any Post is Not Allowed. All Rights Reserved with Matee ur Rehman (Author). Powered by Blogger.

    Advertisements

    Follow us on FaceBook

What they says

© 2013 GC Study Timez. WP Daily Quranand Hadith Converted by Matee Gojra
Blogger templates. Proudly Powered by Blogger.