0% found this document useful (0 votes)
9 views7 pages

Constructors

OOP Constructors

Uploaded by

M Umar
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)
9 views7 pages

Constructors

OOP Constructors

Uploaded by

M Umar
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/ 7

Constructor

A constructor is a special member function of a class and shares the same name as of class,
which means the constructor and class have the same name. Constructor is called by the
compiler whenever the object of the class is created, it allocates the memory to the object and
initializes class data members by default values or values passed by the user while creating an
object. Constructors don’t have any return type because their work is to just create and initialize
an object.
Syntax of Constructor
The basic syntax of the constructor is given below:
class class_name{
private:
// private members
public:
// declaring constructor
class_name({parameters})
{
// constructor body
}
};
In the above syntax, we can see the class has the name class_name and the constructor have
also the same name. A constructor can have any number of parameters as per requirements.
Also, there is no return type or return value of the constructor.
Important Points about Constructors
Access specifiers
Constructors can be defined as public, protected, or private as per the requirements. By default
or default constructors, which are created by the compiler are declared as the public. If the
constructor is created as private, then we are not able to create its object.
When there is no requirement of the object of a class (in a situation when all class members are
static) we can define its constructor as private.
Usually, constructors are defined as public because constructors are used to create an object
and initialize the class data members for the object. An object is always created from outside of
class, which justifies making constructors public.
Types of Constructors
There are four types of constructors in c++
• Default constructor
• Parameterized constructor
• Copy Constructor
• Dynamic Constructor
Default Constructor

Default constructor is also known as a zero-argument constructor, as it doesn’t take any

parameter. It can be defined by the user if not then the compiler creates it on his own.

Default constructor always initializes data members of the class with the same value they
were defined.

Syntax

class class_name{
private:
// private members
public:
// declaring default constructor
class_name()
{
// constructor body
}
};
#include <iostream>

using namespace std;

class Person{

// declaring private class data members


private:

string name;

int age;

public:

// declaring constructor

Person()

cout<<"Default constructor is called"<<endl;

name = "student";

age = 12;

// display function to print the class data members value

void display()

cout<<"Name of current object: "<<name<<endl;

cout<<"Age of current object: "<<age<<endl;

};

int main()

// creating object of class using default constructor

Person obj;

obj.display();

return 0;

Output
Default constructor is called
Name of current object: student
Age of current object: 12

Parameterized Constructor
Parameterized constructor is used to initialize data members with the values provided by the
user. This constructor is basically the upgraded version of the default constructor. We can define
more than one parameterized constructor according to the need of the user, but we have to
follow the rules of the function overloading, like a different set of arguments must be there for
each constructor.
class class_name{
private:
// private members
public:
// declaring parameterized constructor
class_name(parameter1, parameter2,...)
{
// constructor body
}
};
Code Example
#include <iostream>
using namespace std;
class Person{
// declaring private class data members
private:
string name;
int age;
public:
// declaring parameterized constructor of three different types
Person(string person_name)
{
cout<<"Constructor to set name is called"<<endl;
name = person_name;
age = 12;
}
Person(int person_age)
{
cout<<"Constructor to set age is called"<<endl;
name = "Student";
age = person_age;
}
Person(string person_name, int person_age)
{
cout<<"Constructor for both name and age is called"<<endl;
name = person_name;
age = person_age;
}
// display function to print the class data members value
void display()
{
cout<<"Name of current object: "<<name<<endl;
cout<<"Age of current object: "<<age<<endl;
cout<<endl;
}
};
int main()
{
// creating objects of class using parameterized constructor
Person obj1("First person");
// printing class data members for first object
obj1.display();
Person obj2(25);
// printing class data members for second object
obj2.display();
Person obj3("Second person",15);
// printing class data members for third object
obj3.display();
return 0;
}
Output
Constructor to set name is called
Name of current object: First person
Age of current object: 12

Constructor to set age is called


Name of current object: Student
Age of current object: 25

Constructor for both name and age is called


Name of current object: Second person
Age of current object: 15
Copy Constructor
If we have an object of a class and we want to create its copy in a new declared object of the
same class, then a copy constructor is used. The compiler provides each class a default copy
constructor and users can define it also. It takes a single argument which is an object of the
same class.
Syntax
class class_name{
private:
// private members
public:
// declaring copy constructor
class_name(const class_name& obj)
{
// constructor body
}
};
Dynamic Constructor
When memory is allocated dynamically to the data members at the runtime using a new
operator, the constructor is known as the dynamic constructor. This constructor is similar to the
default or parameterized constructor; the only difference is it uses a new operator to allocate
the memory.
Syntax
class class_name{
private:
// private members
public:
// declaring dynamic constructor
class_name({parameters})
{
// constructor body where data members are initialized using new operator
} };

You might also like