Member Functions
Introduction to Member Functions
Member functions are methods defined within a class that operate on the class's data members. They encapsulate the behavior of objects and provide a way to interact with class data.
Types of Member Functions
graph TD
A[Member Functions] --> B[Regular Methods]
A --> C[Constructors]
A --> D[Destructors]
A --> E[Inline Methods]
A --> F[Const Methods]
Regular Methods
Regular methods perform operations and manipulate class data:
class Calculator {
public:
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
};
Constructors
Constructors initialize object state when an object is created:
class Student {
private:
string name;
int age;
public:
// Default constructor
Student() {
name = "Unknown";
age = 0;
}
// Parameterized constructor
Student(string n, int a) {
name = n;
age = a;
}
};
Destructors
Destructors clean up resources when an object is destroyed:
class DatabaseConnection {
public:
~DatabaseConnection() {
// Close database connection
closeConnection();
}
private:
void closeConnection() {
// Cleanup logic
}
};
Method Types and Characteristics
Method Type |
Description |
Key Characteristics |
Inline Methods |
Defined inside class |
Compiler optimization |
Const Methods |
Cannot modify object state |
Read-only operations |
Static Methods |
Belong to class, not object |
Shared across instances |
Inline Methods
Inline methods are expanded at the call site for performance:
class Point {
public:
// Inline method
inline double getDistance() {
return sqrt(x*x + y*y);
}
private:
double x, y;
};
Const Methods
Const methods guarantee they won't modify object state:
class Rectangle {
public:
// Const method
double getArea() const {
return width * height;
}
private:
double width;
double height;
};
Static Methods
Static methods belong to the class, not specific instances:
class MathUtility {
public:
// Static method
static int square(int x) {
return x * x;
}
// Static method to count instances
static int getInstanceCount() {
return instanceCount;
}
private:
static int instanceCount;
};
Advanced Member Function Techniques
- Method Overloading
- Default Arguments
- Reference and Pointer Parameters
Method Overloading Example
class Print {
public:
void display(int x) {
cout << "Integer: " << x << endl;
}
void display(string s) {
cout << "String: " << s << endl;
}
};
Best Practices
- Keep methods focused and modular
- Use const for read-only methods
- Minimize method complexity
- Follow single responsibility principle
Complete Example in LabEx Environment
#include <iostream>
#include <string>
class BankAccount {
private:
string accountHolder;
double balance;
public:
// Constructor
BankAccount(string name, double initialBalance) {
accountHolder = name;
balance = initialBalance;
}
// Method to deposit
void deposit(double amount) {
balance += amount;
}
// Const method to check balance
double getBalance() const {
return balance;
}
// Method to display account info
void displayInfo() {
cout << "Account Holder: " << accountHolder << endl;
cout << "Balance: $" << balance << endl;
}
};
int main() {
BankAccount account("John Doe", 1000);
account.deposit(500);
account.displayInfo();
return 0;
}
By mastering member functions, developers can create more organized, efficient, and maintainable C++ classes.