0% found this document useful (0 votes)
16 views39 pages

Sem3 (OOP) UNIT - 3

The document provides an overview of constructors in C++, detailing their characteristics, types (default, parameterized, and copy constructors), and syntax. It explains the purpose of constructors in initializing class objects, the significance of public access, and the concept of constructor overloading. Additionally, it briefly discusses destructors, their characteristics, and when they are invoked.

Uploaded by

salonisingh58494
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)
16 views39 pages

Sem3 (OOP) UNIT - 3

The document provides an overview of constructors in C++, detailing their characteristics, types (default, parameterized, and copy constructors), and syntax. It explains the purpose of constructors in initializing class objects, the significance of public access, and the concept of constructor overloading. Additionally, it briefly discusses destructors, their characteristics, and when they are invoked.

Uploaded by

salonisingh58494
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/ 39

CONSTRUCTOR

SDJ INTERNATIONAL COLLEGE,


PALSANA
Constructor
■ Constructor is a special member function of a class that initializes
the object of the class.
■ It is special member function because
❑ Constructor name is same as class name.
❑ It doesn’t have a return type.

2
Constructor
Syntax:
class <classname>
{
public:
classname ( [argument] …)
{
constructor body;
}
};

3
Constructor
Example:
class student
{
int id; char name[30];
public:
student( )
{
id=10;
strcpy(name,”om”);
}
};

4
Characteristics of Constructor
■ Its name is same as class name.
■ It doesn’t have a return type.
■ Must declare in public section.
■ Call automatically when object is created.
■ Cannot be virtual.
■ Cannot be inherited.
■ Make implicit call to operator new and delete when memory
allocation is required.

5
Why constructor has no return type?
The reason the constructor doesn't return a value is because
■ It is used to initialized the member of the class or object.
■ It's not called directly by your code, it's called by the memory
allocation and object initialization code in the runtime.

6
Why constructor is written in public?
It is declared in public because
■ Public member function is access outside the class.
■ Constructor is called when object is created and object is declare
in main ( ) means object is created outside the class in main ( ).
■ So if we write constructor in private , we cannot call it outside the
class like main ( ).

7
Type of Constructor
■ There are 3 type of constructor in c++
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor.

8
Default Constructor

■ Default constructor is the constructor which doesn't take any


argument. It has no parameter.
■ First member function call by compiler in any program.
■ If it is not written by programmer , it is automatically called by
compiler.
■ Every program has default constructor.

9
Default Constructor
■ Syntax :
class_name( )
{
// constructor Definition
}

10
Example : Default Constructor
class Cube
{
public:
int side;
Cube( ) // default constructor called when object is created.
{
side = 10;
}
};

11
Example : Default Constructor
int main()
{
Cube c; // call default constructor.
cout << c.side;
}

12
Explanation : Default Constructor
■ In this case, as soon as the object is created the constructor is
called which initializes its data members.
■ A default constructor is so important for initialization of object
members, that even if we do not define a constructor explicitly,
the compiler will provide a default constructor implicitly.

13
Parameterized Constructor
■ Parameterized constructor is constructor which can take
arguments or parameters.
■ It is used to initialize class member with specific value.
■ Using this Constructor you can provide different values to data
members of different objects, by passing the appropriate values
as argument.

14
Parameterized Constructor
■ Syntax
class_name( argument1, argument 2, ….)
{
// constructor Definition
}

15
Parameterized Constructor
■ It is called 2 different ways.
1. Implicit
2. Explicit
■ Implicit parameterized constructor is used when we initialize
object with same value every time program runs.
■ It is called like
Cube S(10); // implicit call.
■ So when object is created it is initialized member with 10 every
time.

16
Parameterized Constructor
■ Explicit parameterized constructor is used when we initialize
object with different value every time program runs or user
defined value.
■ It is called like
int x;
cout<<“Enter value:”<<x;
cin >>x;
Cube S = cube(x) // explicit call
■ So when object is created it is initialized member with 10 every
time.
17
Example : Parameterized Constructor
class Cube
{
int side;
public:
Cube(int x) // Parameterized constructor
{
side = x;
}
void display();
};
18
Example : Parameterized Constructor
int main()
{
Cube c(10); // Implicit called
c.display( );
cout<<“Enter value:”<<x;
cin >>x;
Cube S = cube(x) //Explicit called
s.display();
return 0;
}
19
Example : Parameterized Constructor
void Cube::display( )
{
cout<<“ Cube side :”<<side
}
Output:
Cube side is 10
Enter Value : 20
Cube side is 20

