Question

Data struc. and algo. with C++ class........ How is encapsulation used to facilitate programming? Give a...

Data struc. and algo. with C++ class........

  1. How is encapsulation used to facilitate programming? Give a code example and discuss what needs to be done without encapsulation in place (without encapsulation)?

Homework Answers

Answer #1

Solution:

In general terms, encapsulation is defined as wrapping up of information and data in a single unit.

For example, In an organization suppose three departments are there, IT, HR and sales, so here an employee within the department will have access to a certain amount of information based on the privilege which he/she got.

Suppose an employee from IT requires a piece of information about his/her details, then he/she will ask the HR about this (for example salary breakout for filing taxes and all).

code:

#include<iostream>
using namespace std;

class Enc
{
   private:
       //This data is set private so that it is hidden
       int data;
      
   public:
//The method is set public so anyone can access it
       void setData(int num)
       {
data =num;
       }
      
//This mehod is to return the value of data
       int getData()
       {
           return data;
       }
};

// main method or driver program
int main()
{
   Enc obj; //intantiating the object
  
   obj.setData(5);
  
   cout<<obj.getData();
   return 0;
}

Output:

5

Explanation:

Since the variable data's access modifier is private, this means that only methods within the class can access it, which makes getData(), and setData, binded with x which is nothing but encapsulation.

code without encapsulation:

#include<iostream>
using namespace std;

class Enc
{
   public:
       //This data is set private so that it is hidden
       int data;
      
   public:
//The method is set public so anyone can access it
       void setData(int num)
       {
data =num;
       }
      
//This mehod is to return the value of data
       int getData()
       {
           return data;
       }
};

// main method or driver program
int main()
{
   Enc obj; //intantiating the object
  
   obj.setData(5);
  
   cout<<obj.getData();
   return 0;
}


Hit the thumbs up if you liked the answer. :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
data struc and algo with C++ class.....................(please include resources) What are the advantages and disadvantages of...
data struc and algo with C++ class.....................(please include resources) What are the advantages and disadvantages of Polymorphism in programming?
In this module, we will be introduced to working with the C++ programming language. What are...
In this module, we will be introduced to working with the C++ programming language. What are some of the issues that you had installing and running the Eclipse IDE for C/C++ developers? Working with the C++ language, discuss the different data types available. What are some of the differences, if any, between data types in C++ versus Java? What tips can be utilized to identify possible vulnerabilities using C++ data types? Be sure to provide an appropriate source code example...
***********Java Programming********** I would like to use a data structure that can be resized at run...
***********Java Programming********** I would like to use a data structure that can be resized at run time and is efficient in memory and can be printed out in order. Give an example of any Java implementation of a data structure that fits this description and tell how it can be used.
I have a list of things for review in C programming, could you please give me...
I have a list of things for review in C programming, could you please give me an example and a brief explanation for each question... Thank you very much 5. Create functions that can return pointers or functions that can return structs 6. Analyze code that uses predefined strings such as  strlen, strcat, strncat, and  strtok 7. Create syntax that can perform file input and output 8. Distinguish between array and pointer notation and use both in syntax
Java programming. 1) What, if anything, is wrong with the following Java code? void test(String x)...
Java programming. 1) What, if anything, is wrong with the following Java code? void test(String x) { switch (x) { case 1: break; case 2: break; case 0: break; default: break; case 4: break; } } Select one: a)Each case section must end with a break statement. b)The case label 0 must precede case label 1. c)There is nothing wrong with the Java code. d)The default label must be the last label in the switch statement. e)The variable x does...
A compiler generates the object code for individual modules, and a linkage editor is used to...
A compiler generates the object code for individual modules, and a linkage editor is used to combine multiple object modules into a single program binary. How does the linkage editor change the binding of instructions and data to memory addresses? What information needs to be passed from the compiler to the linkage editor to facilitate the memory binding tasks of the linkage editor?
C++ Programming   You are to develop a program to read Baseball player statistics from an input...
C++ Programming   You are to develop a program to read Baseball player statistics from an input file. Each player should bestored in a Player object. Therefore, you need to define the Player class. Each player will have a firstname, last name, a position (strings) and a batting average (floating point number). Your class needs to provide all the methods needed to read, write, initialize the object. Your data needs to be stored in an array of player objects. The maximum...
What is PACS and give an example of how it would be used?
What is PACS and give an example of how it would be used?
In C Programming: Thread safety is a concept often used when discussing subroutines or other code...
In C Programming: Thread safety is a concept often used when discussing subroutines or other code segments that are written in such way that they can be “multiply run” as parts of multiple threads that are being scheduled concurrently or otherwise multiprogrammed. Do you think that the function printf() is thread safe? Do some research and/or run some code experiments and provide a brief, but complete, answer that explains your view on the subject. Be sure to explain why you...
-Give an example of an experiment from your field of study where a pattern in data...
-Give an example of an experiment from your field of study where a pattern in data is used to construct a mathematical relationship. -Discuss how plotting data can help you identify the relationship between the current through the resistor and the potential difference across it.