Question

In: Computer Science

Write a C++ program to explain the following concepts 1.Data hiding 2.Information hiding 3.Proxy class (solve...

Write a C++ program to explain the following concepts

1.Data hiding

2.Information hiding

3.Proxy class

(solve fast)

Solutions

Expert Solution

1.Data Hiding:

Data hiding is a process of combining data and functions into a single unit. The ideology behind data hiding is to conceal data within a class, to prevent its direct access from outside the class. It helps programmers to create classes with unique data sets and functions, avoiding unnecessary penetration from other program classes.

Following is prog.to understand data hiding in c++

#include<iostream>
using namespace std;
class Base{

int num; 
 public:

void read();
void print();

};

void Base :: read(){
cout<<"Enter any Integer value"<<endl; cin>>num;

}

void Base :: print(){
cout<<"The value is "<<num<<endl;
}

int main(){
Base obj;

obj.read();
obj.print();

return 0;
}

××××××××××××××××

Information Hiding:

OOPs principal encapsulation and abstraction are used for information hiding, we also called it as data hiding too.Im providing another code for information hiding as follows

#include <iostream>
using namespace std;

class Adder {
   public:
      // constructor
      Adder(int i = 0) {
         total = i;
      }
      
      // interface to outside world
      void addNum(int number) {
         total += number;
      }
      
      // interface to outside world
      int getTotal() {
         return total;
      };
   
   private:
      // hidden data from outside world
      int total;
};

int main() {
   Adder a;
   
   a.addNum(10);
   a.addNum(20);
   a.addNum(30);

   cout << "Total " << a.getTotal() <<endl;
   return 0;
}

Here above class adds numbers together, and returns the sum. The public members addNum and getTotal are the interfaces to the outside world and a user needs to know them to use the class. The private member total is something that is hidden from the outside world, but is needed for the class to operate properly............

××××××××××××××××××××××××

3.proxy class

A proxy is a class that provides a modified interface to another class......

suppose we have an array class that we only want to contain binary digits (1 or 0)....
  
We want operator[] to throw if we say something like a[1] = 42, but that isn't possible because that operator only sees the index of the array, not the value being stored.

Now We can solve this using a proxy:

#include <iostream>
using namespace std;

struct aproxy {
aproxy(int& r) : mPtr(&r) {}
void operator = (int n) {
if (n > 1 || n < 0) {
throw "not binary digit";
}
*mPtr = n;
}
int * mPtr;
};

struct array {
int mArray[10];
aproxy operator[](int i) {
return aproxy(mArray[i]);
}
};

int main() {
try {
array a;
a[0] = 1; // ok
a[0] = 42; // throws exception
}
catch (const char * e) {
cout << e << endl;
}
}
The proxy class now does our checking for a binary digit and we make the array's operator[] return an instance of the proxy which has limited access to the array's internals....

Hope this help you..all the best


Related Solutions

(Use the string class to solve the problem) Write a program (in c++) that can be...
(Use the string class to solve the problem) Write a program (in c++) that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with gender-neutral pronouns. For example, it will replace “he” with “she or he”, and “him” with “her or him”. Be sure to preserve...
Write a C++ program where class 1 and class 2 (which is a base class) should...
Write a C++ program where class 1 and class 2 (which is a base class) should have a derived class (class 4 and class 5). Each of the derived classes should include at least 1 variable, 2 functions (one will be showing the function overriding case, the second one will be showing the function overloading case), 2 constructors (with default values, with parameters). Then class 3 (composition) (to relate class 1 and class 2) should include 3 variables (first variable...
Write a program that does the following in C++ 1 ) Write the following store data...
Write a program that does the following in C++ 1 ) Write the following store data to a file (should be in main) DC Tourism Expenses 100.20 Revenue 200.50 Maryland Tourism Expenses 150.33 Revenue 210.33 Virginia Tourism Expenses 140.00 Revenue 230.00 2 ) Print the following heading: (should be in heading function) Store name | Profit [Note: use setw to make sure all your columns line up properly] 3 ) Read the store data for one store (should be in...
-Write a program in C++: • to find the sum of the series 1! /1+2! /2+3!...
-Write a program in C++: • to find the sum of the series 1! /1+2! /2+3! /3+4! /4+5! /5 using the function1, • to convert decimal number to binary number using the function2, • to check whether a number is a prime number or not using the function3, • to check whether two given strings are an anagram using the function4. important must do in (Multi-Filing) of c++
********************C# C# C#******************** Part A: Create a project with a Program class and write the following...
********************C# C# C#******************** Part A: Create a project with a Program class and write the following two methods(headers provided) as described below: 1. A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number if the number is not in the range...
WRITE IN C++ Create a class named Coord in C++ Class has 3 private data items...
WRITE IN C++ Create a class named Coord in C++ Class has 3 private data items               int xCoord;               int yCoord;               int zCoord; write the setters and getters. They should be inline functions               void setXCoord(int)             void setYCoord(int)            void setZCoord(int)               int getXCoord()                     int getYCoord()                   int getZCoord() write a member function named void display() that displays the data items in the following format      blank line      xCoord is                          ????????      yCoord is                          ????????      zCoord...
Using C Language Write a program segment that computes 1 + 2 + 3 + ......
Using C Language Write a program segment that computes 1 + 2 + 3 + ... + ( n - 1) + n , where n is a data value. Follow the loop body with an if statement that compares this value to (n * (n + 1)) / 2 and displays a message that indicates whether the values are the same or different. Please give me code to just copy and paste
Program Requirements: Write a C++ program according to the following requirements: 1.   Open the data file...
Program Requirements: Write a C++ program according to the following requirements: 1.   Open the data file Electricity.txt and read each column into an array (8 arrays total). 2.   Also create 2 arrays for the following: Total Fossil Fuel Energy (sum of all fossil fuels) Total Renewable Energy (sum of all renewable sources) Electricity.txt: Net generation United States all sectors monthly https://www.eia.gov/electricity/data/browser/ Source: U.S. Energy Information Administration All values in thousands of megawatthours Year   all fuels   coal       natural gas   nuclear  ...
Write C++ program Consider the following SimpleString class: class simpleString {     public:          simpleString( );...
Write C++ program Consider the following SimpleString class: class simpleString {     public:          simpleString( );                                 // Default constructor          simpleString(int mVal );                    // Explicit value constructor          ~simpleString() { delete [ ] s;}          // Destructor void readString();                              // Read a simple string          void writeString() const;                    // Display a simple string char at(int pos) const;                       // Return character at (pos)          int getLength() const;                        // Return the string length          int getCapacity() const;...
Draw a Flow chart and write a C++ program to solve the quadratic equation ax^2 +...
Draw a Flow chart and write a C++ program to solve the quadratic equation ax^2 + bx + c = 0 where coefficient a is not equal to 0. The equation has two real roots if its discriminator d = b2 – 4ac is greater or equal to zero. The program gets the three coefficients a, b, and c, computes and displays the two real roots (if any). It first gets and tests a value for the coefficient a. if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT