In C++, the static keyword can be used in various contexts within a class to define static members, static member functions, and static local variables.
class MyClass { public: static int staticDataMember; }; int MyClass::staticDataMember = 0; // Definition of static data member
Lets develop a C++ program that effectively use static variables within a class to maintain a count across multiple object instance and discuss the significance of static variables in object-oriented programming and compare their behavior with instance variables.
#include<iostream> using namespace std; class sample { public: static int count; void display(); }; int sample::count; void sample::display() { ++count; cout << "The value of count : " << count << endl; } int main() { sample c1, c2, c3; c1.display(); c2.display(); c3.display(); }
The value of count : 1 The value of count : 2 The value of count : 3
The count variable is declared as static within the class sample. The static variable count is initialized outside the class, with no initial value specified, so it defaults to 0. The display() function increments the static variable count and displays its value. The main() function creates three objects of class sample and invokes the display() function on each object.
class MyClass { public: static void staticMemberFunction() { // Function body } };
Lets implement a C++ program demonstrating the usage of static methods to increment a static variable without instantiating any objects. How does this approach differ from traditional object-oriented programming?
#include<iostream> using namespace std; class Account { static int count; // static variable declaration public: static void display(); // static member function declaration }; int Account::count = 56; // static data definition void Account::display() { count++; cout << "The value of count is: " << count << endl; } int main() { Account::display(); Account::display(); }
The value of count is: 57 The value of count is: 58
The display() function is declared as a static member function within the Account class.The count variable is declared as static within the class. The static variable count is initialized outside the class. In the main() function, the display() function is called using the scope resolution operator :: without creating objects of the Account class.
class MyClass { public: void memberFunction() { static int staticLocalVariable = 0; // Static local variable staticLocalVariable++; } };
#include <iostream> using namespace std; class MyClass { public: void memberFunction() { static int staticLocalVariable = 0; // Static local variable staticLocalVariable++; cout << "Static local variable value: " << staticLocalVariable << endl; } }; int main() { MyClass obj1, obj2; // Calling memberFunction on obj1 obj1.memberFunction(); // Output will be 1 obj1.memberFunction(); // Output will be 2 // Calling memberFunction on obj2 obj2.memberFunction(); // Output will be 3 return 0; }
Static local variable value: 1 Static local variable value: 2 Static local variable value: 3
This program creates an instance of MyClass named obj1 and obj2 and calls the memberFunction() on each object. The static local variable staticLocalVariable retains its value between function calls for each object.