Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Constructors

Dr.J.saira banu , Associate Professor, SCOPE, VIT University


Constructors

Classes control object initialization by defining one or more


special member functions known as constructors.
The job of a constructor is to initialize the data members of a
class object.
A constructor is run whenever an object of a class type is
created.
Constructors have the same name as the class.
Unlike other functions, constructors have no return type.
Constructors can not be virtual.
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Constructors

• A class can have multiple constructors.


• Like any other overloaded function, the constructors
must differ from each other in the number or types of
their parameters.
• Constructors are the special type of member functions
that initializes the object automatically when it is
created.
• Compiler identifies that the given member function is a
constructor by its name

Dr.J.saira banu , Associate Professor, SCOPE, VIT University


Constructor - Syntax

class classname {
public:
//Declaring the default constructor.
classname() {

}
};

Dr.J.saira banu , Associate Professor, SCOPE, VIT University


Types of constructors

• There are several forms in which a constructor can


take its shape namely:
• Default Constructor
• Parameterized Constructor
• Copy Constructor

Dr.J.saira banu , Associate Professor, SCOPE, VIT University


Default constructor

• Classes control default initialization by defining a


special constructor, known as the default constructor.
The default constructor is one that takes no arguments.
• If the class does not explicitly define any constructors,
the compiler will implicitly define the default
constructor.
• The compiler-generated constructor is known as the
synthesized default constructor.

Dr.J.saira banu , Associate Professor, SCOPE, VIT University


Default constructor – Example1

class math
{ int main()
private:
int a,b,c; {
public:
math()
math o;
{ o.add();
a=0;
b=0; return 0;
}
void add()
}
{
c=a+b;
cout<<"Total : "<<c;
}
};

Dr.J.saira banu , Associate Professor, SCOPE, VIT


University
Default constructor -Example
#include <iostream>
using namespace std;
class Base {
public: int square;
Base(int x = 5) // Default constructor with a default value.
square = x * x;
}

void show() {
cout << "Square of number =" << square << endl;
}
};
int main() {
Base b;
b.show();
return 0; }
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Parameterized constructor

• A parameterized constructor is the one that has


parameters or arguments specified in it.
• We can pass the arguments to constructor function
when object is created.
• A constructor that can take arguments is called
parameterized constructor.
• When a constructor is parameterized, we must pass
the initial values as arguments to the constructor
function when an object is declared.

Dr.J.saira banu , Associate Professor, SCOPE, VIT University


Parameterized constructor -Syntax

class classname {
public:
//Declaring the default constructor.
classname( int, int ) {

}
};
Classname objname(list of parameters);

Dr.J.saira banu , Associate Professor, SCOPE, VIT University


Parameterized constructor -example

• class math • int main()


• {
• private: •{
• int a,b,c;
• math o(10,25);
• public:
• math(int x,int y) • o.add();
• {
• a=x;
• return 0;
• b=y; •}
• }
• void add()
• {
• c=a+b;
• cout<<"Total : "<<c;
• }
• };
Dr.J.saira banu , Associate Professor, SCOPE, VIT
University
Copy constructor

• A copy constructor is a member function which initializes an object


using another object of the same class. It is used to declare and
initialize an object from another object.
• The most common form of copy constructor is shown here:
• classname (const classname &obj)
• { // body of constructor }
• Here, obj is a reference to an object that is being used to initialize
another object.
• Copy constructor object creation:
• integer (integer & i) ; (or)
• integer I 2 ( I 1 ) ; (or)
• integer I 2 = I 1 ;
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Copy constructor

• The copy constructor creates an object by initializing it with an object


of the same class, which has been created previously.
• The copy constructor is used to:
• Initialize one object from another of the same type.
• Copy an object to pass it as an argument to a function.
• Copy an object to return it from a function.
• The process of initializing through a copy constructor is known as copy
initialization.
• A reference variable has been used as an argument to the copy
constructor as we cannot pass the argument by value to a copy
constructor

Dr.J.saira banu , Associate Professor, SCOPE, VIT University


Copy Constructor Example
• class math
• {
• private: • int main()
int a,b,c;
•{

• public:
• math(int x,int y)
• math o(10,25);
• {
• a=x; • math o1(o);
• b=y;
• } • o.add();
math(math &x1)
• o1.add();

• {
• a=x1.a;
• return 0;
• b=x1.b;
• } •}
• void add()
• {
• c=a+b;
• cout<<"Total : "<<c<<endl;
• }
Dr.J.saira banu , Associate Professor, SCOPE, VIT
University
Destructors

• A destructor is a special member function of a class that is executed


whenever an object of it's class goes out of scope.
• A destructor will have exact same name as the class prefixed with a tilde (~).
• It can neither return a value nor can it take any parameters.
• Destructor can be very useful for releasing resources before coming out of
the program like closing files, releasing memories etc.
• The destructor is commonly used to "clean up" when an object is no
longer necessary. Its main purpose is to release any resources that the
object has acquired during its lifetime, such as memory, files, or network
connections

Dr.J.saira banu , Associate Professor, SCOPE, VIT University


Destructors

• Destructors are called when one of the following


events occurs:
• An object allocated using the new operator is explicitly
deallocated using the delete operator.
• A local (automatic) object with block scope goes out of
scope.
• The lifetime of a temporary object ends.
• A program ends and global or static objects exist.

Dr.J.saira banu , Associate Professor, SCOPE, VIT University


Destructor syntax

• Class Name_of _class {


• public:
• ~Name_of_class() //this is known as Destructor
• {
• }
•}

Dr.J.saira banu , Associate Professor, SCOPE, VIT University


Destructor Example

class Demo { ~Demo() {


private: cout<<"Inside Destructor";
int num1, num2;
public:
}
Demo(int n1, int n2) { };
cout<<"Inside Constructor"<<endl;
int main() {
num1 = n1;
num2 = n2; Demo obj1(10, 20);
}
obj1.display();
void display() {
cout<<"num1 = "<< num1 <<endl; return 0;
cout<<"num2 = "<< num2 <<endl; }
}

Dr.J.saira banu , Associate Professor, SCOPE, VIT


University

You might also like