0% found this document useful (0 votes)
13 views7 pages

Understanding Constructors and Destructors

ch 5

Uploaded by

pinkudeb2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

Understanding Constructors and Destructors

ch 5

Uploaded by

pinkudeb2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Created by Ravinder kumar

chapter 5. Constructor and Destructor


What is constructor?
Constructor is a member function having same name as of its class
name.
Constructors are member functions of a class which are used to
initialize the data members of the class objects. These functions are
automatically called when an object of its class is created.
There is no need to call these functions.

Need of constructor:- constructor is needed to automatically


initializes an object as soon as it is created.
Constructor can be created both inside and outside the class.
Declaration and definition
A constructor is a member function of a class with the same name as
that of its class name. a constructor is defined like any other member
function of a class.

Types of constructor
There are three types of constructors.
1. Default Constructors
2. Parameterized Constructors
3. Copy Constructors

1. Default Constructors
A constructor that accepts no parameter is called the default
constructors.
Example

class stud
{
int sno;
char sname[40];
public :
stud( ) // Default Constructor
Created by Ravinder kumar

{
sno=123;
sname=”ravinder”;
}
void getinfo( )
{ cin>> sno;
Cin >>sname;
}
void showinfo( )
{ cout<< sno;
Cout<<sname;
}
};
The above class stud has sno and sname as data members and three
member functions i.e. stud (), getinfo(), showinfo( )
Here stud ( ) is a default constructor since having the same name as
that of class and does not accepts any argument, also declared in the
pubic section.
As we declare the object of this class it will immediately call to the
constructor of the class.It automatically assigns the value 0 to variable
sno and a “new” to sname.
Here consider the following main function
void main()
{
stud obj; // Default constructor called
Obj.showinfo();//displays the value of sno=123 sname=ravinder
Obj.getinfo(); // reads the user given value from the user
Obj.showinfo(); // displays the user given values
}

2. Parameterized Constructors
Created by Ravinder kumar

A constructor that accepts parameters for its invocation is known as


parameterized constructors.
Example

#include<iostream.h>
#include<conio.h>
class sample
{
Int a,b;
Public:
Sample(int x, int y)// parameterized constructor
{
A=x;
B=y;
}
Void display()
{
Cout<<”\n a=”<<a;
Cout<<”\n b=”<<b;
}
};
Void main()
{
Sample s(10,20);
s.display();
getch();
}

Invocation of constructors
Created by Ravinder kumar

Default constructor is invoked as soon as object is created.


Parameterized constructor for a class is called by passing arguments
to the objects.
With a parameterized constructor the initial value must be passed at
the time of object creation. This can be done in two ways:-
i) By calling the constructor implicitly (implicit call)
ii) By calling the constructor explicitly (explicit call)

1) Implicit call to the constructor:- by implicit call, it means that


the constructor is called(invoked) even when its name has not
been mentioned in the statement.
Abc obj(10,5,8);// implicit call

2) Explicit call to the constructor:- by explicit call it means that


the constructor is called by name of the constructor.
Abc obj=abc(10,5,8)// explicit call

Temporary instance:- the explicit call to a constructor also allow


you to create a temporary instance or temporary object. A
temporary instance is the one that lives in the memory as long
as it is being used. the temporary instances are anonymous. ie
the do not bear a name.

Sample(7,8).show();

3. Copy Constructors:
A copy constructor is a constructor of the form classname( &
classname). It is used to initialize an object with the values of another
object.
The compiler will use the copy constructor whenever -
 We initialize an instance using values of another instance of same
type.
 A function returns an object
 A function receives an object as parameter.
Created by Ravinder kumar

class stud
{
int sno;
char sname[40];
Public :
stud( stud &s) // Copy Constructor
{
sno=s.sno;
sname=s.name;
}
stud( ) // Default Constructor
{
sno=0;
sname=”hello”;
}
void getinfo( )
{ cin>> sno;
Cin>>sname;
}
void showinfo( )
{ cout<< sno;
Cout<<sname;
}
}
};
void main()
{
stud obj; // Default constructor called
Obj.showinfo(); // displays the value of sno
stud objnew(obj); // Copy constructor invoked
Objnew.showinfo();
Obj.getinfo(); //reads the user given value for object obj
Obj.showinfo(); // displays the user given values for object obj
Created by Ravinder kumar

Objnew.getinfo(); // reads the user given value for object objnew


Objnew.showinfo(); // displays the user given values for object
}

Dynamic initialization of objects:-


The dynamic initialization means that the initial values may be
provided during runtime.

class college
{
int a,b;
public:
college( int x,int y)
{
a=x;
b=y;
}
void show()
{
cout<<”\n a=”<<a;
cout<<”\n b=”<<b;
}
};
void main()
{
int p,q;
cout<<”enter any 2 numbers”;
cin>>p>>q;
college s(p,q);
s.show();
getch();
}

Characteristics of constructors:
Created by Ravinder kumar

 The name of a constructor is same as that of class in which it is


declared.
 Constructors do not have any return type, not even void.
 Constructors are always defined in the public section of the class.
 They cannot be inherited, though a derived class can call the base
class constructor.
 A constructor may not be static.
 Like other C++ functions, constructors can also have default
arguments.
 It is not possible to take the address of a constructor.

Constructor overloading:-
Constructor can also be overloaded.

DESTRUCTOR:
It is a member function having same name as of its class but
preceded by a tilde(~) sign.
Characteristic of destructor
It is used to destruct the object created by a constructor.
There is only one destructor for a class.
It cannot be overloaded.
They cannot be inherited.
A destructor may not be static.

You might also like