Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views4 pages

Constructors

Uploaded by

udayaveeramani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Constructors

Uploaded by

udayaveeramani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Constructors

In C++, a constructor is a special member function of a class that is automatically called when an object
of that class is created. Its main purpose is to initialize the object’s data members.
 Name – The constructor has the same name as the class.
 No return type – Not even void.
 Automatic call – Runs when an object is created.
 Can be overloaded – You can have multiple constructors with different parameters.
Types:
1. Default constructors
This constructor doesn't take any argument as it is parameter less and initializes object members using
default values. It is also called a zero-argument constructor. However, the default constructor is not
generated by the compiler if the programmer has explicitly defined a constructor.
Example:
#include<iostream.h>
#include<conio.h>
#include<string.h> // for strcpy
class Employee
{
int Id;
char Name[25];
int Age;
long Salary;
public:
Employee() // Default Constructor
{
Id = 101;
strcpy(Name, "Smith"); //Copy string into char array
Age = 30;
Salary = 50000;
cout << "Default Constructor called\n";
}
void PutData()
{
cout << "\n\nEmployee Id : " << Id;
cout << "\nEmployee Name : " << Name;
cout << "\nEmployee Age : " << Age;
cout << "\nEmployee Salary : " << Salary;
}
};
void main()
{
clrscr();
Employee E; // Default constructor is called automatically
E.PutData();
}

Output:
Default Constructor called

Employee Id : 101
Employee Name : Smith
Employee Age : 30
Employee Salary : 50000

2. Parameterized constructors
Parameterized constructor allows us to pass arguments to constructors. Typically, these arguments
help initialize an object's members. 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's members.
Example:
#include<iostream.h>
#include<conio.h>
#include<string.h> // for strcpy
class Employee
{
int Id;
char Name[25];
int Age;
long Salary;
public:
Employee(int id, const char name[], int age, long
salary) // Parameterized Constructor
{
Id = id;
strcpy(Name, name); // copy string into char array
Age = age;
Salary = salary;
cout << "Parameterized Constructor called\n";
}

void PutData()
{
cout << "\n\nEmployee Id : " << Id;
cout << "\nEmployee Name : " << Name;
cout << "\nEmployee Age : " << Age;
cout << "\nEmployee Salary : " << Salary;
}
};

void main()
{
// Passing values directly while creating object
Employee E(102, "John", 28, 55000);
E.PutData();
}
Output:
Parameterized Constructor called

Employee Id : 102
Employee Name : John
Employee Age : 28
Employee Salary : 55000

3. copy constructors
A copy constructor is a member function that initializes an object using another object of the same
class. Copy constructor takes a reference to an object of the same class as an argument.
Example:
#include<iostream.h>
#include<conio.h>
#include<string.h> // for strcpy
class Employee
{
int Id;
char Name[25];
int Age;
long Salary;
public:
// Default Constructor
Employee()
{
Id = 101;
strcpy(Name, "Smith");
Age = 30;
Salary = 50000;
cout << "Default Constructor called\n";
}

// Copy Constructor
Employee(const Employee &E)
{
Id = E.Id;
strcpy(Name, E.Name);
Age = E.Age;
Salary = E.Salary;
cout << "Copy Constructor called\n";
}

void PutData()
{
cout << "\n\nEmployee Id : " << Id;
cout << "\nEmployee Name : " << Name;
cout << "\nEmployee Age : " << Age;
cout << "\nEmployee Salary : " << Salary;
}
};

void main()
{
clrscr();
Employee E1; // Calls Default Constructor
E1.PutData();
Employee E2 = E1; // Calls Copy Constructor
E2.PutData();
getch();
}

Output:
Default Constructor called
Employee Id : 101
Employee Name : Smith
Employee Age : 30
Employee Salary : 50000
Copy Constructor called

Employee Id : 101
Employee Name : Smith
Employee Age : 30
Employee Salary : 50000

You might also like