0% found this document useful (0 votes)
6 views20 pages

Lecture 4-1

The document provides an overview of constructors and destructors in C++, detailing their types, characteristics, and usage. It covers various types of constructors including parameterized, multiple, and copy constructors, as well as dynamic constructors and destructors. Key concepts such as default arguments and the automatic invocation of constructors and destructors are also discussed.

Uploaded by

haroonalmqtrysw2
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)
6 views20 pages

Lecture 4-1

The document provides an overview of constructors and destructors in C++, detailing their types, characteristics, and usage. It covers various types of constructors including parameterized, multiple, and copy constructors, as well as dynamic constructors and destructors. Key concepts such as default arguments and the automatic invocation of constructors and destructors are also discussed.

Uploaded by

haroonalmqtrysw2
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/ 20

CONSTRUCTORS

AND
DESTRUCTORS

Eng.Ezzaddin Ahmed 1
KEY CONCEPTS
 Introduction to Constructors
 Parameterized Constructors
 Multiple Constructors in a class
 Constructors with default arguments
 Copy Constructor
 Dynamic Constructor
 Destructors

Eng. Ezzaddin Ahmed OOP(C++) 2


INTRODUCTION TO CONSTRUCTORS
 A CONSTRUCTOR IS A SPECIAL MEMBER FUNCTION WHOSE TASK
IS TO INITIALIZE THE OBJECTS OF ITS CLASS.

 IT IS SPECIAL BECAUSE ITS NAME IS THE SAME AS THE CLASS


NAME.

 THE CONSTRUCTOR IS INVOKED WHENEVER AN OBJECT OF THAT


CLASS IS CREATED.

 IT IS CALLED A CONSTRUCTOR BECAUSE IT CONSTRUCTS THE


VALUES OF THE DATA MEMBERS OF THE CLASS.

Eng. Ezzaddin Ahmed OOP(C++) 3


Example of Constructor:

// class with a constructor

class integer
{
int m, n;
public:
integer(void); //constructor declared
…….
…….
};

integer :: integer(void) //constructor defined


{
m=0; n=0;
}

Eng. Ezzaddin Ahmed OOP(C++) 4


Some Special Characteristics of Constructors:

 They should be declared in the public section.


 They are invoked automatically when the object is
created.
 They do not have return types, not even void and
therefore they cannot return any values.
 They cannot be inherited, though derived class can
call the base class constructor.
 Constructors can have default arguments.
 We cannot refer to their addresses.
 Constructors cannot be virtual.
 They can make ‘implicit calls’ to the operators ‘new’
and ‘delete’ when memory allocation is required.

Eng. Ezzaddin Ahmed OOP(C++) 5


PARAMETERIZED CONSTRUCTORS
Parameterized constructors is to achieve this objective by passing
arguments to the constructor function when the objects are
created. The constructors that can take arguments are called
Parameterized constructors.

The constructor integer() may be modified to take arguments as shown


below:
class integer
{
int m,n;
public:
integer(int x,int y); // parameterized constructor
………..
………..
};

integer :: integer(int x,int y)


{
m=x; n=y;
}
Eng. Ezzaddin Ahmed OOP(C++) 6
PARAMETERIZED CONSTRUCTORS Cont.
 We must pass the initial values as arguments to the
constructor function when an object is declared. This can be
done in two ways:
By calling the constructor explicitly.
By calling the constructor implicitly.

The following declaration illustrates the first method:

integer int1 = integer(0,100); // explicit call

This statement creates an integer object int1 and passes the


values 0 and 100 to it. The second is implemented as follows:

integer int1(0,100); // implicit call

This method sometimes called the shorthand method, is used


very often as it is shorter, looks better and is easy to
implement.
Eng. Ezzaddin Ahmed OOP(C++) 7
Parameterized Constructors Cont.

Now have to discuss about the main function


of class with constructors:

int main()

{
integer int1(0,100); // constructor called implicitly
integer int2 = integer(25,75);// constructor called
explicitly The output :
cout<< “\nobject1”<<“\n”; Object1
int1.display(); m=0
n=100
cout<<“\nobject2”<<“\n”;
int2.display();
object2
return 0; m=25
} n=75
Eng. Ezzaddin Ahmed OOP(C++) 8
MULTIPLE CONSTRUCTOR

Integer( ); //no arguments


Integer (int, int); //two arguments

In the first case, the constructor itself supplies


data values and no values is passed.
In the second case ,the function call passes the appropriate values.

Integer I2(20,40);
Integer I3(I2); //copy constructor

Eng. Ezzaddin Ahmed OOP(C++) 9


Example of Multiple Constructors:
#include<iostream>

using namespace std;

