Institute - Uie Department-Academic Unit-1
Institute - Uie Department-Academic Unit-1
2
• Need for constructors,
• types of constructors: default,
parameterized, copy constructor,
• order of execution of constructors,
CONTENTS • destructors and their need.
3
CONSTRUCTORS
What is constructor?
A constructor is a member function of a class which initializes objects of a class. In C++, Constructor is automatically called
when object(instance of class) create. It is special member function of the class.
How constructors are different from a normal member function?
A constructor is different from normal functions in following ways:
• Constructor has same name as the class itself
• Constructors don’t have return type
• A constructor is automatically called when an object is created.
• If we do not specify a constructor, C++ compiler generates a default constructor for us (expects no parameters and has an
empty body).
4
WHY NEED CONSTRUCTORS
• The data members of a class have private scope by default.
• The private data members are not accessible out the class. When objects are created, the members of the object cannot be
initialised directly.
• So, constructors are used to initialise the data members of a class when an object is created. A constructor is implicitly
created by the compiler if one is not present.
class Student
{
int marks;
int rollno;
} Here, when you create s1 object of
class Student, s1 will have a copy of
void main()
{ marks and copy of rollno.
Student s1; //creating object s1
}
5
WHY NEED CONSTRUCTORS
• Why constructor is used to initialize member variables of class? It’s so because we cannot declare and define the member
variables in single statement.
• Example:
class Student {
int marks = 95;
int rollno = 7007;
}
class Student
{
int marks;
int rollno;
Student() //default constructor
{
marks=0, rollno=0;} };
6
CHARACTERISTICS OF CONSTRUCTOR
• They should be declared in the public section.
• They are invoked automatically when the objects are created.
• They do not have return types, not even void and they cannot return values.
• They cannot be inherited, though a derived class can call the base class constructor.
• Like other C++ functions, Constructors can have default arguments.
• Constructors can not be virtual.
• We can not refer to their addresses.
• An object with a constructor (or destructor) can not be used as a member of a union.
• They make ‘implicit calls’ to the operators new and delete when memory allocation is required.
7
TYPES OF CONSTRUCTORS
Constructors are of three types :
• Default Constructor
• Parameterized Constructor
• Copy Constructor
• Default Constructors: Default constructor is the constructor which doesn’t take any argument. It has no parameters. Even if
we do not define any constructor explicitly, the compiler will automatically provide a default constructor implicitly.
• Parameterized Constructors: It is possible to pass arguments to constructors. Typically, these arguments help initialize an
object when it is created. To create a parameterized constructor, simply add parameters to it the way you would to any other
function. When you define the constructor’s body, use the parameters to initialize the object.
• Copy Constructor: A copy constructor is a member function which initializes an object using another object of the same
class.
8
DEFAULT CONSTRUCTOR
CLASS
11
PARAMETERISED CONSTRUCTOR
Uses of Parameterized constructor:
• It is used to initialize the various data elements of different objects with different values when they are created.
• It is used to overload constructors.
Can we have more than one constructors in a class?
• Yes, It is called Constructor Overloading.
12
COPY CONSTRUCTOR
Copy Constructor: A copy constructor is a member function which initializes an object using another object of the same class.
Detailed article on Copy Constructor.
Whenever we define one or more non-default constructors( with parameters ) for a class, a default constructor( without
parameters ) should also be explicitly defined as the compiler will not provide a default constructor in this case. However, it is
not necessary but it’s considered to be the best practice to always define a default constructor.
13
COPY CONSTRUCTOR
17
WHY COPY CONSTRUCTOR ARGUMENT
SHOULD BE CONST IN C++?
When we create our own copy constructor, we pass an object by reference and we generally pass it as a const reference.
One reason for passing const reference is, we should use const in C++ wherever possible so that objects are not accidentally
modified[3].
18
WHEN IS COPY CONSTRUCTOR
CALLED?
In C++, a Copy Constructor may be called in following cases:
• When an object of the class is returned by value.
• When an object of the class is passed (to a function) by value as an argument.
• When an object is constructed based on another object of the same class.
• When compiler generates a temporary object
19
CAN WE CALL PRIVATE
CONSTRUCTOR?
YES
Using friend function.
Activity to be done in class. Teacher should ask the students to use the concepts learned and execute the program.
20
DESTRUCTORS
Destructors are also special member functions used in C++.
It has the same name as that of the class to which it belongs preceded by tilde (~) sign.
Syntax:
class classname
{
public:
~classname(); //destructor declaration.
};
• Destructors are used to free memory, release resources and to perform other clean up.
• Destructors are automatically named when an object is destroyed.
21
ORDER OF EXECUTION OF
CONSTRUCTORS & DESTRUCTORS
Whenever we create an object of a class, the default constructor of that class is invoked automatically to initialize the members
of the class.
• If we inherit a class from another class and create an object of the derived class, it is clear that the default constructor of the
derived class will be invoked but before that the default constructor of all of the base classes will be invoke, i.e the order of
invokation is that the base class’s default constructor will be invoked first and then the derived class’s default constructor
will be invoked.
• Why?
• When a class is inherited from other class,t he data members and member functions of base class comes automatically in
derived class based on the access specifier but the definition of these members exists in base class only. So when we create
an object of derived class, all of the members of derived class must be initialized but the inherited members in derived class
can only be initialized by the base class’s constructor as the definition of these members exists in base class only. This is
why the constructor of base class is called first to initialize all the inherited members.
22
ORDER OF EXECUTION OF
CONSTRUCTORS & DESTRUCTORS
24
APPLICATIONS
• Constructor Delegation in C++
https://www.learncpp.com/cpp-tutorial/8-6-overlapping-and-delegating-constructors/
• WAP to show that for each object, constructor is called separately.
• WAP to read value through keyboard using constructor function.
25
REFERENCES
• Reference Website
[1] https://www.geeksforgeeks.org/c-classes-and-objects/
[2] https://www.geeksforgeeks.org/object-oriented-programming-in-cpp/
[3] https://www.geeksforgeeks.org/copy-constructor-argument-const/
26
THANK YOU
For queries
Email: [email protected]