Answer to Question #262582 in C++ for Empire

Question #262582

How do you implement abstraction and encapsulated in c++


1
Expert's answer
2021-11-07T17:18:34-0500

Abstraction 

The abstraction principle only shows people the information they need. It hides the implementation intricacies of programs, reducing the program's complexity.

Example of Abstraction:

#include <iostream>
using namespace std;
 
class Abstraction {
private:
   
    int a, b, c; //The private members
public:
    void mult(int x, int y)
    {
        a = x;
        b = y;
        c = a  * b;
        cout<<"Multiplication of the two number is : "<<c<<endl;
    }
};
int main()
{
    Abstraction s;
    s.mult(5, 4);
    return 0;
}

Encapsulation: 

Encapsulation is a technique for hiding data in a single entity or unit while also protecting it from the outside world.

Example of Abstraction:



#include <iostream>
using namespace std;
 
class Encapsulation {
private:
   
    int x; // Declared as private: 
 
public:
    // Initializing the value of x
    void setX(int a)
    {
        x = a;
    }
 
    //Getting the value of x
    int getX()
    {
        return x;
    }
};


int main()
{
    Encapsulation en1;
 
    en1.setX(10);
 
    cout<<en1.getX();
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS