0% found this document useful (0 votes)
45 views5 pages

Constructors in C

constructor

Uploaded by

siddhiagrawal019
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)
45 views5 pages

Constructors in C

constructor

Uploaded by

siddhiagrawal019
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/ 5

1|Page

Constructors in C++
Constructor in C++ is a special method that is invoked
automatically at the time of object creation. It is used to initialize
the data members of new objects generally. The constructor in C+
+ has the same name as the class or structure. It constructs the
values i.e. provides data for the object which is why it is known as
constructor.
Constructor is a member function of a class, whose name is same
as the class name.
Constructor is a special type of member function that is used to
initialize the data members for an object of a class automatically,
when an object of the same class is created.
Constructor is invoked at the time of object creation. It constructs
the values i.e. provides data for the object that is why it is known
as constructor.
Constructor do not return value, hence they do not have a return
type.
In C++, a constructor has the same name as that of the class and it
does not have a return type. For example,

class demo
{
public:
demo() // create a constructor
{
// code
}
};

Types of Constructors in C++


There are 3 types of constructors in C++, They are :

 Default Constructor
 Parameterized Constructor
 Copy Constructor
2|Page

Default Constructor

Default constructor is the constructor which doesn’t take any


argument. It has no parameters. It is also called a zero-argument
constructor.

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

// Example: defining the constructor within the class

#include<iostream.h>
#include<conio.h>
class student
{
int rno;
char name[50];
double fee;
public:
student()
{
cout<<"Enter the RollNo:";
cin>>rno;
cout<<"Enter the Name:";
cin>>name;
cout<<"Enter the Fee:";
cin>>fee;
}
void display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}
};
3|Page

void main()
{
student s; //constructor gets called automatically when we
create the object of the class
s.display();
getch();
}

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.h>
#include<conio.h>
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;
}
};
void main() {
Employee e1(101, "Sonoo", 890000); //creating an object of
Employee
4|Page

Employee e2=Employee(102, "Nakul", 59000);


e1.display();
e2.display();
getch();
}

Output:

101 Sonoo 890000


102 Nakul 59000

Copy Constructor:

A copy constructor is a member function that initializes an object


using another object of the same class. A 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.

Copy constructor takes a reference to an object of the same class as


an argument.

Sample(Sample &t)

id=t.id;

}
// Example: Explicit copy constructor

#include <iostream.h>
#include<conio.h>
class Sample
5|Page

{
int id;
public:
void input(int x)
{
id=x;
}
Sample()
{
} //default constructor with empty body

Sample(Sample &t) //copy constructor


{
id=t.id;
}
void display()
{
cout<<endl<<"ID="<<id;
}
};
int main()
{
Sample obj1;
obj1.input(10);
obj1.display();
Sample obj2(obj1); //or obj2=obj1; copy constructor
called
obj2.display();
return 0;
}

You might also like