Member Functions: Inside vs Outside the Class

Two ways to write the code for a class's functions.

Member Functions: Inside vs Outside the Class

Member Function वह function होता है जो किसी class का member होता है और class के data members पर operations perform कर सकता है। C++ में member functions को मुख्य रूप से दो तरीकों से define किया जा सकता है—Inside the Class और Outside the Class

Member Function Definition: Class के अंदर define करना → Inside the Class
Class के बाहर define करना → Outside the Class

What is a Member Function?

Class के अंदर define किया गया function जो class के data members के साथ काम करता है, उसे member function कहा जाता है। Member function object के माध्यम से call किया जा सकता है।

उदाहरण:

class Student
{
public:
    int marks;

    void display()
    {
        cout << marks;
    }
};

यहाँ display() एक member function है। यह Student class के marks data member को access कर रहा है।

Defining Member Function Inside the Class

जब member function की पूरी definition class के अंदर ही लिखी जाती है, तो इसे member function defined inside the class कहा जाता है।

इस method में function का return type, name, parameters और body class के braces के अंदर लिखे जाते हैं।

class Student
{
public:
    int marks;

    void display()
    {
        cout << "Marks: " << marks;
    }
};

यहाँ display() function की पूरी definition Student class के अंदर है।

Example of Function Defined Inside the Class

#include <iostream>
using namespace std;

class Student
{
public:
    int marks;

    void display()
    {
        cout << "Marks: " << marks;
    }
};

int main()
{
    Student s;

    s.marks = 85;
    s.display();

    return 0;
}
Output:
Marks: 85

यहाँ display() function class के अंदर define किया गया है और object s के माध्यम से call किया गया है।

Defining Member Function Outside the Class

Member function को class के बाहर भी define किया जा सकता है। ऐसी स्थिति में class के अंदर केवल function का declaration किया जाता है और उसकी पूरी definition class के बाहर लिखी जाती है।

Class के बाहर member function define करते समय scope resolution operator (::) का उपयोग किया जाता है।

class Student
{
public:
    void display();
};

void Student::display()
{
    cout << "Hello Student";
}

यहाँ class के अंदर display() का declaration है और class के बाहर Student::display() के रूप में उसकी definition दी गई है।

Syntax of Outside Member Function

Class के बाहर member function define करने का सामान्य syntax इस प्रकार है:

ReturnType ClassName::FunctionName(Parameters)
{
    // Function body
}

यहाँ :: scope resolution operator यह बताता है कि function दिए गए class का member है।

Example of Function Defined Outside the Class

#include <iostream>
using namespace std;

class Student
{
public:
    int marks;

    void display();
};

void Student::display()
{
    cout << "Marks: " << marks;
}

int main()
{
    Student s;

    s.marks = 90;
    s.display();

    return 0;
}
Output:
Marks: 90

यहाँ display() function का declaration class के अंदर है, जबकि उसकी definition class के बाहर Student::display() के रूप में दी गई है।

Inside vs Outside Member Function

Inside the Class Outside the Class
Function की पूरी definition class के अंदर होती है। Class के अंदर declaration और बाहर definition होती है।
Function body सीधे class के अंदर लिखी जाती है। Function definition के साथ scope resolution operator (::) का उपयोग किया जाता है।
छोटे functions के लिए convenient है। बड़े functions के लिए code को organized रखने में उपयोगी है।
Class definition comparatively लंबी हो सकती है। Class definition अधिक compact रखी जा सकती है।

Scope Resolution Operator (::)

Scope Resolution Operator (::) C++ में किसी member को उसके class के scope से identify करने के लिए उपयोग किया जाता है। जब member function को class के बाहर define किया जाता है, तो इसका उपयोग आवश्यक होता है।

void Student::display()
{
    cout << "Student Details";
}

यहाँ Student::display() का अर्थ है कि display() function Student class का member function है।

Remember: Class के बाहर member function define करते समय ClassName और FunctionName के बीच :: operator लगाया जाता है।

Member Function Accessing Data Members

Member function class के data members को directly access कर सकता है। Object के माध्यम से function call करने पर वह उसी object के data members के साथ काम करता है।

#include <iostream>
using namespace std;

class Rectangle
{
public:
    int length;
    int breadth;

    int area();
};

int Rectangle::area()
{
    return length * breadth;
}

int main()
{
    Rectangle r;

    r.length = 10;
    r.breadth = 5;

    cout << "Area: " << r.area();

    return 0;
}
Output:
Area: 50

यहाँ area() function class के बाहर define किया गया है। यह Rectangle class के length और breadth data members को directly access कर रहा है।

Member Function with Parameters

Member functions parameters भी accept कर सकते हैं। यदि function class के बाहर define किया गया है, तो declaration और definition में parameter list compatible होनी चाहिए।

#include <iostream>
using namespace std;

