←Back to Subject
Notes
Notes
Study material and explanations
Diagrams
Diagrams
Visualize concepts easily
Mnemonics
Mnemonics
Tricks to remember faster
One Night Prep
One Night Prep
Short and focused revision
Revision
Revision
Quick summary and key points
Important Questions
Important Questions
Practice most relevant questions
Notes

Defining Member Functions in C++: Outside Inlining & Nesting

Study material and explanations for Unit 3

Defining Member Functions in C++: Outside Inlining & Nesting

1. Two Ways to Define Member Functions (In Simple Words)

In C++, when you write a function for a class, you can write its code in two different places:

Member Function Definition Styles
Method 1: Inside the Class
You write the full code body right inside the class { ... } block. The compiler automatically treats it as inline by default. Best for small 1–2 line functions.
Method 2: Outside the Class
You only write the prototype (declaration) inside the class, and write the full code outside using the Scope Resolution Operator (::). Keeps the class neat and clean!

2. Defining Member Functions Outside Using ::

When you write the function code outside the class body, how does the compiler know which class it belongs to?

You must use the Scope Resolution Operator (::):

return_type ClassName :: functionName(parameters) {
    // Function code
}

Complete Code Example:

#include <iostream>
using namespace std;

class Rectangle {
    private:
        float length;
        float breadth;

    public:
        // Prototype declarations INSIDE the class
        void setDimensions(float, float);
        float calculateArea();
};

// 1. Defining outside using ClassName::
void Rectangle::setDimensions(float l, float b) {
    length = l;
    breadth = b;
}

// 2. Defining outside using ClassName::
float Rectangle::calculateArea() {
    return length * breadth;
}

int main() {
    Rectangle rect;
    rect.setDimensions(10.0f, 5.0f);
    cout << "Area of Rectangle: " << rect.calculateArea() << endl; // Prints 50
    return 0;
}

3. Making an Outside Function Inline

By default, any function defined outside the class is treated as a normal jumping function.

If you want the speed of an inline function while keeping your class definition uncluttered, simply add the inline keyword before the outside function header:

class MathBox {
    public:
        int square(int);
};

// Add 'inline' before return type
inline int MathBox::square(int x) {
    return x * x;
}

4. Nesting of Member Functions

Normally, when you call a member function from main(), you have to use an object and the dot operator: obj.display();.

However, a member function can call another member function of the same class directly by name, without needing any object or dot operator! This is called Nesting of Member Functions.

#include <iostream>
using namespace std;

class NumberAnalyzer {
    private:
        int num1, num2;

    public:
        void inputNumbers() {
            cout << "Enter two numbers: ";
            cin >> num1 >> num2;
        }

        // Function 1: Finds the larger number
        int findLargest() {
            return (num1 > num2) ? num1 : num2;
        }

        // Function 2: NESTED CALL to findLargest()
        void displayResult() {
            // Calling findLargest() directly without any dot operator!
            int maxVal = findLargest(); 
            cout << "The largest number is: " << maxVal << endl;
        }
};

int main() {
    NumberAnalyzer na;
    na.inputNumbers();
    na.displayResult(); // displayResult internally calls findLargest()
    return 0;
}

5. Private Member Functions (Internal Helper Functions)

Usually, member functions are placed in public: so they can be called from main().

But what if you have a sensitive helper function (like checking an ATM PIN) that outside users should never be allowed to call directly? You can place it in the private: section!

A Private Member Function cannot be called from main() using obj.func() (the compiler will give an error). It can only be called internally by other public member functions of the same class.

#include <iostream>
using namespace std;

class BankAccount {
    private:
        double balance;

        // PRIVATE HELPER FUNCTION: Secret internal PIN check
        bool verifyPin(int pin) {
            return (pin == 1234); // Only class methods can call this
        }

    public:
        BankAccount(double b) { balance = b; }

        void withdrawMoney(double amount, int pin) {
            // Public function calls the private function internally!
            if (verifyPin(pin)) {
                balance -= amount;
                cout << "Withdrawal successful! Remaining: Rs. " << balance << endl;
            } else {
                cout << "Error: Wrong ATM PIN!" << endl;
            }
        }
};

int main() {
    BankAccount account(10000.0);

    // account.verifyPin(1234); // COMPILER ERROR! Cannot call private function!
    account.withdrawMoney(2000.0, 1234); // OK: calls verifyPin internally
    return 0;
}

DAVV Exam Preparation Tips

Frequently Asked 5-Mark Questions:

  1. How do you define a member function outside a class? What is the role of ::?
    Model Answer:
    • Syntax: return_type ClassName :: functionName(parameters) { }
    • Role of ::: It tells the compiler that the function belongs to the specific class, giving it access to private data members.
  2. What are private member functions? Give a real-life example.
    Model Answer:
    • Functions declared under private: that cannot be called directly from main().
    • They are called internally by public member functions.
    • Example: Verifying ATM PIN or OTP check before processing a bank withdrawal.

Frequently Asked 10-Mark Questions:

  1. Explain Outside Member Function Definition, Outside Inlining, Nesting of Member Functions, and Private Member Functions with clear C++ code examples.
    Model Answer:
    • Explain inside vs outside definition with pros and cons.
    • Show how inline can be added to an outside definition.
    • Explain and write code for Nesting (one function calling another inside the class without dot .).
    • Explain and write code for Private Member Functions with a PIN verification example.