In C++, static data members and static member functions are class members that have special properties, distinct from regular data members and member functions. The static keyword is used to define members that are shared by all instances of the class rather than each object having its own copy. This is useful when you want data or behavior that is common to all instances of the class.
Let’s break down the concept of static data members and static member functions in detail.
A static data member is a class variable that is shared by all instances of the class. Unlike normal data members, which have separate copies for each object, a static data member has a single copy for the entire class, regardless of how many objects are created.
class ClassName {
public:
static type memberName;
};
#include <iostream>
using namespace std;
class Car {
private:
string brand;
static int count; // Static data member: shared by all Car objects
public:
Car(string b) : brand(b) {
count++; // Increment the static member for each object created
}
static void showCount() {
cout << "Total cars: " << count << endl;
}
};
// Definition of static data member outside the class
int Car::count = 0;
int main() {
Car car1("Toyota");
Car car2("Honda");
Car::showCount(); // Calling static function using class name
return 0;
}
count is a static data member. It keeps track of how many Car objects have been created.int Car::count = 0;.showCount() is a static member function that accesses the static data member and prints the total number of Car objects created.count is static, its value is shared across all instances of the Car class.A static member function is a function that belongs to the class itself rather than to any particular object of the class. This means that static member functions can only access static data members or other static member functions. They cannot access non-static members because they do not operate on any particular instance of the class.
class ClassName {
public:
static returnType functionName(parameters);
};
#include <iostream>
using namespace std;
class Car {
private:
string brand;
static int count; // Static data member
public:
Car(string b) : brand(b) {
count++; // Increment the static member for each object created
}
static void showCount() {
cout << "Total cars: " << count << endl; // Access static member inside static function
}
};
// Definition of static data member outside the class
int Car::count = 0;
int main() {
Car car1("Toyota");
Car car2("Honda");
Car::showCount(); // Calling static function using class name
return 0;
}
showCount() is a static member function. It is called using the class name Car::showCount().showCount() can access the static data member count because it is shared across all instances of the class.brand) because static member functions do not operate on a particular instance of the class.Static Data Members:
Static Member Functions:
this pointer and thus cannot access non-static members directly.#include <iostream>
using namespace std;
class BankAccount {
private:
static int totalAccounts; // Static data member to keep track of total accounts
double balance;
public:
BankAccount(double initialBalance) {
balance = initialBalance;
totalAccounts++; // Increment totalAccounts for each new object
}
// Static member function to access the static data member
static void showTotalAccounts() {
cout << "Total Bank Accounts: " << totalAccounts << endl;
}
void deposit(double amount) {
balance += amount;
}
void displayBalance() const {
cout << "Balance: " << balance << endl;
}
};
// Definition of static data member outside the class
int BankAccount::totalAccounts = 0;
int main() {
BankAccount account1(1000);
BankAccount account2(5000);
account1.deposit(500);
account1.displayBalance();
account2.deposit(2000);
account2.displayBalance();
// Calling static function using class name
BankAccount::showTotalAccounts(); // Output will show 2 accounts
return 0;
}
totalAccounts is a static data member. It keeps track of the number of BankAccount objects created.showTotalAccounts() is a static member function that outputs the total number of BankAccount objects.totalAccounts is shared across all instances of the class, and any modification (like creating a new BankAccount) affects all objects of the class.| Aspect | Static Data Members | Static Member Functions |
|---|---|---|
| Shared Among Objects | Yes, all objects share the same copy. | No, but they belong to the class, not to individual objects. |
| Memory Allocation | Allocated once for the entire class, not per object. | Exists once per class, not per object. |
| Access | Can be accessed via the class or objects. | Can be accessed via the class or objects. |
| Access to Non-Static Members | Cannot access non-static members directly. | Cannot access non-static members directly. |
| Initialization | Must be defined outside the class. | Defined within the class itself. |
| Purpose | Used for values shared among all objects of the class. | Used for class-wide functions, not tied to individual objects. |
Open this section to load past papers