🔷 What is a Member Function?
A member function is a function declared inside a class. It operates on the data members of that class and defines the behavior of objects created from the class.
🔷 Different Forms of Member Functions in C++
Member functions can take various forms depending on how they are used and defined. Here’s a breakdown of the most commonly used forms:
🔶 1. Simple Member Function
A regular function that performs actions or returns values.
✅ Example:
class Student {
public:
void display() {
cout << "This is a simple member function." << endl;
}
};
🔶 2. Parameterized Member Function
Takes parameters to work with different input values.
✅ Example:
class Student {
public:
void setData(int roll, string name) {
cout << "Roll No: " << roll << ", Name: " << name << endl;
}
};
🔶 3. Return Type Member Function
Returns a value after performing a task.
✅ Example:
class Student {
public:
int getRollNumber() {
return 101;
}
};
🔶 4. Inline Member Function
Defined inside the class, making it automatically inline
(compiler may insert the function code directly into calling code for speed).
✅ Example:
class Student {
public:
void show() {
cout << "This is an inline function." << endl;
}
};
🔶 5. Outside Class Definition (Using Scope Resolution Operator ::
)
Defined outside the class to keep class declaration clean and concise.
✅ Example:
class Student {
public:
void display(); // Declared only
};
void Student::display() {
cout << "Defined outside the class." << endl;
}
🔶 6. Const Member Function
Used when a function does not modify any data members. Useful for read-only operations.
✅ Example:
class Student {
public:
int rollNo;
int getRollNo() const {
return rollNo;
}
};
🔶 7. Static Member Function
Belongs to the class, not to any specific object. Can be called without creating an object. Can only access static members.
✅ Example:
class Student {
public:
static void display() {
cout << "Static function called." << endl;
}
};
int main() {
Student::display(); // Called without an object
}
🔶 8. Friend Function (Not technically a member)
A function declared as friend can access private/protected data of a class but is not a member of that class.
✅ Example:
class Student {
private:
int rollNo = 101;
public:
friend void showRoll(Student s);
};
void showRoll(Student s) {
cout << "Roll No: " << s.rollNo << endl;
}
🔷 Summary Table
Form | Description | Access to Members | Called Using Object? |
---|---|---|---|
Simple | Regular function | Yes | Yes |
Parameterized | Accepts parameters | Yes | Yes |
Return Type | Returns values | Yes | Yes |
Inline | Defined inside the class | Yes | Yes |
Defined Outside Class | Defined outside using :: | Yes | Yes |
Const | Cannot modify members | Yes | Yes |
Static | Belongs to class, not object | Only static | No (use ClassName) |
Friend | External function with access to private data | Yes | No (called normally) |
🔷 Summary
- Member functions define object behavior.
- They come in different forms: simple, parameterized, static, const, etc.
- Some are used for data modification, others for read-only access or utility purposes.
- Good design uses appropriate member function forms for clarity, safety, and performance.