class Calculator
{
public:
    int add(int a, int b);
};

int Calculator::add(int a, int b)
{
    return a + b;
}

int main()
{
    Calculator c;

    cout << "Sum: " << c.add(15, 25);

    return 0;
}
Output:
Sum: 40

यहाँ add() function class के बाहर define किया गया है और दो parameters a तथा b accept करता है।

Multiple Member Functions Outside the Class

एक class के multiple member functions को class के बाहर define किया जा सकता है। प्रत्येक function definition में scope resolution operator का उपयोग किया जाता है।

#include <iostream>
using namespace std;

class Rectangle
{
public:
    int length;
    int breadth;

    int area();
    int perimeter();
};

int Rectangle::area()
{
    return length * breadth;
}

int Rectangle::perimeter()
{
    return 2 * (length + breadth);
}

int main()
{
    Rectangle r;

    r.length = 8;
    r.breadth = 4;

    cout << "Area: " << r.area() << endl;
    cout << "Perimeter: " << r.perimeter();

    return 0;
}
Output:
Area: 32
Perimeter: 24

Advantages of Defining Functions Outside the Class

  • Class definition को छोटा और organized रखा जा सकता है।
  • बड़े member functions की definitions को अलग रखा जा सकता है।
  • Program का structure अधिक readable हो सकता है।
  • Declaration और implementation को अलग रखने में सहायता मिलती है।
  • Large programs में code management आसान हो सकता है।

Advantages of Defining Functions Inside the Class

  • छोटे functions को आसानी से define किया जा सकता है।
  • Function declaration और definition एक ही स्थान पर रहती हैं।
  • Simple classes के लिए code लिखना आसान होता है।
  • Function का implementation तुरंत class structure के साथ दिखाई देता है।

Important Difference: Declaration and Definition

Function Declaration compiler को function के नाम, return type और parameters के बारे में बताता है, जबकि Function Definition में function का actual code या body दी जाती है।

उदाहरण:

class Student
{
public:
    void display();   // Declaration
};

void Student::display()   // Definition
{
    cout << "Hello";
}

यहाँ void display(); declaration है और void Student::display() के साथ लिखा गया function body definition है।

Complete Example

निम्न example में एक function class के अंदर और दूसरा function class के बाहर define किया गया है।

#include <iostream>
using namespace std;

class Student
{
public:
    int marks;

    void showName()
    {
        cout << "Name: Rahul" << endl;
    }

    void showMarks();
};

void Student::showMarks()
{
    cout << "Marks: " << marks;
}

int main()
{
    Student s;

    s.marks = 92;

    s.showName();
    s.showMarks();

    return 0;
}
Output:
Name: Rahul
Marks: 92

इस example में showName() class के अंदर define किया गया है, जबकि showMarks() का declaration class के अंदर और definition class के बाहर दी गई है।

Important Points

  • Member function class का member होता है।
  • Member function class के data members पर operations कर सकता है।
  • Member functions को class के अंदर या class के बाहर define किया जा सकता है।
  • Class के बाहर member function define करने के लिए scope resolution operator (::) का उपयोग किया जाता है।
  • Class के अंदर function की पूरी definition लिखी जा सकती है।
  • Class के बाहर function define करने के लिए पहले class में उसका declaration किया जाता है।
  • Class का नाम और member function का नाम :: से separate किया जाता है।
  • एक class के कई member functions class के बाहर define किए जा सकते हैं।
  • Member function अपने class के accessible data members को directly use कर सकता है।

Board Focus

Exam के लिए याद रखें:
Member Function → Class का member function
Inside Class → Function की पूरी definition class के अंदर
Outside Class → Declaration class के अंदर, definition बाहर
Scope Resolution Operator → ::
Syntax → ReturnType ClassName::FunctionName()
Small Functions → Inside class convenient
Large Functions → Outside class useful

Board Important Questions

Very Short Answer Questions

Q1. Member Function क्या है?

Answer: Class का वह function जो class के data members पर operations कर सकता है, member function कहलाता है।

Q2. Member function को class के बाहर define करने के लिए किस operator का उपयोग होता है?

Answer: Scope resolution operator :: का उपयोग होता है।

Q3. Class के अंदर define किए गए member function को क्या कहा जाता है?

Answer: Member function defined inside the class.

Q4. Class के बाहर member function define करने के लिए क्या आवश्यक है?

Answer: Class के अंदर function declaration और class के बाहर scope resolution operator के साथ उसकी definition आवश्यक होती है।

Q5. Scope Resolution Operator क्या है?

Answer: :: को scope resolution operator कहा जाता है।

Short Answer Questions

Q6. Member function को class के अंदर कैसे define किया जाता है?

Answer: Member function की पूरी definition class के braces के अंदर लिखकर उसे class के अंदर define किया जाता है।

