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 करना)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: 101Enter Name: RahulStudent DetailsRoll Number: 101Name: Rahul
Explanation:
Studentis a class.rollNoandnameare data members.input()anddisplay()are member functions.Both member functions are defined inside the class.
s1is 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 Classvoid 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: 102Enter Name: AmitStudent DetailsRoll Number: 102Name: Amit
Explanation:
Studentis a class containing data members and member function declarations.input()anddisplay()are declared inside the class.Their definitions are written outside the class.
Student::input()specifies that theinput()function belongs to theStudentclass.Student::display()specifies that thedisplay()function belongs to theStudentclass.
व्याख्या:
Studentएक class है, जिसमें data members और member functions की declarations हैं।input()औरdisplay()को class के अंदर declare किया गया है।इनके definitions class के बाहर लिखे गए हैं।
Student::input()बताता है किinput()function,Studentclass का member है।Student::display()बताता है किdisplay()function,Studentclass का member है।
Difference Between Member Functions Defined Inside and Outside a Class
| Basis | Inside the Class | Outside the Class |
|---|---|---|
| Definition | Function body is written inside the class. | Function body is written outside the class. |
| Declaration | Separate declaration is not required. | Function is generally declared inside the class first. |
| Operator | Scope resolution operator is not required. | Scope resolution operator (::) is used. |
| Readability | Suitable for short functions. | Useful for longer functions and better organization. |
| Inline | Implicitly inline. | Not implicitly inline merely because it is defined outside the class. |
Comments
Post a Comment