16066_constructors & destructors
TRANSCRIPT
-
7/29/2019 16066_Constructors & Destructors
1/28
-
7/29/2019 16066_Constructors & Destructors
2/28
Constructors
(c++ allows objects to initializethemselves when they are
created. this automaticinitialization is performed through
the use of a constructor
function.)
-
7/29/2019 16066_Constructors & Destructors
3/28
Constructor is a special memberfunction having same name as that of
its class which is used to initializesome valid values to the membervariables.
-
7/29/2019 16066_Constructors & Destructors
4/28
A constructor has same name as that of theclass to which it belongs.
A constructor is executed automaticallywhenever the object is created
A constructor doesn`t have a return type, noteven void
We can declare more than one constructor in aclass. These constructor differ in thereparameter list.
If you dont provide a constructor of your ownthen the compiler generates a default
constructor(expects no parameters and hasan empty body).
A constructor can preferably be used forinitialization and not for input\output operations.
-
7/29/2019 16066_Constructors & Destructors
5/28
Constructor should be declared in thepublic section of the class. If it is not
declared in public section of the classthen the whole class become private .By doing this the object of the classcreated from outside cannot invoke the
constructor which is the first memberfunction to be executed automatically.
-
7/29/2019 16066_Constructors & Destructors
6/28
class CLASSNAME
{
public:CLASSNAME([parameter list]);
};
-
7/29/2019 16066_Constructors & Destructors
7/28
#include
#include
class Rectangle
{private:
int length,breadth;
public:
Rectangle()
{
Length=5,breadth=6;
}
-
7/29/2019 16066_Constructors & Destructors
8/28
int area()
{
int a=(length*breadth);
cout
-
7/29/2019 16066_Constructors & Destructors
9/28
class integer{
int n, m;public:
integer(void);....
};
integer : : integer(void){m=0;n=0;
}
-
7/29/2019 16066_Constructors & Destructors
10/28
In order to initialize various dataelements of different objects with
different values when they are created.C++ permits us to achieve this objectsby passing argument to the constructorfunction when the object are created .
The constructor that can takearguments are calledparameterizedconstructors
-
7/29/2019 16066_Constructors & Destructors
11/28
class abc{int m, n;public:abc (int x, int y); // parameterizedconstructor................
.................};abc : : abc (int x, int y){m = x;
n = y;
}
-
7/29/2019 16066_Constructors & Destructors
12/28
#includeclass Rectangle
{private:
int length,breadth;
public:
Rectangle(int a,int b){
length=a,breadth=b;
}
-
7/29/2019 16066_Constructors & Destructors
13/28
int area()
{
int a=(length*breadth);
cout
-
7/29/2019 16066_Constructors & Destructors
14/28
class rectangle {private:float height;float width;int xpos;int ypos;
public:rectangle() { xpos = 0; ypos = 0; }rectangle(float, float); // constructorvoid draw(); // draw member functionvoid posn(int, int); // position member functionvoid move(int, int); // move member function
};
rectangle::rectangle(float h, float w){height = h;width = w;
}
-
7/29/2019 16066_Constructors & Destructors
15/28
A constructor which is used to initializethe value of an object by copying
values of another object of the sameclass.
Syntax
Class name(constructor name &);
-
7/29/2019 16066_Constructors & Destructors
16/28
A constructor is a constructor thatcreates a new object using an existing
object of the same class and initializeseach data member of newly createdobject with corresponding data memberof existing object passed as
argumnt.since it creates a copy of anexisting object so it is called copyconstructor.
-
7/29/2019 16066_Constructors & Destructors
17/28
Class counter
{
Int c;
public:
Counter(int a){
C=a;
}
Counter(counter &ob)
{cout
-
7/29/2019 16066_Constructors & Destructors
18/28
Void show()
{
cout
-
7/29/2019 16066_Constructors & Destructors
19/28
#includeClass code{
int id;Public:code(){}code(int a)
{id = a;}code (code & x){id = x. id;
}void display(void){cout
-
7/29/2019 16066_Constructors & Destructors
20/28
#include
#include
class abc
{
int rn;float fee;
public:
abc(int a,float b)
{
rn=a;fee=b;
couty;
class abc m1(x,y);
getch();
}
-
7/29/2019 16066_Constructors & Destructors
21/28
Is a member function having
same name as that of constructor
but it is preceded by a tilde(~)symbol and is executedautomatically when object of a
class is destroyed
-
7/29/2019 16066_Constructors & Destructors
22/28
A destructor has same name as that of the class to which itbelongs preceded by tilde(~)sign.
A destructor is executed automatically whenever the objectis destroyed.
A destructor doesn`t have a return type, not even void andno arguments
There is only one destructor in class .
If you dont provide a destructor of your own then thecompiler generates a default destructor
A destructor can be used to deallocate memory for anobject and declared in the public section.
-
7/29/2019 16066_Constructors & Destructors
23/28
A destructor function is calledautomatically when the object goes out ofscope:
(1) the function ends(2) the program ends(3) a block containing temporary variables ends(4) a delete operator is called
-
7/29/2019 16066_Constructors & Destructors
24/28
Need for Destructors
To de-initialize the objects when they are
destroyed To clear memory space occupied by a
data member.
-
7/29/2019 16066_Constructors & Destructors
25/28
class CLASSNAME
{
.public:
~CLASSNAME([parameter list]);
};
-
7/29/2019 16066_Constructors & Destructors
26/28
#include
#includeclass counter
{
int id;public:
counter(int i)
{Id=I;
cout
-
7/29/2019 16066_Constructors & Destructors
27/28
~counter()
{
cout
-
7/29/2019 16066_Constructors & Destructors
28/28
Output
constructor of object with id=1
constructor of object with id=2constructor of object with id=3
End of main
destructor with id=3destructor with id=2
destructor with id=1