Sunday, October 30, 2016

CONSTRUCTORS AND TYPES


C++ Provides a special member function called constructor that enables an object to initialize itself when it is created.This is know as automatic initialization of objects.

it special function because its name is same as the class name.The constructor is invoked whenever an object of its associated class is created.
A Constructor is declared and defined as follows :


                                                      class student
                                                       {
                                                       private:
                                                                   int roll no;
                                                                   int marks;
                                                       public:
                                                                    student( )             // default constructor defined
                                                                     {
                                                                             roll no=0;
                                                                             marks=0;
                                                                      }
                                                        };
                                                         void main( )
                                                          {
                                                                 student s;                  // object s created
                                                           }
                                                   

                         in this example, the declaration
                                                                             
                                                                               student s;
 not only created the object s of type student but also initialize its data members roll no,marks to zero.

characteristics of a constructor


* A constructor name must be the same as that of its class name.
* it is declared with no return type(not even void)
* it is declared in public section of the class.
* it can have default arguments.
* it can not refer to its own address.
* it may not be static.
* it may not be virtual.
* it can not be inherited.


types of constructors

there are three types of constructors 


1. default constructors

2. parameterized constructors

3. copy constructors

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 Constructor.

/*  Example Program For Simple Example Program Of default Constructor In C++

 #include <iostream.h>
 #include<conio.h>
       class Defal
           {
            public:

            int x;    // Variable Declaration

            int y;

            Defal(){x=y=0;}    //default constructor
                           
          };

          int main()
           {
           Defal A;

            cout << "Default constructs x,y value::"<<

            A.x <<" , "<< A.y << "\n";

            return 0;
         }

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.

/*  Example Program For Simple Example Program Of Parameterized Constructor In C++   

    Coded By:abglobe.blogspot.com             */

#include<iostream>
#include<conio.h>

using namespace std;

class Example        {
   
int a,b;  // Variable Declaration
    public:

    
    Example(int x,int y)  //Constructor           
    {
    
    a=x;       // Assign Values In Constructor

    b=y;

    cout<<"Im Constructor\n";
    }

    void Display()
    {

    cout<<"Values :"<<a<<"\t"<<b;
    
    }

  };

       int main()        
        {

           Example Object(10,20);
        
           Object.Display();   // Constructor invoked

           getch();  // Wait For Output Screen
           
           return 0;
         
         }


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.

/*  Example Program For Simple Example Program Of Copy Constructor Overloading In C++

    Coded By:abglobe.blogspot.com             */

#include<iostream>
#include<conio.h>

using namespace std;

class Example        
{
    
    int a,b;  // Variable Declaration
    public:

    Example(int x,int y)   //Constructor with Argument
     {
    
    a=x;     // Assign Values In Constructor

    b=y;

    cout<<"\nIm Constructor";

    }

    void Display() 
    {

    cout<<"\nValues :"<<a<<"\t"<<b;

    }
 };

int main() 
               {
    Example Object(10,20);

       
        Example Object2=Object;   //Copy Constructor

        

        Object.Display();     // Constructor invoked.

        Object2.Display();

        getch();   // Wait For Output Screen

        return 0;
}

thanks for watching!!!!!!

any problem please comment @

No comments:

Post a Comment