In C++ there are various ways of using constructors. We will understand with the help of programming examples.
Constructors
•
Objects need to initialize the variables.
Such initialized variables can then be used for processing. If the variables
are not been initialized then they hold some garbage value. And if such variables are taken for operation then
unexpected results may occur. In order to avoid that the class can include a
special function called constructor.
The constructor can automatically be
called whenever a new object of
this class is created. The constructor will have the same name as the class name.
It should not have any return type.
•
In C++ there are various ways of using constructors. We will understand with
the help of programming examples.
Characteristics of Constructors
•
Following are some rules that must be followed while making use of constructors
‒
1.
Name of the constructor must be same as
the name of the class for which it is being used.
2.
The constructor must be declared in the public
mode.
3.
The constructor gets invoked automatically when an object gets created.
4.
The constructor should not have any return type. Even a void type should not be
written for the constructor.
5.
The constructor can not be used as a member of union or structure.
6.
The constructors can have default arguments.
7.
The constructors can not be inherited.
But the derive class can invoke the constructor of base class.
8.
Constructors can make use of new or delete operators for allocating or
releasing the memory.
9.
Constructors can not be virtual.
10.
Multiple constructors can be used by the same class.
11. When we declare the constructor explicitly
then we must declare the object of that class.
•
Various s types of constructors used in C++ are ‒
1.
Default constructor
2.
Parameterized constructor
3.
Default argument constructor
4.
Copy constructor.
Let
us discuss them in detail.
Object Oriented Programming: Chapter 2: Classes and Objects : Tag: : C++ Programming | Object Oriented Programming - Characteristics of Constructors
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Applied Physics (CSIE) II
PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Digital Principles and Computer Organization
CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electrical and Electronics Engineering
EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs) - Laboratory
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation