Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading

Proxy Classes

Data Structures using C ++ Program

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.

Creation of Proxy Classes

We will create

1. Myclass.h

2. MyInterface.h

3. MyInterface.cpp

4. Test.cpp

 

Step 1 :

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;

}

};

 

Step 2 :

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.

 

Step 3 :

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;

}

 

Step 4 :

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();

}

 

Step 5 :

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 C PlusPlus: Chapter 1: Data Abstraction and Overloading



Under Subject


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 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