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

0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (114) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (88) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (741) Python Coding Challenge (190) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses