Curriculum
Course: Learn C++
Login

Curriculum

Learn C++

Text lesson

Understanding Constructors in C++

In this lesson, you will learn

  • Constructor and its types
  • Default Constructor
  • Defining Constructor Outside the Class
  • Features of Constructor

 

Constructor

  • A constructor is a special member function that is automatically called when an object of a class is created.
  • Constructors have the same name as the class and does not have any return type.
  • Constructors are used to initialize the attributes or properties of an object.

 

There are several types of constructors in C++

ConstructorsInC++

 

Default Constructor

  • A default constructor is one that doesn’t take any arguments.
  • If you don’t provide any constructors for a class, the compiler will automatically generate a default constructor for you.

Syntax: Default Constructor

class Box {
public:
    // Default constructor
    Box() {
        // Initialization code goes here
    }
};

int main() {
    Box obj; // Calls the default constructor
    return 0;
}


Example-1: Default Constructor

#include
using namespace std;
//Declaring a class: Box
class Box {
    double length,width,height;
    public:
        // Default constructor
        Box() {
            // Initialization code goes here
            length=3.0;
            width=4.0;
            height=6.0;
        }
        double volume(){
            return length*width*height;
        }
    };
 
int main() {
    Box b1; // Calls the default constructor
    double vol=b1.volume();
    cout<<"Volume of Box b1: "<<vol;
    return 0;
}


Output:

Volume of Box b1: 72


 

Example -2: Default Constructor Outside the Class

#include<iostream>
using namespace std;

//Declaring a class: Box
class Box {
    double length, width, height; //Private by default
    public:
        /* Default constructor declaration 
        (implementation will be outside teh class)*/
        Box();

        double volume(){
            return length*width*height;
        }
    };
    // Default constructor definition
    Box :: Box(){
        length=4.0;
        width=5.0;
        height=7.0;
    }
int main() {
    Box b1; // Calls the default constructor
    double vol=b1.volume();
    cout<<"Volume of Box b1: "<<vol<<endl;
    return 0;
}


Output:

Volume of Box b1: 72


 

Features of Constructor

  • A constructor should be declared in the public section.
  • A constructor has the same name as the name of the class itself.
  • A constructor is invoked automatically when the object of the class is created.
  • A constructor does not have a return type not even void.
  • Like a C++ function, a constructor can have default arguments.
  • A constructor can not be declared as virtual (‘Virtual’ will be discussed in inheritance)

 

 


End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

5.0
5.0 out of 5 stars (based on 1 review)
Excellent100%
Very good0%
Average0%
Poor0%
Terrible0%

 

 

September 10, 2024

crazyy…..and easy to understand content :>

 

 

Submit a Review

 

 

Layer 1
Login Categories