💻 Lab 08: Static Members and the this Pointer in C++
🎯 Objective:
To understand the use of static data members, static member functions, and the this
pointer in C++ classes.
🧠 Introduction:
1. Static Data Members
A static data member is shared among all objects of a class.
It has only one copy in memory, regardless of how many objects are created.
Declared inside the class using the keyword static.
Must be defined outside the class (once only).
All objects share the same value.
Example:
#include <iostream>
using namespace std;
class MyC {
private:
int z;
public:
static int x; // declared static
void set(int a, int b) { x = a; z = b; }
void get() {
cout << "static member x = " << x << endl;
cout << "instance variable z = " << z << endl;
}
void increment() { x += 2; }
};
int MyC::x = 0; // initialize static variable
int main() {
MyC obja, objb, objc;
[Link](1, 2);
[Link](2, 3);
[Link](4, 5);
[Link]();
[Link]();
1
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
2. Static Member Functions
Can be called without creating an object.
Can access only static variables and other static functions.
Cannot use the this pointer, since no specific object is referenced.
Example:
#include <iostream>
using namespace std;
class A {
public:
static void display() {
int x = 8;
cout << "Static function output: " << x << endl;
}
};
int main() {
A::display(); // called using class name
}
3. The this Pointer
Every object in C++ has an implicit pointer called this,
which points to the current instance of the class.
It can be used explicitly or implicitly to access class members.
Example:
#include <iostream>
using namespace std;
class A {
public:
int x, y;
void set(int a, int b) {
this->x = a;
this->y = b;
}
void show() {
cout << "Sum = " << this->x + this->y << endl;
2
}
};
int main() {
A obj;
[Link](3, 4);
[Link]();
}
✅ Solved Example
#include <iostream>
using namespace std;
class MyClass {
private:
int a = 5, b = 6, c = 9;
public:
void fun(int b) {
int a = 2;
cout << this->b + b + a + c << endl;
}
};
int main() {
MyClass obj;
[Link](8);
}
Expected Output:
25
🧩 Lab Questions
Q1. Find the Output (10 marks)
#include <iostream>
using namespace std;
class Test {
public:
static int count;
Test() { count++; }
~Test() { count--; cout << count << " "; }
};
int Test::count = 0;
int main() {
Test a, b, c;
}
3
Q2. Simple Coding (15 marks)
Write a C++ program that:
Defines a class Counter with a static variable count.
Each time an object is created, count increases by 1.
Print the total number of objects created.
Expected Output:
Number of objects created: 3
Q3. Hard Coding (25 marks)
Create a class BankAccount with:
Private members: accountNumber, balance
A static variable interestRate (shared by all accounts)
A static function setRate() to update the interest rate
Member functions deposit(), withdraw(), and display()
Use the this pointer inside class methods where appropriate
Example Output:
Enter interest rate: 5
Account 1 Balance: 500
Account 2 Balance: 1000
Updated Interest Rate: 5%