Friday, 30 March 2018

Constructor

A constructor is a member function whose task is to allocate the memory and initialize the object of the class.

It's special member function because its name and class name is name.

The constructor in invoked whenever an object of its associated class is created.

It is called as constructor because its construct the value of data member of the class.

Constructor function should be declared in public section.

Constructor do not have return , not even void so it can not return any type of value.

Constructor function can not be inherited but derived class can the base class constructor.

Constructor can not have default argument.

Constructor can not be virtual.

Constructor make implicit calls to the operators new and delete when memory allocation is required.




When you declare the object as follows
time t1;

Then object will get initialized automatically that is data members hours and minutes will be initialized to a value zero.

Therefore there is no need to write any statement to invoke the constructor function.

Different Types of Constructor
  1. Default Constructor
  2. Parameterized Constructor
  3. Copy Constructor
  4. Dynamic Constructor

Default Constructor :-
 A constructor that accepts no arguments is called as default constructor.

The default constructor for class Emp is
   Emp::Emp( )

 If no such a constructor is defined then compiler supplies a default constructor.

Statement Emp x;
 Invokes default constructor while creating object 'x';

Ex:
 #include<conio.h>
 #include<iosteam.h>
 class sample
 {
 int k;
 public:
 sample( )
 {
  k=0;
  }
  void display( )
  {
  cout<<"\n\tValue of k is --->"<<k;
   }
  };
  void main( )
   {
    sample obj1;
    clrscr( );
    obj1.display( );
     getch( );
    }

Output:-
   Value of k is --->0

Parameterized Constructor :

 A constructor with arguments / parameters is called as parameterized constructor.
 As we know that each object has separate memory for data members sometime.It becomes necessary to initialize each object with different values when they are created.

Then these arguments help to initialize an object when it is created.
 To create parameterized constructor, simply add parameters to it as you add to any function.

Write a program to declare class student having data members name and percentage. Write a constructor to initialize these data members and display them.

#include<iostream.h>
#include<conio.h>
#include<string.h>
class student
{
 private:
 char name[20];
 float per;
 public:
 student(char n[20],float p)
 {
  strcpy(name,n)
  per=p;
  }
  void display( )
  {
   cout<<"\n\tName is --->"<<name;
   cout<<"\n\tPercentage is --->"<<per;
   }
  };
 void main( )
  {
   student s1("Irawen",99.92);
   s1.display( );
   getch( );
 }

Output:-
   Name is --->Irawen
   Percentage is --->99.92

Copy Constructor 
   A constructor can accept a reference to its own class as a parameter which is called as copy constructor.
   A copy constructor takes a reference to an object of the same class as itself as an argument.
   A copy constructor can be used to declare and initialize an object of another object.

Ex:
 Class Time
 {
 ..........
 public:
 Time(Time &t)  //Copy Constructor
  {
   }
 };
→ Time T2(T1);
      It define the object t2 and at the same time initialize it to have of t1.
→ Statements T2=T1;
      This will not invoke copy constructor.If T1 and T2 are the objects,this statement is legal and simply assigns the value of T1 and T2 member by member.

Write a program to implement copy constructor

#include<iostream.h>
#include<conio.h>
class code
{
 int id;
 public:
 code(int a)
 {
  id=a;
  }
  code(code &x)
  {
   id=x.id;
   }
   void display( )
   {
    cout<<"\t"<<id;
    }
   };
  void main( )
   {
    code A(100);
    code B(A);
    code C=A;
    clrscr( );
    cout<<"\n\t ID of A is --->";
    A.display( );
    cout<<"\n\t ID of B is --->";
    B.display( );
    cout<<\n\t ID of C is --->";
    C.display( );
     getch( );
   }

Output:-
   ID of A is --->100
   ID of B is --->100
   ID of C is --->100

1 comment:

Popular Posts

Categories

Android (21) AngularJS (1) Books (3) C (75) C++ (81) Data Strucures (4) Engineering (13) FPL (17) HTML&CSS (38) IS (25) Java (85) PHP (20) Python (85) R (69) Selenium Webdriver (2) Software (13) SQL (27)