Member Functions of a class in C++

Member functions are functions declared as members of a class. They can access the class's accessible data members and other member functions. In C++, member functions can be defined in two ways:

Member functions वे functions होते हैं जिन्हें class के members के रूप में declare किया जाता है। ये class के accessible data members और अन्य member functions को access कर सकते हैं। C++ में member functions को दो तरीकों से define किया जा सकता है:

1. Defining Member Functions Inside a Class (Class के अंदर Member Functions define करना )

2. Defining Member Functions Outside a Class ( Class के बाहर Member Functions define करना)

1. Defining Member Functions Inside a Class:

When a member function is defined inside the class, its complete function body is written within the class definition.

जब किसी member function को class के अंदर define किया जाता है, तो उसका पूरा function body class definition के अंदर ही लिखा जाता है।

Syntax

class ClassName
{
public:
return_type functionName(parameters)
{
// Function Body
}
};

Example: Member Function Defined Inside a Class

#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
int rollNo;
string name;
void input()
{
cout << "Enter Roll Number: ";
cin >> rollNo;
cout << "Enter Name: ";
cin >> name;
}
void display()
{
cout << "\nStudent Details" << endl;
cout << "Roll Number: " << rollNo << endl;
cout << "Name: " << name << endl;
}
};
int main()
{
Student s1;
s1.input();
s1.display();
return 0;
}

Output:

Enter Roll Number: 101
Enter Name: Rahul
Student Details
Roll Number: 101
Name: Rahul

Explanation:

  • Student is a class.

  • rollNo and name are data members.

  • input() and display() are member functions.

  • Both member functions are defined inside the class.

  • s1 is an object used to call the member functions.

व्याख्या:

  • Student एक class है।

  • rollNo और name इसके data members हैं।

  • input() और display() इसके member functions हैं।

  • दोनों member functions को class के अंदर define किया गया है।

  • s1 एक object है, जिसका उपयोग member functions को call करने के लिए किया गया है।

Note: A member function defined inside a class definition is implicitly inline, subject to the rules of C++.

ध्यान दें: Class definition के अंदर define किया गया member function C++ के नियमों के अनुसार implicitly inline होता है।

2. Defining Member Functions Outside a Class:

In C++, member functions can also be defined outside the class. In this approach, the function is first declared inside the class and then its complete definition is written outside the class. The scope resolution operator (::) is used to specify that the function belongs to a particular class.

C++ में member functions को class के बाहर भी define किया जा सकता है। इस method में function को पहले class के अंदर declare किया जाता है और फिर उसका complete definition class के बाहर लिखा जाता है। Scope Resolution Operator (::) का उपयोग यह बताने के लिए किया जाता है कि function किसी particular class का member है।

Syntax

class ClassName
{
public:
return_type functionName(parameters);
};
return_type ClassName::functionName(parameters)
{
// Function Body
}

Example: Member Function Defined Outside a Class

#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
int rollNo;
string name;
void input();
void display();
};
// Function Definition Outside the Class
void Student::input()
{
cout << "Enter Roll Number: ";
cin >> rollNo;
cout << "Enter Name: ";
cin >> name;
}
void Student::display()
{
cout << "\nStudent Details" << endl;
cout << "Roll Number: " << rollNo << endl;
cout << "Name: " << name << endl;
}
int main()
{
Student s1;
s1.input();
s1.display();
return 0;
}

Output:

Enter Roll Number: 102
Enter Name: Amit
Student Details
Roll Number: 102
Name: Amit

Explanation:

  • Student is a class containing data members and member function declarations.

  • input() and display() are declared inside the class.

  • Their definitions are written outside the class.

  • Student::input() specifies that the input() function belongs to the Student class.

  • Student::display() specifies that the display() function belongs to the Student class.

व्याख्या:

  • Student एक class है, जिसमें data members और member functions की declarations हैं।

  • input() और display() को class के अंदर declare किया गया है।

  • इनके definitions class के बाहर लिखे गए हैं।

  • Student::input() बताता है कि input() function, Student class का member है।

  • Student::display() बताता है कि display() function, Student class का member है।

Difference Between Member Functions Defined Inside and Outside a Class

BasisInside the ClassOutside the Class
DefinitionFunction body is written inside the class.Function body is written outside the class.
DeclarationSeparate declaration is not required.Function is generally declared inside the class first.
OperatorScope resolution operator is not required.Scope resolution operator (::) is used.
ReadabilitySuitable for short functions.Useful for longer functions and better organization.
InlineImplicitly inline.Not implicitly inline merely because it is defined outside the class.

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)