Constructors in C++ - GeeksforGeeks
Constructors in C++ - GeeksforGeeks
Constructors in C++ - GeeksforGeeks
DSA Data Structures Algorithms Interview Preparation Data Science Topic-wise Practice
Constructors in C++
Difficulty Level :
Easy ● Last Updated :
22 Aug, 2022
Read Discuss
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. Constructor is
invoked at the time of object creation. It constructs the values i.e. provides data for
Constructor does not have a return value, hence they do not have a return type.
<class-name> (list-of-parameters);
Example
C++
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
that you have read and understood our
Cookie Policy &
Privacy Policy
// defining the constructor within the class
Got It !
https://www.geeksforgeeks.org/constructors-c/?ref=lbp 1/21
11/16/22, 8:34 PM Constructors in C++ - GeeksforGeeks
#include <iostream>
Start
Your Coding Journey Now!
using namespace std; Login Register
class student {
int rno;
char name[10];
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;
}
};
int main()
{
student s; // constructor gets called automatically when
// we create the object of the class
s.display();
return 0;
}
Output
0 6.95303e-310
Example
C++
https://www.geeksforgeeks.org/constructors-c/?ref=lbp 2/21
11/16/22, 8:34 PM Constructors in C++ - GeeksforGeeks
char name[50];
Start
Your Coding Journey Now!
double fee; Login Register
public:
student();
void display();
};
student::student()
{
cout << "Enter the RollNo:";
cin >> rno;
cout << "Enter the Name:";
cin >> name;
cout << "Enter the Fee:";
cin >> fee;
}
void student::display()
{
cout << endl << rno << "\t" << name << "\t" << fee;
}
int main()
{
student s;
s.display();
return 0;
}
Output :
30 ram 20000
C++
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
that you have read and understood our
Cookie Policy &
Privacy Policy
#include <iostream>
using namespace std; Got It !
https://www.geeksforgeeks.org/constructors-c/?ref=lbp 3/21
11/16/22, 8:34 PM Constructors in C++ - GeeksforGeeks
Start Your
class Line
public:
{ Coding Journey Now! Login Register
void setLength( double len );
double getLength( void );
Line( double len ); //This is the constructor
private:
double length;
};
//Member function definition including constructor
Line::Line( double len ) {
cout<<"Object is being created , length ="<< len <<endl;
length = len;
}
void Line::setLength( double len ) {
length = len;
}
double Line::getLength( void ) {
return length;
}
//Main function for the program
int main() {
Line line(10.0);
//get initially set length
cout<<"Length of line :" << line.getLength() << endl;
//set line length again
line.setLength(6.0);
cout<<"Length of line :" << line.getLength() << endl;
return 0;
}
Default Constructors don’t have input argument however, Copy and Parameterized
We If
usewe
cookies to ensure
do not you yhave
specif the best browsingC++
a constructor, experience on our generates
compiler website. By using our site, you
acknowledge
a default constructor
Got It !
https://www.geeksforgeeks.org/constructors-c/?ref=lbp 4/21
11/16/22, 8:34 PM Constructors in C++ - GeeksforGeeks
Login Register
Suppose you went to a shop to buy a marker. When you want to buy a marker, what
are the options. The first one you go to a shop and say give me a marker. So just
saying give me a marker mean that you did not set which brand name and which color,
you didn’t mention anything just say you want a marker. So when we said just I want a
marker so whatever the frequently sold marker is there in the market or in his shop he
will simply hand over that. And this is what a default constructor is! The second
method is you go to a shop and say I want a marker a red in color and X Y Z brand. So
you are mentioning this and he will give you that marker. So in this case you have
given the parameters. And this is what a parameterized constructor is! Then the third
one you go to a shop and say I want a marker like this(a physical marker on your
hand). So the shopkeeper will see that marker. Okay, and he will give a new marker
for you. So copy of that marker. And that ’s what a copy constructor is!
Constructors are mostly declared in the public section of the class though it can be
Constructors do not return values; hence they do not have a return type.
A constructor gets called automatically when we create the object of the class.
Got It !
https://www.geeksforgeeks.org/constructors-c/?ref=lbp 5/21
11/16/22, 8:34 PM Constructors in C++ - GeeksforGeeks
CPP
a: 10
b: 20
Note : Even if we do not define any constructor explicitly, the compiler will
C++
// Example
#include<iostream>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
student() // Explicit Default constructor
{
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;
}
};
int main()
{
student s;
s.display();
return 0;
}
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
that you have read and understood our
Cookie Policy &
Privacy Policy
Got It !
https://www.geeksforgeeks.org/constructors-c/?ref=lbp 7/21
11/16/22, 8:34 PM Constructors in C++ - GeeksforGeeks
Login Register
Typically, these arguments help initialize an object when it is created. 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
constructor is defined explicitly, the compiler will not implicitly call the default
Student s;
CPP
Start Your
return
}
0; Coding Journey Now! Login Register
Output
C++
// Example
#include<iostream>
#include<string.h>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
student(int,char[],double);
void display();
};
student::student(int no,char n[],double f)
{
rno=no;
strcpy(name,n);
fee=f;
}
void student::display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}
int main()
{
student s(1001,"Ram",10000);
s.display();
return 0;
}
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
that you have read and understood our
Cookie Policy &
Privacy Policy
Got It !
https://www.geeksforgeeks.org/constructors-c/?ref=lbp 9/21
11/16/22, 8:34 PM Constructors in C++ - GeeksforGeeks
Login Register
be passed as arguments to the constructor function. The normal way of object
declaration may not work. The constructors can be called explicitly or implicitly.
3. Copy Constructor:
A copy constructor is a member function that initializes an object using another object
the compiler will not provide a default constructor in this case. However, it is not
constructor.
Sample(Sample &t)
id=t.id;
CPP
// use
We Illustration
cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
#include <iostream> that you have read and understood our
Cookie Policy &
Privacy Policy
using namespace std;
Got It !
https://www.geeksforgeeks.org/constructors-c/?ref=lbp 10/21
11/16/22, 8:34 PM Constructors in C++ - GeeksforGeeks
class point {
Start Your Coding Journey Now!
private:
double x, y;
Login Register
public:
// Non-default Constructor &
// default Constructor
point(double px, double py) { x = px, y = py; }
};
int main(void)
{
// Define an array of size
// 10 & of type point
// This line will cause error
point a[10];
// Remove above line and program
// will compile without error
point b = point(5, 6);
}
Output :
C++
Sample obj1;
Start Your Coding Journey Now!
obj1.init(10);
obj1.display();
Login Register
Sample obj2(obj1); //or obj2=obj1;
obj2.display();
return 0;
}
Output
ID=10
ID=10
C++
https://www.geeksforgeeks.org/constructors-c/?ref=lbp 12/21
11/16/22, 8:34 PM Constructors in C++ - GeeksforGeeks
}
Start Your Coding Journey Now! Login Register
Output
ID=10
ID=10
C++
#include<iostream>
#include<string.h>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
student(int,char[],double);
student(student &t) //copy constructor
{
rno=t.rno;
strcpy(name,t.name);
fee=t.fee;
}
void display();
};
student::student(int no,char n[],double f)
{
rno=no;
strcpy(name,n);
fee=f;
}
void student::display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}
int main()
{ use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
We
student s(1001,"Manjeet",10000);
that you have read and understood our
Cookie Policy &
Privacy Policy
s.display();
Got It !
https://www.geeksforgeeks.org/constructors-c/?ref=lbp 13/21
11/16/22, 8:34 PM Constructors in C++ - GeeksforGeeks
C++
#include<iostream>
#include<string.h>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
student(int,char[],double);
student(student &t) //copy constructor (member wise initialization)
{
rno=t.rno;
strcpy(name,t.name);
}
void display();
void disp()
{
cout<<endl<<rno<<"\t"<<name;
}
};
student::student(int no, char n[],double f)
{
rno=no;
strcpy(name,n);
fee=f;
}
void student::display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}
int main()
{ use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
We
student s(1001,"Manjeet",10000);
that you have read and understood our
Cookie Policy &
Privacy Policy
s.display();
Got It !
https://www.geeksforgeeks.org/constructors-c/?ref=lbp 14/21
11/16/22, 8:34 PM Constructors in C++ - GeeksforGeeks
Destructor:
the class objects created by the constructor. Destructor has the same name as their
class name preceded by a tilde (~) symbol. It is not possible to define more than one
destructor. The destructor is only one way to destroy the object created by the
any argument nor returns any value. It is automatically called when the object goes
out of scope. Destructors release memor y space occupied by the objects created by
the constructor. In destructor, objects are destroyed in the reverse of object creation.
~ <class-name>()
<class-name>: : ~ <class-name>(){}
C++
#include <iostream>
using namespace std;
class Test {
public:
Test() { cout << "\n Constructor executed"; }
~Test() { cout << "\n Destructor executed"; }
};
main()
We
{ use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
Test t; that you have read and understood our
Cookie Policy &
Privacy Policy
Got It !
return 0;
https://www.geeksforgeeks.org/constructors-c/?ref=lbp 15/21
11/16/22, 8:34 PM Constructors in C++ - GeeksforGeeks
}
Start Your Coding Journey Now! Login Register
Output
Constructor executed
Destructor executed
C++
#include <iostream>
using namespace std;
class Test {
public:
Test() { cout << "\n Constructor executed"; }
~Test() { cout << "\n Destructor executed"; }
};
main()
{
Test t, t1, t2, t3;
return 0;
}
Output
Constructor executed
Constructor executed
Constructor executed
Constructor executed
Destructor executed
Destructor executed
Destructor executed
Destructor executed
C++
#include <iostream>
We use cookies
using to ensurestd;
namespace you have the best browsing experience on our website. By using our site, you
acknowledge
that you have read and understood our
Cookie Policy &
Privacy Policy
int count = 0;
class Test { Got It !
public:
https://www.geeksforgeeks.org/constructors-c/?ref=lbp 16/21
11/16/22, 8:34 PM Constructors in C++ - GeeksforGeeks
Test()
Start Your Coding Journey Now!
{
count++;
Login Register
cout << "\n No. of Object created:\t" << count;
}
~Test()
{
cout << "\n No. of Object destroyed:\t" << count;
--count;
}
};
main()
{
Test t, t1, t2, t3;
return 0;
}
Output
Characteristic s of a destructor:-
constructor goes out of scope and releases the memor y space that is no longer
2. Destructor neither requires any argument nor returns any value therefore it cannot
be overloaded.
Got It !
https://www.geeksforgeeks.org/constructors-c/?ref=lbp 17/21
11/16/22, 8:34 PM Constructors in C++ - GeeksforGeeks
I. Default constructor
IV. Destructor
Related Ar ticles :
Destructors in C++
Please write comments if you find anything incorrect, or if you want to share more
Like 420
Previous Next
Related Articles
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
1. When are Constructors Called?
that you have read and understood our
Cookie Policy &
Privacy Policy
Got It !
https://www.geeksforgeeks.org/constructors-c/?ref=lbp 18/21
11/16/22, 8:34 PM Constructors in C++ - GeeksforGeeks
Ar ticle Contributed By :
GeeksforGeeks
Current difficulty :
Easy
Article Tags :
Start Your Coding Journey Now! Login Register
Practice Tags : CPP
Company Learn
About Us Algorithms
Careers Data Structures
In Media SDE Cheat Sheet
Contact Us Machine learning
Privacy Policy CS Subjects
Copyright Policy Video Tutorials
Courses
News Languages
Top News
Python
Technology
Java
Work & Career
CPP
Business
Golang
Finance
C#
Lifestyle
SQL
Knowledge
Kotlin
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
Web Development Contribute
that you have read and understood our
Cookie Policy &
Privacy Policy
https://www.geeksforgeeks.org/constructors-c/?ref=lbp 20/21
11/16/22, 8:34 PM Constructors in C++ - GeeksforGeeks
@geeksforgeeks
, Some rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge
that you have read and understood our
Cookie Policy &
Privacy Policy
Got It !
https://www.geeksforgeeks.org/constructors-c/?ref=lbp 21/21