0% found this document useful (0 votes)
17 views32 pages

Constructor and Destructor

The document provides an overview of constructors in C++, detailing their characteristics, types, and examples of usage. It explains concepts such as default constructors, parametric constructors, constructor overloading, and copy constructors, along with function overloading principles. Additionally, it introduces destructors, highlighting their role in object destruction and their characteristics.

Uploaded by

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

Constructor and Destructor

The document provides an overview of constructors in C++, detailing their characteristics, types, and examples of usage. It explains concepts such as default constructors, parametric constructors, constructor overloading, and copy constructors, along with function overloading principles. Additionally, it introduces destructors, highlighting their role in object destruction and their characteristics.

Uploaded by

Abdul Wahab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Object

Oriented
Programming
Using

C ++
Abdul Wahab abdul_bu@yahoo.com
Lecturer
University of Science and Technology Bannu
1
Constructor

 A constructor is a special member function that is automatically called when an object


is created.

 Constructor name is distinguish from other member functions because its name is
same as the class they belong to.

 For global objects, the constructor is called when the program begins execution, prior
to the call to main().

2
Characteristics of Constructor
 Constructor has the same name as that of the class it belongs

 Constructor is executed automatically when an object is declared.

 Constructor have neither return value nor void.

 The main function of constructor is to initialize objects and allocate appropriate memory
to objects.

 Constructor can have default values and can be overloaded.

 The constructor without arguments is called as default constructor.

3
Programming Example
void show()
class Num { {
private: cout<<“\n
int a, b; X=”<<x<<“A=”<<a<< “B=”<<b;
public: }
int x; };
Num()
{ void main ()
cout<<“\n Constructor Called ”; {
x=5; a=0; b=1; clrscr();
} Num obj;
… obj.show();
}

4
Output

Constructor Called
X= 5 A= 0 B= 1

5
Constructor (Contd…)
For Each Object Constructor is Called Separately

class Num{ void main()


int a; {
public; clrscr();
Num(); Num x, y, z;
}; Num a[2]; /// Array of Objects
Num :: Num() getch();
{ }
cout<<“\n Constructor Called”;
a=1;
cout<<“ A = ”<<a;
}

6
Output

Constructor Called A=1


Constructor Called A=1
Constructor Called A=1
Constructor Called A=1
Constructor Called A=1

7
Constructor (Contd…)
Reading Values from Keyboard Using Constructor

class Num{
int a, b, c; void main()
public: {
Num() clrscr();
{ Num x;
cout<<“\n Constructor Called”;
x.show();
cout<<“”\n Enter Values for A, B C : “;
cin>> a >> b >> c; getch();
} }
void show()
{
cout<< “\n A=”<< a <<“ B=”<< b <<“ C=”<< c;
}
};

8
Output

Constructor Called
Enter Values for A, B, C : 1 2 4
A=1 B=2 C=4

9
Constructor with Arguments
 Normally constructors are used to initialize member variables

 It is also possible to create constructor with arguments called “Parametric Constructor”

 For such constructors, it is necessary to pass values to the constructor when object is
created.

10
Constructor (Contd…)

class Num{
int a, b, c; void main()
public: {
Num( int m, int j, int k) clrscr();
{ Num x=Num(4, 5, 6); //Explicit Call
a=m; b=j; c=k; Num y(1, 2, 3);
} x.show();
void show() y.show();
{ getch();
cout<< “\n A=”<< a <<“ B=”<< b << }
“ C=”<< c;
}
};

11
Output

A=4 B=5 C=6


A=1 B=2 C=3

12
Function Overloading

 Defining multiple functions with same name is known as function overloading or function
polymorphism.

 The overloaded function must be different in its argument list and with different data
types.

13
Example:

void main() int Sqr(int s)


{ {
clrscr(); return (s * s);
int a=15; }
float b=2.5;

cout<<“Square = “<<Sqr(a); float Sqr(double j)


cout<<“\nSquare = ”<<Sqr(b); {
return (j * j);
getch(); }
}

14
Output

Square = 225
Square = 6.25

15
Example:

int Area (int length, int bredth) void main()


{ {
return (length * bredth); int area1= Area(10,20);
} float area2= Area(4.5, 2.1);
double area3= Area(3.12);
float Area (float base,float height)
{ cout<<“Area of Rectangle= ”<<area1;
return (0.5 * base * height); cout<<“\nArea of Triangle= ”<<area2;
} cout<<“\nArea of Circle= ”<<area3;

float Area (double radius) getch();


{ }
return (3.142 * radius * radius);
}

16
Principles Function Overloading

 If two functions have the similar type and number of arguments, the function can not be
