Introduction to Constructors in C++ - Definition, Syntax and Characteristics

A constructor is a special member function of a class in C++ that is automatically called when an object of the class is created. It is mainly used to initialize the data members of an object. In C++, constructors make object initialization easier and help ensure that objects begin their lifetime with appropriate initial values.

कंस्ट्रक्टर C++ में किसी क्लास का एक विशेष मेंबर फंक्शन होता है, जिसे क्लास का ऑब्जेक्ट बनाते समय अपने आप कॉल किया जाता है। इसका मुख्य उपयोग ऑब्जेक्ट के data members को initialize करने के लिए किया जाता है।

What is a Constructor in C++?

A constructor is a special member function whose name is the same as the class name. It does not have any return type, not even void. When an object is created, the constructor is automatically invoked by the compiler to initialize that object.

Definition: A constructor in C++ is a special member function that is automatically executed when an object of a class is created.

परिभाषा: C++ में कंस्ट्रक्टर एक विशेष मेंबर फंक्शन है, जिसका नाम क्लास के नाम के समान होता है और जो ऑब्जेक्ट बनाते समय अपने आप execute होता है।

Why Do We Need Constructors in C++?

Suppose we create a Student class containing two data members: rollNo and marks. 

Without a constructor, we may need to assign values to these members separately after creating an object.

कंस्ट्रक्टर के बिना ऑब्जेक्ट बनाने के बाद data members को अलग-अलग initialize करना पड़ सकता है।

A constructor allows us to initialize the data members automatically when an object is created.

कंस्ट्रक्टर की सहायता से ऑब्जेक्ट बनाते समय ही उसके data members को initialize किया जा सकता है।

Syntax of a Constructor in C++

class ClassName
{
public:
ClassName()
{
// Initialization statements
}
};

Here:

  • ClassName is the name of the class.

  • ClassName() is the constructor.

  • The constructor is declared inside the class.

  • It does not have a return type.

यहाँ ClassName() कंस्ट्रक्टर है। इसका नाम क्लास के नाम के समान है और इसके पहले कोई return type नहीं लिखा जाता है।

Example 1: Simple Constructor in C++

The following program demonstrates how a constructor initializes the data members of a class.

#include <iostream>
using namespace std;
class Student
{
private:
int rollNo;
float marks;
public:
Student()
{
rollNo = 101;
marks = 85.5;
}
void display()
{
cout << "Roll Number: " << rollNo << endl;
cout << "Marks: " << marks << endl;
}
};
int main()
{
Student s1;
s1.display();
return 0;
}

Output:

Roll Number: 101
Marks: 85.5

Explanation:

  1. The Student class contains two private data members: rollNo and marks.

  2. The Student() constructor initializes rollNo to 101 and marks to 85.5.

  3. In main(), the statement Student s1; creates an object.

  4. The constructor is automatically called when the object is created.

  5. The display() function displays the initialized values.

व्याख्या:

  1. Student क्लास में दो private data members हैं: rollNo और marks।

  2. Student() कंस्ट्रक्टर इन दोनों variables को प्रारंभिक मान देता है।

  3. Student s1; स्टेटमेंट से ऑब्जेक्ट बनाया जाता है।

  4. ऑब्जेक्ट बनते ही कंस्ट्रक्टर अपने आप कॉल होता है।

  5. display() फंक्शन इन values को स्क्रीन पर प्रदर्शित करता है।

Example 2: Constructor Called Automatically

This program demonstrates that a constructor is automatically called whenever an object is created.

#include <iostream>
using namespace std;
class Demo
{
public:
Demo()
{
cout << "Constructor is called." << endl;
}
};
int main()
{
cout << "Creating first object..." << endl;
Demo d1;
cout << "Creating second object..." << endl;
Demo d2;
return 0;
}

Output:

Creating first object...
Constructor is called.
Creating second object...
Constructor is called.

Explanation:

In this program, two objects, d1 and d2, are created. The constructor is called separately for each object.

इस प्रोग्राम में d1 और d2 नाम के दो ऑब्जेक्ट बनाए गए हैं। प्रत्येक ऑब्जेक्ट के निर्माण पर कंस्ट्रक्टर अलग-अलग कॉल होता है।

Characteristics of Constructors in C++

