0% found this document useful (0 votes)
21 views6 pages

CONSTUCTOR

In C++, constructors are special methods invoked automatically during object creation to initialize data members, with two types: default and parameterized constructors. A destructor, which is the opposite of a constructor, is used to destruct objects and is defined with a tilde prefix. Additionally, a copy constructor allows for the creation of an object from another object of the same class, with both default and user-defined versions available.

Uploaded by

raghavrk3032
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)
21 views6 pages

CONSTUCTOR

In C++, constructors are special methods invoked automatically during object creation to initialize data members, with two types: default and parameterized constructors. A destructor, which is the opposite of a constructor, is used to destruct objects and is defined with a tilde prefix. Additionally, a copy constructor allows for the creation of an object from another object of the same class, with both default and user-defined versions available.

Uploaded by

raghavrk3032
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/ 6

C++ Constructor

In C++, constructor is a special method which is invoked automatically at the time of


object creation. It is used to initialize the data members of new object generally. The
constructor in C++ has the same name as class or structure.

There can be two types of constructors in C++.

o Default constructor
o Parameterized constructor

Constructors do not return values; hence they do not have a return type. Constructors can be
overloaded. Constructor cannot be declared virtual.

C++ Default Constructor


A constructor which has no argument is known as default constructor. It is invoked at the
time of creating object.

Let's see the simple example of C++ default Constructor.

#include <iostream>
using namespace std;
class Employee
{
public:
Employee()
{
cout<<"Default Constructor Invoked"<<endl;
}
};
int main(void)
{
Employee e1; //creating an object of Employee
Employee e2;
return 0; }
DefaulDefault Constructor Invoked
Default Constructor Invoked
t Constructor Invoked

DefaultC++ Parameterized Constructor


A constructor which has parameters is called parameterized constructor. It is used to
provide different values to distinct objects.

Let's see the simple example of C++ Parameterized Constructor.

#include <iostream>
using namespace std;
class Employee {
public:
int id;//data member (also instance
variable)
string name;//data member(also instance
variable)
float salary;
Employee(int i, string n, float s)
{
id = i;
name = n;
salary = s;
}
void display()
{
cout<<id<<" "<<name<<"
"<<salary<<endl;
}
};
int main(void) {
Employee e1 =Employee(101, "Sonoo", 890000);
//creating an object of Employee
Employee e2=Employee(102, "Nakul", 59000);
e1.display();
e2.display();
return 0;
}
101 Sonoo 890000
102 Nakul 59000
Default Constructor Invoked
Default Constructor Invoked
1. Constructor Invoked

C++ DESTRUCTOR
A destructor works opposite to constructor; it destructs the objects of classes. It can be
defined only once in a class. Like constructors, it is invoked automatically.

A destructor is defined like constructor. It must have same name as class. But it is prefixed
with a tilde sign (~).

Note: C++ destructor cannot have parameters.

C++ DESTRUCTOR
A destructor works opposite to constructor; it destructs the objects of classes. It can be
defined only once in a class. Like constructors, it is invoked automatically.

It is prefixed with a tilde sign (~).

Note: C++ destructor cannot have parameters. Moreover, modifiers can't be applied on
destructors.

C++ Constructor and Destructor Example


Let's see an example of constructor and destructor in C++ which is called automatically.

#include <iostream>
using namespace std;
class Employee
{
public:
Employee()
{
cout<<"Constructor Invoked"<<endl;
}
~Employee()
{
cout<<"Destructor Invoked"<<endl;
}
};
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

C++ COPY CONSTRUCTOR


A Copy constructor is an overloaded constructor used to declare and initialize an object from

another object.

Copy Constructor is of two types:


o Default Copy constructor: The compiler defines the default copy constructor. If the user
defines no copy constructor, compiler supplies its constructor.

o User Defined constructor: The programmer defines the user-defined constructor.


#include <iostream>
using namespace std;
class A
{
public:
int x;
A(int a) // parameterized constructor.
{
x=a;
}
A(A &i) // copy constructor
{
x = i.x;
}
};
int main()
{
A a1(20); // Calling the parameterized constructor.
A a2(a1);
A a3 = a1;
// Calling the copy constructor.
cout<<a2.x;
return 0;

When Copy Constructor is called


Copy Constructor is called in the following scenarios:

o When we initialize the object with another existing object of the same class type. For example,
Student s1 = s2, where Student is the class.

o When the object of the same class type is passed by value as an argument.

You might also like