class complex
{
float x,y;
public:
complex () { } //constructor no argument
complex (float a){x=y=a;} //constructor-one arg
complex(float real, float imag) //constructor-two args
{ x=real; y=imag;}
friend complex sum(complex,complex);
friend void show (complex);
};
complex sum(complex c1,complex c2) //friend
{
complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return(c3);
}
Eng. Ezzaddin Ahmed OOP(C++) 10
void show(complex c) //friend
{
cout<<c.x<<"+j"<<c.y<<"\n";
}
int main()
{
complex A(2.7,3.5); //define & initialize
complex B(1.6); //define & initialize
complex c; //define

c=sum(A,B); //sum( ) is a friend


cout<<"A=";show(A); //show() is also friend
cout<<"B=";show(B);
cout<<"C=";show(c);

return 0; Output:
}
A = 2.7 + j3.5
B = 1.6 + j1.6
C = 4.3 + j5.1

Eng. Ezzaddin Ahmed OOP(C++) 11


CONSTRUCTOR WITH DEFAULT ARGUMENTS
It is possible to define constructors with default arguments. For
example, the constructor complex() can be declared as follows:

Complex(float real, float imag=0);

The default value of the argument imag is zero. Then, the


statement

Complex C(5.0);
Assigns the value 5.0 to the real variable and 0.0 to imag(by
default). However, the statement
Complex C(2.0,3.0);
Assigns 2.0 to real and 3.0 to image.

Eng. Ezzaddin Ahmed OOP(C++) 12


COPY CONSTRUCTOR
 Copy constructor is used to declare and initialize an
object from another object.

 Copy constructor takes a reference to an object of the


same class as itself as an argument.

 Constructor can take any data type/class as its parameter


except it’s own type.

 Copy constructor is allowed to take it’s own reference as


parameter.

Eng. Ezzaddin Ahmed OOP(C++) 13


Example of Copy Constructor:
#include<iostream>
using namespace std;
class A
{
int a;
public:
A() { cout<<"Default\n"; }
A(int x)
{ cout<<"parameterized\n";
a=x;
}
A(A &ob) //copy constructor
{
cout<<"copy constructor\n";
a=ob.a;
} Output:
};
int main() Parameterized
{ Copy constructor
A ob1(10); Copy constructor
A ob2=ob1; //copy constructor
Default
A ob3(ob2); //copy constructor
A ob4;
ob4=ob1; //copy constructor not called
return 0;
}
Eng. Ezzaddin Ahmed OOP(C++) 14
DYNAMIC CONSTRUCTOR
 Enables the system to allocate right amount of memory for
each object when the objects are not of the same size.

 Allocation of memory to object at the time of their


construction is known as dynamic construction of object.

 The memory allocated with the help of new operator.

Eng. Ezzaddin Ahmed OOP(C++) 15


Example of Dynamic Constructor:
#include<iostream>
#include<string.h>
class String
{
int len;
char *data;
public:
String (char *st)
{
len=strlen(st);
data=new char [len];
strcpy(data,st);
}
void show() { cout<<data<<"\n"; }
};
int main()
{
char str1[] ="Hello";
Output:
String s2(str1);
char str2[] ="This is a good day";
String s3(str2); Hello
s2.show(); This is a good day
s3.show();
return 0;
}

Eng. Ezzaddin Ahmed OOP(C++) 16


const Objects
class hello
{
int n;
public:
hello(){n=22;}
void setn(int a){n=a;}
void print(){cout<<n<<endl;}
void printf() const
{cout<<n<<endl;}
//void setn2(int b) const {n=b;}
//not allowed-as you cannot modify anything in a constant
};
void main()
{
const hello h;
//h.setn(34); //not allowed
//h.print(); //not allowed
h.printf();//a const object can only call a const function
}
• you can not declare a constructor or destructor to be const.
Eng. Ezzaddin Ahmed OOP(C++) 17
DESTRUCTORS
 Destructor is used to destroy the object created by the
constructor.

 It will be invoked automatically as soon as the life of the object


is finished.

 It neither has any return type nor it takes any parameter.

 It has same name as the class name but begins with a tilde(~)
sign.

 As like constructor, a default destructor is always written if not


explicitly mentioned by the programmer.

Eng. Ezzaddin Ahmed OOP(C++) 18


Example of Destructor:

#include<iostream>

using namespace std;


int count =0;
class alpha
{
public:
alpha( )
{
count++;
cout<<“No. of object created”<<count;
}
~alpha( )
{
cout<<“No. of object destroyed”<<count;
count--;
}
};

Eng. Ezzaddin Ahmed OOP(C++) 19


Continues:
UOTPUT:

Enter Main
int main()
{ No. of object created 1
cout<<“Enter Main”; No. of object created 2
alpha A1,A2,A3,A4; No. of object created 3
{ No. of object created 4
cout<<“Enter Block1”;
alpha A5; Enter Block1
}
{ No. of object created 5
cout<<“Enter Block2”; No. of object destroyed 5
alpha A6;
} Enter Block2
cout<<“Re-Enter Main”;
No. of object created 5
return 0; No. of object destroyed 5
}
Re-enter Main

No. of object destroyed 4


No. of object destroyed 3
No. of object destroyed 2
No. of object destroyed 1

Eng. Ezzaddin Ahmed OOP(C++) 20

You might also like