๐ท 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.