class Student
{
public:
    void display()
    {
        cout << "Hello";
    }
};

Q7. Member function को class के बाहर कैसे define किया जाता है?

Answer: पहले class के अंदर member function का declaration किया जाता है। इसके बाद class के बाहर ClassName::FunctionName() syntax का उपयोग करके function की definition लिखी जाती है।

class Student
{
public:
    void display();
};

void Student::display()
{
    cout << "Hello";
}

Q8. Scope Resolution Operator (::) का क्या उपयोग है?

Answer: Scope resolution operator class के बाहर member function की definition करते समय function को उसकी class से associate करने के लिए उपयोग किया जाता है।

Q9. Inside और Outside member function definition में अंतर बताइए।

Inside Outside
Function की definition class के अंदर होती है। Function की definition class के बाहर होती है।
Scope resolution operator की आवश्यकता नहीं होती। Scope resolution operator (::) का उपयोग होता है।
छोटे functions के लिए सुविधाजनक है। बड़े functions और organized code के लिए उपयोगी है।

Q10. Function Declaration और Function Definition में क्या अंतर है?

Answer: Function declaration compiler को function के नाम, return type और parameters की जानकारी देता है, जबकि function definition में function का actual implementation या body दी जाती है।

Long Answer Questions

Q11. Member Function को class के अंदर और बाहर define करने की विधि उदाहरण सहित समझाइए।

Answer: C++ में member functions को दो तरीकों से define किया जा सकता है। पहला तरीका class के अंदर function की पूरी definition लिखना है। दूसरा तरीका class के अंदर function का declaration और class के बाहर उसकी definition लिखना है। बाहर definition देते समय scope resolution operator (::) का उपयोग किया जाता है।

#include <iostream>
using namespace std;

class Student
{
public:
    int marks;

    void displayInside()
    {
        cout << "Marks: " << marks << endl;
    }

    void displayOutside();
};

void Student::displayOutside()
{
    cout << "Marks: " << marks;
}

int main()
{
    Student s;

    s.marks = 90;

    s.displayInside();
    s.displayOutside();

    return 0;
}
Output:
Marks: 90
Marks: 90

Q12. Class के बाहर member function define करने का syntax लिखिए और समझाइए।

Answer: इसका सामान्य syntax है:

ReturnType ClassName::FunctionName(Parameters)
{
    // Function body
}

यहाँ ReturnType function द्वारा return किए जाने वाले data type को, ClassName class के नाम को और FunctionName member function के नाम को दर्शाता है। :: scope resolution operator है जो function को संबंधित class से associate करता है।

Q13. निम्न program का output लिखिए:

#include <iostream>
using namespace std;

class Test
{
public:
    int x;

    void show();
};

void Test::show()
{
    cout << "Value: " << x;
}

int main()
{
    Test t;

    t.x = 25;
    t.show();

    return 0;
}
Output:
Value: 25

Q14. Class के बाहर member functions define करने के advantages लिखिए।

Answer: Class के बाहर member functions define करने से class definition compact और organized रहती है। बड़े functions की implementation को अलग रखा जा सकता है और declaration तथा implementation को अलग-अलग manage करना आसान होता है। Large programs में इससे code readability और maintainability बेहतर हो सकती है।

Quick Revision

  • Member Function: Class का function
  • Inside: पूरी function definition class के अंदर
  • Outside: Declaration class के अंदर और definition class के बाहर
  • Scope Resolution: ::
  • Outside Syntax: ClassName::FunctionName()
  • Inside Functions: छोटे functions के लिए convenient
  • Outside Functions: बड़े और organized code के लिए useful
  • Member Access: Member function class के accessible data members को use कर सकता है
One-Line Revision: C++ में member function को class के अंदर सीधे define किया जा सकता है या class के बाहर scope resolution operator (::) की सहायता से define किया जा सकता है।

Practice Questions

  1. Member Function क्या है?
  2. Member function को class के अंदर कैसे define किया जाता है?
  3. Member function को class के बाहर कैसे define किया जाता है?
  4. Scope Resolution Operator क्या है?
  5. Class के बाहर member function define करने का syntax लिखिए।
  6. Function Declaration और Function Definition में अंतर बताइए।
  7. Inside और Outside member function definition में अंतर बताइए।
  8. Class के बाहर member functions define करने के advantages लिखिए।
  9. एक C++ program लिखिए जिसमें member function class के अंदर define हो।
  10. एक C++ program लिखिए जिसमें member function class के बाहर define हो।
  11. Scope resolution operator का उपयोग करते हुए Rectangle class का area calculate करने का program लिखिए।
  12. एक class में दो member functions define कीजिए और दोनों को class के बाहर implement कीजिए।
  13. एक C++ program लिखिए जिसमें एक member function class के अंदर और दूसरा class के बाहर define किया गया हो।
Lesson 6 of 37
On This Page