Constructor in C++
C++ Programming

Constructors in C++

 

In C++, a constructor is a special member function of a class that is used to initialize the objects of that class. It is called automatically when an object is created, and its purpose is to set the initial values of the object’s data members or perform any necessary setup operations.

Here is the syntax for defining a Constructor in C++:

class MyClass {

public:

    // Constructor declaration

    MyClass() {

        // Constructor body

        // Initialize data members or perform setup operations

    }

};

In the example above, MyClass is the name of the class, and the constructor has the same name as the class itself. The constructor does not have a return type, not even void.

Constructors can also have parameters to accept arguments during object creation. Such constructors are called parameterized constructors. Here’s an example:

class MyClass {

public:

    // Parameterized constructor declaration

    MyClass(int value) {

        // Constructor body

        // Initialize data members or perform setup operations using the parameter

    }

};

In the example above, the parameterized constructor MyClass takes an integer parameter named value. This allows you to provide an initial value during object creation.

Constructors are useful for ensuring that objects are properly initialized and in a valid state when created. They can also be overloaded, meaning you can have multiple constructors with different sets of parameters, providing flexibility in object creation.

It’s worth noting that if a class does not explicitly define any constructors, C++ automatically generates a default constructor, which initializes the data members to their default values (e.g., zero for numeric types) and performs no additional operations.

Example with output….

#include<iostream.h>

#include<conio.h>

class abc

{

public:

abc()   //constructor means class name and function name always the same

{

cout<<“good”;

}};

void main()

{

clrscr();

abc ob1;

getch();

}

Constructor in C++

The above class has a member function with the same name as its class. Whenever an object of the “abc” type will be created(declared), the compiler will automatically call the constructor. Constructors have no return type, not even void.

#include<iostream.h>

#include<conio.h>

class rectangle

{

private:

int length,breadth,area;

public:

rectangle()

{

length=breadth=0;

}

rectangle(int a)

{

length=breadth=a;

}

rectangle(int a,int b)

{

length = a;

breadth = b;

}

void disp()

{

area=length*breadth;

cout<<“area=”<<area;

}};

void main()

{

rectangle r(0);

rectangle r1(5);

rectangle r2(2,5);

clrscr();

r.disp();

r1.disp();

r2.disp();

getch();

}

Parameterized Constructor in C++

Recommended Posts

C++ Programming

Visibility modes in C++

In C++, visibility modes refer to the accessibility of class members (such as variables and functions) from different parts of a program. C++ provides three visibility modes: public, private, and protected. These modes control the access levels of class members concerning the outside world and derived classes. Public: Members declared as public are accessible from […]

Rekha Setia 
C++ Programming

Inheritance in C++

In C++, inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class based on an existing class, known as the base or parent class. The new class is called the derived or child class. Inheritance facilitates code reuse and supports the creation of a hierarchy of classes. There […]

Rekha Setia 
C++ Programming

C++ Classes and Objects

In C++, classes and objects are fundamental concepts that support object-oriented programming (OOP). Here’s a brief overview of classes and objects in C++: Classes: In C++, a class is a user-defined data type that allows you to encapsulate data members and member functions into a single unit. Classes are the building blocks of object-oriented programming […]

Rekha Setia 

Leave A Comment