Proxy class is a kind of class that allows you to hide the private data members from clients of the class.
Proxy
Classes
Definition :
Proxy class is a kind of class that allows you to hide the private data members
from clients of the class.
In
this mechanism only public interface to the main class allows the client to use
the class's services without giving the implementation details to the client.
We
will create
1.
Myclass.h
2.
MyInterface.h
3.
MyInterface.cpp
4.
Test.cpp
We
will create Myclass.h in which the
class definition is stored.
Myclass.h
class Myclass
{
private:
int
value;
public:
Myclass(int
v):value(v)
{}
void setValue(int v)
{
value=v;
}
int getValue()
{
return
value;
}
};
Now
we will define proxy class called MyInterface
in which the pointer to Myclass object
is defined. Hence in the proxy class declaration in MyInterface.h the forward reference to the Myclass and the pointer
to the Myclass must be declared.
MyInterface.h
class Myclass; //forward
declaration of the class
class MyInterface
{
private:
public:
Myclass
*ptr; //pointer to object of the Myclass
MyInterface(int);
void
setValue(int);
int
getValue();
~MyInterface();
};
Note
that in the above class we have declared
getValue and setValue methods.
The definition of these methods in mentioned in the following .cpp file.
MyInterface.cpp
#include "d:\MyInterface.h"
#include "d:\Myclass.h"
MyInterface::MyInterface(int v):ptr(new Myclass(v))
{
}
void MyInterface::setValue(int v)
{
ptr‒>setValue(v);
}
int MyInterface::getValue()
return ptr‒>getValue();
}
MyInterface::~MyInterface()
{
delete
ptr;
}
Now
it's the time to write the test program for using the services of the Myclass.
Test.cpp
#include<iostream.h>
#include "D:\MyInterface.h"
void main()
{
MyInterface
obj(10);
cout<<"\n
Interface contains (Before): "<<<obj.getValue();
obj.setValue(20);
cout<<"\n
Interface contains(After): "<<obj.getValue();
}
Now
compile the above Test.cpp program
to get the output.
Output
Interface
contains(Before): 10
Interface
contains(After): 20
Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading : Tag: Data Structure, C++, C Programing : Data Structures using C ++ Program - Proxy Classes
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Electron Devices
EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Circuits and Network Analysis
EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Engineering Drawing - Laboratory
ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus - Laboratory
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Devices and Circuits Laboratory
EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation