Constructor vs Member Function in C++ - Differences with Examples

In C++, constructors and member functions are important components of a class. Both are defined inside a class and are used to work with objects. However, they perform different tasks. A constructor is a special member function that initializes an object when it is created, whereas an ordinary member function performs operations on an object when it is called.

C++ में Constructor और Member Function दोनों ही class के महत्वपूर्ण components हैं। Constructor का उपयोग object बनाते समय उसे initialize करने के लिए किया जाता है, जबकि Member Function का उपयोग object पर अलग-अलग operations perform करने के लिए किया जाता है।

1. What is a Constructor in C++?

A constructor is a special member function of a class that is automatically called when an object is created. Its primary purpose is to initialize the data members of an object.

A constructor has the same name as its class and does not have a return type, not even void.

C++ में Constructor एक special member function है, जो object create होने पर automatically call होता है। इसका मुख्य उद्देश्य object के data members को initialize करना होता है।

Syntax of Constructor

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

Example of Constructor

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

Output

Constructor called
Roll Number: 101

Explanation

In this program:

  • Student() is the constructor of the Student class.

  • When object s1 is created, the constructor is automatically called.

  • The constructor initializes rollNo with 101.

  • The display() member function is called separately using the object.

इस program में Student s1; statement execute होते ही constructor automatically call होता है और rollNo को 101 से initialize करता है। इसके बाद display() function को object के माध्यम से अलग से call किया जाता है।

2. What is a Member Function in C++?

A member function is an ordinary function declared inside a class. It defines the operations or behaviour of objects and can access the class's data members and other member functions, subject to access rules.

Unlike a constructor, a member function must be called explicitly unless it is invoked by another function or through another language mechanism.

C++ में Member Function एक ordinary function होता है, जिसे class के अंदर declare किया जाता है। इसका उपयोग object के data को access करने और उस पर operations perform करने के लिए किया जाता है।

Syntax of Member Function

class ClassName {
public:
returnType functionName() {
// Function statements
}
};

Example of Member Function

#include <iostream>
using namespace std;
class Calculator {
int a, b;
public:
void setData(int x, int y) {
a = x;
b = y;
}
int add() {
return a + b;
}
};
int main() {
Calculator c1;
c1.setData(10, 20);
cout << "Sum = " << c1.add() << endl;
return 0;
}

Output

Sum = 30

Explanation

In this program:

  • setData() and add() are member functions of the Calculator class.

  • setData() assigns values to the data members.

  • add() calculates and returns their sum.

  • Both functions are explicitly called through object c1.

इस program में setData() और add() दोनों member functions हैं। इन्हें object c1 के माध्यम से explicitly call किया जाता है। setData() values assign करता है और add() उनका sum return करता है।

3. Difference Between Constructor and Member Function in C++

The following table explains the major differences between a constructor and an ordinary member function.

ConstructorMember Function
It is a special member function.It is an ordinary class function.
Its name must be the same as the class name.It can have any valid function name.
It has no return type.It has a return type, such as int, float, or void.
It is automatically invoked when an object is created, as part of its initialization.It is generally called explicitly.
Its main purpose is to initialize an object.Its purpose is to perform operations on an object.
It is not inherited as a constructor by derived classes.Member functions can be inherited, subject to inheritance and access rules.
It cannot be virtual.It can be virtual.
It can be overloaded.It can also be overloaded.
It cannot be static.It can be static.
It cannot be called like an ordinary member function.It can be called using an object or class name for static functions.

4. Can a Constructor Return a Value?

No. A constructor cannot have a return type, including void. Its purpose is to initialize an object rather than return a value.

Example:

class Demo {
public:
Demo() {
// Valid constructor
}
};

The following is invalid:

class Demo {
public:
void Demo() {
// Not a constructor
}
};

In the second example, void Demo() is an ordinary member function, not a constructor, because it has a return type.

C++ में Constructor का कोई return type नहीं होता। यदि class के नाम जैसा function void या किसी अन्य return type के साथ लिखा जाता है, तो वह constructor नहीं बल्कि ordinary member function होता है।

5. Can Constructors Be Overloaded?

Yes. Constructors can be overloaded by defining multiple constructors with different parameter lists.

Example

#include <iostream>
using namespace std;
class Number {
int value;
public:
Number() {
value = 0;
}
Number(int x) {
value = x;
}
void display() {
cout << "Value: " << value << endl;
}
};
int main() {
Number n1;
Number n2(50);
n1.display();
n2.display();
return 0;
}

Output

Value: 0
Value: 50

Explanation

The Number class contains two constructors:

  • Number() initializes value to 0.

  • Number(int x) initializes value with the supplied argument.

The compiler selects the appropriate constructor based on the arguments used during object creation.

इस उदाहरण में दो constructors define किए गए हैं। बिना argument के object बनाने पर पहला constructor और integer argument देने पर दूसरा constructor call होता है। इसे Constructor Overloading कहते हैं।

6. Can Member Functions Be Overloaded?

Yes. Member functions can also be overloaded by defining functions with the same name but different parameter lists.

Example

#include <iostream>
using namespace std;
class Calculator {
public:
int add(int a, int b) {
return a + b;
}
float add(float a, float b) {
return a + b;
}
};
int main() {
Calculator c1;
cout << "Integer Sum: " << c1.add(10, 20) << endl;
cout << "Float Sum: " << c1.add(5.5f, 2.5f) << endl;
return 0;
}

Output

Integer Sum: 30
Float Sum: 8

Explanation

The Calculator class contains two overloaded member functions named add():

  • One accepts two integer arguments.

  • The other accepts two float arguments.

The compiler selects the appropriate function according to the arguments supplied in the function call.

Member Functions को भी overload किया जा सकता है। अलग-अलग parameter lists के आधार पर compiler सही function select करता है।

7. Frequently Asked Questions (FAQs)

Q1. What is the main difference between a constructor and a member function?

A constructor initializes an object when it is created, whereas a member function performs operations on an object when called.

Constructor object को initialize करता है, जबकि Member Function object पर operations perform करता है।

Q2. Is a constructor a member function in C++?

Yes. A constructor is a special member function of a class, but it has special rules and behaviour.

हाँ, Constructor class का special member function होता है।

Q3. Can a constructor have a return type?

No. A constructor cannot have any return type, including void.

नहीं, Constructor का कोई return type नहीं होता।

Q4. Can a member function have the same name as a class?

Yes, but if it has a return type, it is an ordinary member function and not a constructor.

हाँ, लेकिन return type होने पर वह constructor नहीं बल्कि ordinary member function होगा।

Q5. Can constructors and member functions both be overloaded?

Yes. Both can be overloaded by using different parameter lists.

हाँ, Constructors और Member Functions दोनों को अलग-अलग parameter lists के आधार पर overload किया जा सकता है।

Q6. Can a constructor be virtual or static?

No. Constructors cannot be virtual or static. Ordinary member functions can be virtual or static.

नहीं, Constructor को virtual या static नहीं बनाया जा सकता, जबकि ordinary member functions virtual या static हो सकते हैं।

Constructors and member functions are both essential parts of object-oriented programming in C++. A constructor initializes an object automatically during object creation, while member functions define the operations that an object can perform. Understanding their differences helps students design classes correctly and use object-oriented programming concepts effectively.

C++ में Constructor और Member Function की भूमिकाएँ अलग-अलग होती हैं। Constructor object को initialize करता है, जबकि Member Functions object के behaviour और operations को define करते हैं। दोनों के बीच अंतर समझना Classes and Objects तथा 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)