Maham

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Assignment: #2

Submitted by:--
Maham nawaz
Submitted To:--
SIR USAMA
Department:--
Cs--iii
Reg.No:--
As22atk0092
Static Class member:-
A static class member is a member (variable or function) that belongs to the class itself rather
than to any specific instance (object) of the class. This means that the value of a static member is
shared among all instances of the class and can be accessed without creating an object of the
class.
To declare a static class member in C++, you use the static keyword in the class definition.
Here's an example:
Example:-
#include <iostream>

class MyClass {
public:
// Regular member variable
int instanceVar;

// Static member variable


static int staticVar;

// Regular member function


void instanceFunction() {
std::cout << "Instance function\n";
}

// Static member function


static void staticFunction() {
std::cout << "Static function\n";
}
};

// Definition of the static member variable (must be done outside the class)
int MyClass::staticVar = 0;
int main() {
MyClass obj1, obj2;
obj1.instanceVar = 5;
obj1.instanceFunction();
MyClass::staticVar = 10;
MyClass::staticFunction();
obj2.staticVar = 15;
obj2.staticFunction(); // It works, but it's confusing and discouraged
return 0;
}

Proxy Classes:

Proxy classes, also known as wrapper classes, are a design pattern in object-oriented
programming where one class acts as an intermediary or substitute for another class. The proxy
class typically has the same interface as the original class, allowing it to be used in place of the
original class seamlessly. The purpose of using proxy classes is to add an extra layer of control
or functionality to the underlying class without modifying its core behavior.

#include <iostream>

// Original class
class Subject {
public:
void request() {
std::cout << "Subject::request()\n";
}
};

// Proxy class
class Proxy {
private:
Subject subject; // The underlying object
public:
void request() {
// Additional functionality can be added here
std::cout << "Proxy::request()\n";
subject.request();
}
};
int main() {
Proxy proxy;
proxy.request(); // This will call Proxy::request(), which in turn calls Subject::request()
return 0;
}

You might also like