C++ Constructor basic concept
In this tutorial we will learnt about C++ constructor basic concept. Tutorial explains you everything about constructor in C++ in easier language.
Table of content
C++ Constructor basic concept
- C++ constructors are the special member function of the class which are used to initialize the objects of that class
- The constructor of class is automatically called immediately after the creation of the object.
- Name of the constructor should be exactly same as that of name of the class.
- C++ constructor is called only once in a lifetime of object when object is created.
- C++ constructors can be overloaded
- C++ constructors does not return any value so constructor have no return type.
- C++ constructor does not return even
void
as return type.
C++ Constructor Types
- Default Constructor
- Parametrized Constructor
- Copy Constructor
We will discuss different C++ constructor types in detail -
C++ Constructor Types #1 : Default Constructor
- If the programmer does not specify the constructor in the program then compiler provides the default constructor.
- In C++ we can overload the default compiler generated constructor
- In both cases (user created default constructor or default constructor generated by compiler), the default constructor is always parameterless.
Syntax
class_name() { ----- ----- }
Example of Default Constructor
Let us take the example of class Marks
which contains the marks of two subjects Maths and Science.
#include<iostream> using namespace std; class Marks { public: int maths; int science; //Default Constructor Marks() { maths=0; science=0; } display() { cout << "Maths : " << maths <<endl; cout << "Science :" << science << endl; } }; int main() { //invoke Default Constructor Marks m; m.display(); return 0; }
Output :
Maths : 0 Science : 0
C++ Constructor Types #2 : Parametrized Constructor
This type of constructor can take the parameters.
Syntax
class_name(Argument_List) { ----- ----- }
Example of Parametrized Constructor
Let us take the example of class ‘Marks’ which contains the marks of two subjects Maths and Science.
#include<iostream> using namespace std; class Marks { public: int maths; int science; //Parametrized Constructor Marks(int mark1,int mark2) { maths = mark1; science = mark2; } display() { cout << "Maths : " << maths <<endl; cout << "Science :" << science << endl; } }; int main() { //invoke Parametrized Constructor Marks m(90,85); m.display(); return 0; }
Output
Maths : 90 Science : 85
C++ Constructor Types #3 : Copy Constructor
- All member values of one object can be assigned to the other object using copy constructor.
- For copying the object values, both objects must belong to same class.
Syntax
class_Name (const class_Name &obj) { // body of constructor }
Example of Copy Constructor
Let us take the example of class ‘Marks’ which contains the marks of two subjects Maths and Science.
#include<iostream> using namespace std; class marks { public: int maths; int science; //Default Constructor marks(){ maths=0; science=0; } //Copy Constructor marks(const marks &obj){ maths=obj.maths; science=obj.science; } display(){ cout<<"Maths : " << maths cout<<"Science : " << science; } }; int main(){ marks m1; /*default constructor gets called for initialization of m1 */ marks m2(const marks &m1); //invoke Copy Constructor m2.display(); return 0; }
Output
Maths : 0 Science : 0