The main characteristics of constructors are:

  1. Same Name as Class: A constructor must have the same name as its class.
    कंस्ट्रक्टर का नाम हमेशा क्लास के नाम के समान होता है।

  2. No Return Type: A constructor does not have any return type, including void.
    कंस्ट्रक्टर का कोई return type नहीं होता है, यहाँ तक कि void भी नहीं।

  3. Automatic Invocation: It is automatically called when an object is created.
    ऑब्जेक्ट बनाते समय कंस्ट्रक्टर अपने आप कॉल होता है।

  4. Used for Initialization: It is mainly used to initialize data members of an object.
    इसका मुख्य उपयोग ऑब्जेक्ट के data members को initialize करने के लिए होता है।

  5. Can Be Overloaded: A class can have multiple constructors with different parameter lists.
    एक क्लास में अलग-अलग parameters वाले एक से अधिक constructors हो सकते हैं।

  6. Can Have Default Arguments: Constructors can be declared with default arguments.
    कंस्ट्रक्टर में default arguments का उपयोग किया जा सकता है।

  7. Cannot Be Inherited: Constructors are not inherited as ordinary member functions, although a derived class can invoke a base-class constructor.
    कंस्ट्रक्टर सामान्य member functions की तरह inherit नहीं होते, हालाँकि derived class के constructor से base class का constructor कॉल किया जा सकता है।

  8. Cannot Be Virtual: Constructors cannot be declared virtual.
    कंस्ट्रक्टर को virtual घोषित नहीं किया जा सकता है।

  9. Cannot Be Static: Constructors cannot be declared static.
    कंस्ट्रक्टर को static घोषित नहीं किया जा सकता है।

  10. Can Be Public, Protected or Private: A constructor can have any of these access specifiers, depending on the design of the class.
    क्लास की आवश्यकता के अनुसार कंस्ट्रक्टर को public, protected या private बनाया जा सकता है।

Types of Constructors in C++

C++ supports different forms of constructors. The commonly studied types are:

  1. Default Constructor: A constructor that can be called without providing arguments.

  2. Parameterized Constructor: A constructor that accepts parameters to initialize an object.

  3. Copy Constructor: A constructor that initializes an object using another object of the same class.

  4. Constructor Overloading: Defining multiple constructors with different parameter lists.

  5. Dynamic Constructor: A traditional textbook term for a constructor that performs dynamic memory allocation.

C++ also supports constructors with default arguments and other advanced forms.

C++ में विभिन्न प्रकार के constructors का उपयोग किया जाता है, जैसे Default Constructor, Parameterized Constructor और Copy Constructor। इनके अलावा constructor overloading और dynamic memory allocation भी महत्वपूर्ण विषय हैं।

Each type of constructor will be discussed with separate programs and examples in the upcoming articles.

Constructor vs Ordinary Member Function

ConstructorOrdinary Member Function
Name is the same as the class name.Can have any valid function name.
Has no return type.May have a return type, including void.
Automatically called during object initialization.Generally called explicitly.
Mainly used to initialize an object.Used to perform operations.
Cannot be virtual or static.Can be virtual or static, subject to language rules.

Example:

class Demo
{
public:
Demo()
{
cout << "Constructor called" << endl;
}
void display()
{
cout << "Member function called" << endl;
}
};

In this example, Demo() is a constructor, whereas display() is an ordinary member function.

इस उदाहरण में Demo() एक constructor है, जबकि display() एक सामान्य member function है।

Frequently Asked Questions (FAQs)

1. What is a constructor in C++?

A constructor is a special member function that is automatically called when an object is created. It is mainly used to initialize objects.

2. Why does a constructor have the same name as the class?

C++ uses the class name to identify its constructor. This allows the language to invoke the appropriate constructor during object creation.

3. Does a constructor have a return type?

No. A constructor does not have any return type, not even void.

4. Can we define more than one constructor in a class?

Yes. A class can have multiple constructors with different parameter lists. This is known as constructor overloading.

5. Can a constructor be private?

Yes. A constructor can be private. Private constructors are useful in designs such as singleton classes and controlled object creation.

6. Can a constructor be virtual?

No. Constructors cannot be virtual in C++.

7. Is a constructor called automatically?

Yes. A suitable constructor is automatically invoked when an object is created.

Constructors are an essential feature of object-oriented programming in C++. They help initialize objects automatically and make class-based programs easier to organize and maintain. Understanding constructors is the foundation for learning default constructors, parameterized constructors, copy constructors and constructor overloading.

C++ में constructors object initialization को सरल बनाते हैं। इनके विभिन्न प्रकारों को समझना object-oriented programming सीखने के लिए आवश्यक है।

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

C++ Programming Syllabus Complete - From Basics to Advanced C++, including OOP and Data Structures and Algorithms (DSA)

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)