20
Copy Constructor
■ A copy constructor is a member function which initializes an
object using another object of the same class.
■ These are special type of Constructors which takes an object
reference as argument, and is used to copy values of data
members of one object into other object.
■ Syntax
ClassName (ClassName &old_obj)
{
Constructor body
}
21
Example : Copy Constructor
class Cube
{
int side;
public:
Cube(Cube &x) // copy constructor
{
side = x.side;
}
Cube(int x) { side = x; } // Parameterized constructor.
};
22
Example : Copy Constructor
int main()
{
Cube c(10); // parameterized constructor
Cube c1=c; // call copy constructor
clrscr();
c.display();
c1.display( );
}

23
Constructor Overloading
■ In C++, We can have more than one constructor in a class with
same name, as long as each has a different list of arguments.
This concept is known as Constructor Overloading and is quite
similar to function overloading.
■ Every constructor has same name as class name but they differ
in terms of either number of arguments or the datatypes of the
arguments or the both.
■ Overloaded constructors essentially have the same name (name
of the class) and different number of arguments.

24
Constructor Overloading
■ A constructor is called depending upon the number and type of
arguments passed.
■ While creating the object, arguments must be passed to let
compiler know, which constructor needs to be called.
■ In Copy constructor , we have to use constructor overloading
concept.
.
NOTE: Write same program written in copy constructor

25
NOTE

First member function called by compiler is Default

Constructor.

First function called by compiler is main( )

26
Default Arguments
■ A default argument is a function argument that has a default
value provided to it when function is declare.
■ If the user does not supply a value for this argument during
function call, the default value will be used.
■ If the user does supply a value for the default argument, the
user-supplied value is used.
■ The default arguments are used when you provide no arguments
or only few arguments while calling a function.
■ The default arguments are used during compilation of program.

27
Default Arguments
■ For example, lets say you have a user-defined function sum
declared like this:
int sum(int a=10, int b=20);
■ Now while calling this function you do not provide any
arguments, simply called
sum(); // sum(50,60)
■ Then in this case the result would be 30, compiler used the
default values 10 and 20 declared in function signature.

28
Default Arguments
■ If you pass only one argument like this:
sum(80);
■ Then the result would be 100, using the passed argument 80 as
first value and 20 taken from the default .

29
Example : Default Arguments
#include <iostream>
int sum(int a, int b=10, int c=20); // function with default argument
int main()
{
/* In this case “a” value is passed as 1 and “b” and “c” values are
taken from default arguments. */
cout<<sum(1)<<endl;

30
Example : Default Arguments
/* In this case a value is passed as 1 and b value as 2, value of c
values is taken from default arguments.
*/
cout<<sum(1, 2)<<endl;

/* In this case all the three values are passed during function call,
hence no default arguments have been used. */
cout<<sum(1, 2, 3)<<endl;
return 0;
}
31
Example : Default Arguments
int sum(int a, int b, int c)
{
int z;
z = a+b+c;
return z;
}
Output
31
23
6

32
Rules for Default Arguments
■ Only the trailing arguments can have default values and
therefore We must add default values form right-to-left.

int Add(int x, int y=20, int z=30); //Valid

int Add(int x=10, int y=20, int z=30); //Valid

int Add(int x=10, int y, int z); //Invalid

int Add(int x=10, int y, int z=30); //Invalid

33
Destructor
■ A destructor is a special member function that works just
opposite to constructor.
■ Destructors is used to destroy (or delete) the object.
■ It is used to release resource used by object during program
execution like memory, cpu, files, etc..
■ Similar to constructor, the destructor name should exactly match
with the class name.

■ ~
A destructor declaration should always begin with the tilde( ).

34
Destructor
■ Syntax of Destructor
~class_name()
{
//Some code
}

35
Characteristics of Destructor
■ The destructor has the same name as that of the class prefixed
by the tilde character ‘~’.
■ The destructor cannot have arguments.
■ It has no return type.
■ Destructors cannot be overloaded .
■ In the absence of user defined destructor, it is generated by the
compiler.
■ The destructor is executed automatically when the control
reaches the end of class scope to destroy the object.
■ They cannot be inherited

36
When Destructor Called?
■ A destructor is automatically called when:
■ The program finished execution.
■ When a scope (the { } parenthesis) containing local variable
ends.
■ When you call the delete operator.

37
Example : Destructor
#include <iostream>
class Employee
{
public:
Employee()
{
cout<<"Constructor Invoked"<<endl;
}
~Employee()
{
cout<<"Destructor Invoked"<<endl;
}
};

38
Example : Destructor
int main(void)
{
Employee e1; //creating an object of Employee
Employee e2; //creating an object of Employee
return 0;
}
Output:
Constructor Invoked
Constructor Invoked
Destructor Invoked
Destructor Invoked

39

You might also like