Constructors in C++
Constructor is a special member function of a class that initializes the object of the class. Constructor name is same as class name and it doesn't have a return type. Lets take a simple example to understand the working of constructor.
Simple Example: How to use constructor in C++
#include <iostream>
using namespace std;
class constructorDemo{
public:
int num;
char ch;
/* This is a default constructor of the
* class, do note that it's name is same as
* class name and it doesn't have return type.*/
constructorDemo() {
num = 100;
ch = 'A';
} };
int main(){
/* This is how we create the object of class,
* I have given the object name as obj, you can
* give any name, just remember the syntax:
* class_name object_name; */
constructorDemo obj;
/* This is how we access data members using object
* we are just checking that the value we have
* initialized in constructor are reflecting or not. */
cout<<"num: "<<obj.num<<endl;
cout<<"ch: "<<obj.ch;
return 0; }
Output:
num: 100
ch: A
Constructor vs Member function
Now that we know what is constructor, lets discuss how a constructor is different from member function of the class.
1) Constructor doesn't have a return type. Member function has a return type.
2) Constructor is automatically called when we create the object of the class. Member function needs to be called explicitly using object of class.
3) When we do not create any constructor in our class, C++ compiler generates a default constructor and insert it into our code. The same does not apply to member functions.
This is how a compiler generated default constructor looks:
Inheritance
class XYZ {
....
XYZ()
{
//Empty no code
} };
Types of Constructor in C++
There are three types of constructor in C++. 1) Default constructor 2) Parameterized constructor 3) Copy Constructor
1) Default Constructor
#include <iostream>
using namespace std;
class Website{
public:
//Default constructor
Website() {
cout<<"Welcome to Book"<<endl;
} };
int main(void){
/*creating two objects of class Website.
* This means that the default constructor
* should have been invoked twice. */
Website obj1;
Website obj2;
return 0; }
Output
Welcome to Book
Welcome to Book
When you don't specify any constructor in the class, a default constructor with no code (empty body) would be inserted into your code by compiler.
2) Parameterized Constructor
Constructors with parameters are known as Parameterized constructors. These type of constructor allows us to pass arguments while object creation. Lets see how they look:
Lets say class name is XYZ
Default constructor:
XYZ() {
}
....
XYZ obj;
....
Parameterized Constructor:
XYZ(int a, int b) {
}
...
XYZ obj(10, 20);
Example
#include <iostream>
using namespace std;
class Add{
public:
//Parameterized constructor
Add(int num1, int num2) {
cout<<(num1+num2)<<endl;
} };
int main(void){
/* One way of creating object. Also
* known as implicit call to the
* constructor */
Add obj1(10, 20); /*
Another way of creating object. This
* is known as explicit calling the
* constructor. */
Add obj2 = Add(50, 60);
return 0; }
Output
30
110
Copy Constructor
The copy constructor is a constructor which creates creates an object by initializing initializing it with an object of
the same class, which has been created previously. The copy constructor is used to -
1. Initialize one object from another of the same type.
2. Copy an object to pass it as an argument to a function.
3. Copy an object to return it from a function.
If a copy constructor constructor is not defined defined in a class, the compiler compiler itself defines defines one.If the class has
pointer pointer variables variables and has some dynamic dynamic memory allocations, allocations, then it is a must to have a copy
constructor. The most common form of copy constructor is shown here
classname (const classname &obj) {
// body of constructor
}
Example
#include<iostream>
using namespace std;
class Point
{
private:
int x, y;
public:
Point(int x1, int y1) { x = x1; y = y1; }
// Copy constructor
Point(const Point &p2) {x = p2.x; y = p2.y; }
int getX() { return x; }
int getY() { return y; }
};
int main()
{
Point p1(10, 15); // Normal constructor is called here
Point p2 = p1; // Copy constructor is called here
// Let us access values assigned by constructors
cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
cout << "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY();
return 0;
}
Leave Comment