Contents
oIntroduction
oCharacteristics of Constructor.
oTypes of constructor.
- Default Constructor
- Parameterized Const...
Concept of constructor
In C++, a constructor is a ‘special’ member function whose task is
to initialize the objects of it...
Characteristics of Constructor
They should be declared in the public section.
They are invoked automatically when the ob...
Program to explain concept of
constructor
#include<iostream.h>
#include<conio.h>
class stud
{
int roll_no;
char name[20];
...
Types of constructor

Constructor

1.Default

2.Parameterized

3.Copy
1.Default Constructor
 A constructor that accepts no parameters is called the default
constructor.
If no such constructo...
Parameterized constructor
The constructors that can take arguments are as shown
bellow:
class integer
{
int m,n;
public:
...
Calling to Constructor
By calling the constructor explicitly
integer int1=integer(0,100);//explicit call
This statement c...
The constructor functions can also be defined as inline
functions,
e.g.
class a
e.g.
{
class integer
------{
------int m,n...
Copy Constructor
- However

a constructor can accept a reference to it’s own
class as a parameter, in such cases, the cons...
Program to explain copy constructor
# include<iostream.h>
#include<conio.h>
class integer
{
int x;
public:
integer(int a)
...
Overloaded Constructors: Multiple Constructors in a class
- We are used two kinds of
constructors, they are:
1. integer();...
Program shows the use of overloaded constructors
#include<iostream.h>
class integer
{
int m,n;
public:
integer()
{
m=0;
n=...
Constructors with default arguments
- It is possible to define constructors with
default arguments.
- For example, the con...
Advantages of constructor
1. Constructor is invoked automatically as soon as the object of that
class is created this conc...
Destructors
- A destructor, as the name implies, is used to destroy the objects that
have been created by a constructor.
-...
Difference Between Constructor & Destructor
Constructor

Destructor

1.Constructor is a special
member function whose task...
Constructor and desturctor
Upcoming SlideShare
Loading in …5
×

Constructor and desturctor

1,714 views

Published on

Published in: Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,714
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
164
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Constructor and desturctor

  1. 1. Contents oIntroduction oCharacteristics of Constructor. oTypes of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor oOverloaded Constructor -Multiple Constructor in a class -Constructor with default argument oDestructor oDifference between constructor & destructor. oConclusion
  2. 2. Concept of constructor In C++, a constructor is a ‘special’ member function whose task is to initialize the objects of it’s class when it is created. It is special because it’s name is the same as the class name. It is called constructor because it constructs the value of data members of the class. The constructor is invoked whenever an object of it’s associated class is created.
  3. 3. Characteristics of Constructor They should be declared in the public section. They are invoked automatically when the objects are created. Do not have return types. Cannot be virtual . Cannot be inherited through a derived class can call the base class constructor.
  4. 4. Program to explain concept of constructor #include<iostream.h> #include<conio.h> class stud { int roll_no; char name[20]; public: stud(); void show() { cout<<“roll_no”<<roll_no<<endl; cout<<“name”<<name<<endl; } }; stude::stud() { cout<<“enter roll_no”<<endl; cin>>roll_no; cout<<“enter name”<<endl; cin>>name; } void main() { stud s; clrscr(); s.show(); getch(); }
  5. 5. Types of constructor Constructor 1.Default 2.Parameterized 3.Copy
  6. 6. 1.Default Constructor  A constructor that accepts no parameters is called the default constructor. If no such constructor is defined , then the compiler supplies a default constructor. E.g. #include<iostream.h> class integer { int x; public: integer(); }; integer::integer() { x= 10; } void main() { integer int 1; getch(); }
  7. 7. Parameterized constructor The constructors that can take arguments are as shown bellow: class integer { int m,n; public: integer(intx,inty);//parameterized constructor ------------------------------}; integer::integer(int x,int y) { m=x; n=y; }
  8. 8. Calling to Constructor By calling the constructor explicitly integer int1=integer(0,100);//explicit call This statement creates an integer object int1 and passes the values 0 and 100 to it. By calling the constructor implicitly integer int1(0,100);//implicit call.
  9. 9. The constructor functions can also be defined as inline functions, e.g. class a e.g. { class integer ------{ ------int m,n; public: public: a(a); integer(int x,int y) }; { it is illegal m=x; class a n=y; { } -- - - - ----------------- - - - ---------------public: }; a(a&); }; is valid
  10. 10. Copy Constructor - However a constructor can accept a reference to it’s own class as a parameter, in such cases, the constructor is called the copy constructor. Syntax: class integer { int x; public: integer(integer &i) { x=i.x; } -- - - };
  11. 11. Program to explain copy constructor # include<iostream.h> #include<conio.h> class integer { int x; public: integer(int a) { x=a; } integer(integer &i) { x=i.x; } void show() { cout<<“x=”<<x<<endl; } }; void main() { integer i1(100); integer i2(i1); integer i3=i1 integer i4; i4=i1; // it is not copy constr cout<<“i1”; i1.show(); cout<<“i2”; i2.show(); cout<<“i3”; i3.show(); getch(); }
  12. 12. Overloaded Constructors: Multiple Constructors in a class - We are used two kinds of constructors, they are: 1. integer(); //no arguments 2. Integer(int,int); //two arguments - e.g class integer { int m,n; public: integer() // constructor 1 { m=0; n=0; } integer(int a,int b) //constructor 2 { m=a; n=b; } integer(integer &i) //constructor3 { m=i.m; n=i.n; } };
  13. 13. Program shows the use of overloaded constructors #include<iostream.h> class integer { int m,n; public: integer() { m=0; n=0; } integer(int a,int b) { m=a; n=b; } integer(ineger &x) { m=x.m; n=x.n; } }; void main() { integer i1; integer i2(30,60); integer i3(i2); getch(); }
  14. 14. Constructors with default arguments - It is possible to define constructors with default arguments. - For example, the constructor complex() can be declared as follows: complex(float teal, float imag=0); - e.g. program #include<iostream.h> #include<conio.h> class student { int roll_no; float marks; public: student(int x=10;float y=40) { roll_no=x; marks=y; } void display() { cout<<“roll no”<<roll_no<<endl; cout<<“marks”<<marks<<endl; } }; void main() { student s1; student s2(1,60); clrscr(); cout<<“output using default constructor argumets:n”; s1.display(); cout<<“output without default constructor arguments:n”; s2.display(); getch(); }
  15. 15. Advantages of constructor 1. Constructor is invoked automatically as soon as the object of that class is created this concept is also known as automatic initialization of objects. 2. A key benefit of using a constructor is that it guarantees that the object will go through proper initialization before being used. 3. When a user instantiates an object, that object’s constructor is called and must return before the user can perform any other work with that object. 4. The main use and advantage of constructors is to initialize objects.
  16. 16. Destructors - A destructor, as the name implies, is used to destroy the objects that have been created by a constructor. - Like a constructor, the destructor is a member function whose name is the same as the class name but is precede by a tilde ( ~ ). - E.g. ~integer() { ----------------------------} - Destructor never take any arguments. - The object is destroyed when end of scope of program.
  17. 17. Difference Between Constructor & Destructor Constructor Destructor 1.Constructor is a special member function whose task is to initialize the object’s of it’s class. 1.Destructor is used to destroy objects that have been created by a constructor. 2. It’s name is same as class name . 2. it’s name is same as class name but is preceded by tilde. 3.Constructor is invoked when the object of it’s associated class is created. 3.It is invoked implicitly by the compiler upon exit of the program to clean up the storage that is no longer accessible. 4.Constructor can take arguments. 4. Destructor does not take any arguments. 5.e.g. student(int a,int b) 5. e.g. ~student()

×