Constructor in c++
A constructor is a member function of a class which initializes objects of a class. In C++, Constructor is automatically called when object(instance of class) create. It is special member function of the class.
How constructors are different from a normal member function?
A constructor is different from normal functions in following ways:
- Constructor has same name as the class itself
- Constructors don’t have return type
- A constructor is automatically called when an object is created.
- If we do not specify a constructor, C++ compiler generates a default constructor for us (expects no parameters and has an empty body).
Syntax:-
class Wall
{
public Wall() // create a constructor
{
// code
}
};
Note:- If we have not defined a constructor in our class, then the C++ compiler will automatically create a default constructor with an empty code and no parameters.
Types of Constructor:-
1). Default Constructor
A constructor with no parameters is known as a default constructor. In the example above, Wall() is a default constructor.
Example:-
#include <iostream>
using namespace std;
class Wall // declare a class
{
private double length;
public Wall() // create a constructor
{
length = 5.5; // initialize private variables
cout << "Creating a wall." << endl;
cout << "Length = " << length << endl;
}
};
int main()
{
Wall wall1; // create an object
return 0;
}
Output:-
Creating a Wall
Length = 5.5
2). Parameterized Constructor:-
In C++, a constructor with parameters is known as a parameterized constructor. This is the preferred method to initialize member data.
Example:-
#include <iostream>
using namespace std;
// declare a class
class Wall {
private:
double length;
double height;
public Wall(double l, double h) // create parameterized constructor
{
// initialize private variables
length = l;
height = h;
}
double calculateArea() {
return length * height;
}
};
int main() {
// create object and initialize data members
Wall wall1(10.5, 8.6);
Wall wall2(8.5, 6.3);
cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
cout << "Area of Wall 2: " << wall2.calculateArea() << endl;
return 0;
}
Output:-
Area of Wall 1: 90.3
Area of Wall 2: 53.55
3). Copy Constructor
The copy constructor in C++ is used to copy data of one object to another.
Example:-
#include <iostream>
using namespace std;
class Wall {
private:
double length;
double height;
public:
// parameterized constructor
Wall(double len, double hgt) {
// initialize private variables
length = len;
height = hgt;
}
// copy constructor with a Wall object as parameter
Wall(Wall &obj) {
// initialize private variables
length = obj.length;
height = obj.height;
}
double calculateArea() {
return length * breadth;
}
};
int main() {
// create an object of Wall class
Wall wall1(10.5, 8.6);
// print area of wall1
cout << "Area of Room 1: " << wall1.calculateArea() << endl;
// copy contents of room1 to another object room2
Wall wall2 = wall1;
// print area of wall2
cout << "Area of Room 2: " << wall2.calculateArea() << endl;
return 0;
}
Output:-
Area of Room 1: 90.3
Area of Room 2: 90.3
In C++, We can have more than one constructor in a class with same name, as long as each has a different list of arguments.This concept is known as Constructor Overloading and is quite similar to function overloading.
- Overloaded constructors essentially have the same name (name of the class) and different number of arguments.
- A constructor is called depending upon the number and type of arguments passed.
Example:-
#include <iostream>
using namespace std;
class construct
{
public:
float area;
// Constructor with no parameters
construct()
{
area = 0;
}
// Constructor with two parameters
construct(int a, int b)
{
area = a * b;
}
void disp()
{
cout<< area<< endl;
}
};
int main()
{
// Constructor Overloading
// with two different constructors
construct o;
construct o2( 10, 20);
o.disp();
o2.disp();
return 1;
}
Output:-
0
200
Class Destructor
A destructor is a special member function of a class that is executed whenever an object of it's class goes out of scope or whenever the delete expression is applied to a pointer to the object of that class.
A destructor function is called automatically when the object goes out of scope:
(1) the function ends
(2) the program ends
(3) a block containing local variables ends
How destructors are different from a normal member function?
Destructors have same name as the class preceded by a tilde (~)
Destructors don’t take any argument and don’t return anything
Example:-
class String
{
private:
char *s;
int size;
public:
String(char *); // constructor
~String(); // destructor
};
String::String(char *c)
{
size = strlen(c);
s = new char[size+1];
strcpy(s,c);
}
String::~String()
{
delete []s;
}
Note:-
i). There can only one destructor in a class with classname preceded by ~, no parameters and no return type.
ii). If we do not write our own destructor in class, compiler creates a default destructor for us. The default destructor works fine unless we have dynamically allocated memory or pointer in class.
0 Comments