overloaded. The return type may be similar or void , but argument data type or number of
arguments must be different. Foe example:
Sum (int, int, int);
Sum(int, int);
Sum(float, float);

 If there is no accurate match found, compiler makes the implicit conversion of actual
arguments. For example:
char is converted into int
float is converted into double

17
Example:

void main() int Sqr(int s)


{ {
clrscr(); return (s * s);
int A=15; }
float B=3.5;

cout<<“Square = “<<Sqr(„A‟); float Sqr(double j)


cout<<“\nSquare = ”<<Sqr(B); {
return (j * j);
getch(); }
}

18
Output

Square = 4225
Square = 12.25

19
Principles Function Overloading

 Sometime while making internal conversion, ambiguity is created if one data type is
compatible two or more data types. In such situations, the compiler displays an error
massage. For example:
long Sqr(long);
double Sqr(double);

Sqr(10); //Ambiguity between „Sqr(long)‟ and „Sqr(double)‟

 If a program has two versions of functions i.e. one for float and second for double data
type, then if we pass a float number, the double version of function is selected for
execution.
But the same is not applicable with integer and long integer, appropriate version is
selected for execution, based on range of the value passed to the function

20
Principles Function Overloading

 Only those functions should be overloaded, that basically do the same task.

 Instead of function overloading, using default arguments may make sense and fewer
overheads.

 Declare function prototypes before main() function and pass variables instead of passing
constant directly. This will avoid ambiguity.

21
Constructor Overloading
 It is possible to declare more than one constructor for a class. Differentiating them by list
of arguments.

 This is known as Constructor Overloading.

 All constructors are defined with same name as the class.

 Depending upon number of arguments, the compiler executes appropriate constructor.

22
Example
class Num{
private:
int a;
float b;
char c;
public:
Num ( int m, float j, char k); // Three Arguments Constructor

Num (int m, float j); // Two Arguments Constructor

Num (); // Constructor Without Arguments

void show()
{
cout<< “\n A=”<< a <<“ B=”<< b << “ C=”<< c;
}
};

23
Example (Contd…)
Num :: Num ( int m, float j, char k)
{
cout<<“\n Constructor with Three Arguments ”; void main()
a=m; b= j; c=k; {
} clrscr();
Num :: Num ( int m, float j) Num x(4, 5.5, „A‟);
{ x.show();
cout<<“\n Constructor with Two Arguments ”;
a=m; b= j;
Num y(1, 2.2);
c=„ „; // As show() is print C, So it should be
// initialized with Null string y.show();
}
Num :: Num ( ) Num z;
{ z.show();
cout<<“\n Constructor without Arguments ”; }
a=b=c=NULL;
}

24
Output

Constructor with Three Arguments


A=4 B=5.5 C=A
Constructor with Two Arguments
A=1 B=2.2 C=
Constructor without Arguments
A= B= C=

25
Copy Constructor

 The constructor can accept arguments of any type including user-defined data types and
objects of its own class.

 To pass reference of object to the constructor is known as Copy Constructor

 Using Copy constructor, it is possible for the programmers to declare and initialize one
object using another object.

26
Example
class Num void main()
{ {
private:
clrscr();
int n;
public: Num J(50);
Num ( ) { } // Default Constructor Num K(J); // Deep Copy, Copy Constructor
Num L=J; // Shallow Copy
Num ( int k ) { n=k; } // One Argument Num M; // Default Constructor is Used
Constructor
M=J; // Member by Member Copy
Num (Num & obj ) // Copy Constructor
{ n= obj.n; } cout<<“Object J, Value of n is= ”; J.show();
cout<<“\nObject K, Value of n is= ”; K.show();
void show()
cout<<“\nObject L, Value of n is= ”; L.show();
{
cout<< n; cout<<“\nObject M, Value of n is= ”; M.show();
} getch();
}; }

27
Output

Object J, Value of n is= 50


Object K, Value of n is= 50
Object L, Value of n is= 50
Object M, Value of n is= 50

28
Destructors

 Destructor is also a special member function like constructor

 Destructor destroy the objects created by constructors

 The destructors have the same name as their class, preceded by a tilde (~)

 Destructors does not have return type and not even void

 Only one destructor can be defined in a class.

 Destructor does not have any argument, they can not be overloaded.

29
Example
int c=0; // Global Variable void main()
class Num {
{ clrscr();
public: cout<<“ \n In Main() Function”;
Num ( ) // Default Constructor
{
Num A, B;
c++;
cout<< “\nObject Created: Object No. ”<< c ;
} cout<<“\n \n Block A”;
{
~Num ( ) // Destructor Num C;
{ }
cout<< “\nObject Released: Object No. ”<< c ;
c--; cout<<“ \n Again in Main() Function”;
} getch();
}; }

30
Output

31
Have a Nice Day!

You might also like