In this lesson, we will cover Constructors and Copy Constructors in C++ under the following topics:
1. What is a Constructor?
A constructor in C++ is special member function of a class that is automatically called whenever a new object is created.
A constructor has the following properties:
- the same name as the class
- does not have a return type
Default constructor: C++ provides a default constructor. This is a constructor that accepts no parameters. For example
class Circle{ public: // create a constructor Circle() { // code } };
Here, the function Circle() is a constructor of the class Circle. The program below show the use of default constructor
// Program to demonstrate default constructor #include <iostream> using namespace std; // declare a class, circle class Circle { private: double radius; public: // create a constructor Circle() { // initialize private variables radius = 7.5; cout << "Creating a circle." << endl; cout << "Radius = " << radius << endl; } }; int main() { // create an object Circle circle; return 0; }
The output is:
Creating a circle. Radius = 7.5
However, not that if we fail to provide a default constructor, C++ compiler will provide one implicitly.
2. Parameterized Constructor
A constructor that accepts parameters is called a parameterized constructor. This is the preferred approach to initializing data members.
The program below illustrates parameterized constructor;
#include <iostream> #include <math.h> /* pow */ using namespace std; // declare a class, circle class Cylinder { private: double radius; double height; public: // create a parameterized constructor Cylinder(double r, double h) { // Initialise the member variables radius = r; height = h; } double calculateVolume(){ return 22/7 * pow(radius,2) * height; } }; int main() { // create a cylinder with parameters Cylinder cylinder1(10.2, 5.3); Cylinder cylinder2(20.5, 2.5); cout <<"Volume 1: " << cylinder1.calculateVolume() <<endl; cout <<"Volume 2: " << cylinder2.calculateVolume() <<endl; return 0; }
Output of the program
Volume 1: 1654.24 Volume 2: 3151.88
Here we created a parameterized constructor Cylinder() with two parameters: radius and height. The values of these parameters are use to initialized the member variables when the Cylinder is instantiated.
3. Copy Constructor
This is a constructor that instantiates an object by copying data from an existing object of the same class.
A copy constructor is used for the following:
- Initialize on object from another object
- copy an object to pass as an argument to a function
- copy an object to return from a function
Similar to normal constructor, if we don’t define a copy constructor, then the C++ compiler will define one implicitly.
A copy constructor for our cylinder object is given below:
// copy constructor with a Cylinder object as parameter Cylinder(Cylinder &obj) { // initialize private variables radius = obj.radius; height = obj.height; }
Notice in the code that the parameter to the copy constructor has the address of an object of the Cylinder class.
Final note: Although a constructor is used to initialize an object, they can also be used to perform certain operations when an object is created.
One thought on “C++ OOP – Constructors”