Professional Documents
Culture Documents
ref=lbp
Data Structures Algorithms Interview Preparation Topic-wise Practice C++ Java Python
Abstraction in C++
Difficulty Level : Easy ● Last Updated : 30 May, 2018
Consider a real life example of a man driving a car. The man only knows
that pressing the accelerators will increase the speed of car or applying
brakes will stop the car but he does not know about how on pressing
accelerator the speed is actually increasing, he does not know about the
inner mechanism of the car or the implementation of accelerator, brakes
etc in the car. This is what abstraction is.
1 of 7 12-02-2022, 07:11 pm
Abstraction in C++ - GeeksforGeeks https://www.geeksforgeeks.org/abstraction-in-c/?ref=lbp
simply call the function pow() present in the math.h header file and
pass the numbers as arguments without knowing the underlying
algorithm according to which the function is actually calculating power
of numbers.
Example:
#include <iostream>
using namespace std;
class implementAbstraction
{
private:
int a, b;
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
public: that you have read and understood our Cookie Policy & Privacy Policy
2 of 7 12-02-2022, 07:11 pm
Abstraction in C++ - GeeksforGeeks https://www.geeksforgeeks.org/abstraction-in-c/?ref=lbp
// private members
void set(int x, int y)
{
a = x;
b = y;
}
void display()
{
cout<<"a = " <<a << endl;
cout<<"b = " << b << endl;
}
};
int main()
{
implementAbstraction obj;
obj.set(10, 20);
obj.display();
return 0;
}
Output:
a = 10
b = 20
You can see in the above program we are not allowed to access the
variables a and b directly, however one can call the function set() to set
the values in a and b and the function display() to display the values of a
and b.
3 of 7 12-02-2022, 07:11 pm
Abstraction in C++ - GeeksforGeeks https://www.geeksforgeeks.org/abstraction-in-c/?ref=lbp
Want to learn from the best curated videos and practice problems,
check out the C++ Foundation Course for Basic to Advanced C++ and
C++ STL Course for foundation plus STL. To complete your preparation
from learning a language to DS Algo and many more, please refer
Complete Interview Preparation Course.
Like 197
Previous Next
R ECO M M E N D E D A RT I C L E S Page : 1 2
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
01 05
that you have read and understood our Cookie Policy & Privacy Policy
Difference between Abstraction C++ Program to Find the Mth
and Encapsulation in C++ Got It ! element of the Array after K left
4 of 7 12-02-2022, 07:11 pm
Abstraction in C++ - GeeksforGeeks https://www.geeksforgeeks.org/abstraction-in-c/?ref=lbp
Article Contributed By :
GeeksforGeeks
We useArticle Tags
cookies to : you have
ensure cpp-class, C++ experience on our website. By using our site, you acknowledge
the best browsing
that you have read and understood our Cookie Policy & Privacy Policy
Practice Tags : CPP
Got It !
5 of 7 12-02-2022, 07:11 pm
Abstraction in C++ - GeeksforGeeks https://www.geeksforgeeks.org/abstraction-in-c/?ref=lbp
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Load Comments
feedback@geeksforgeeks.org
Company Learn
About Us Algorithms
Contact Us CS Subjects
CSS Internships
JavaScript Videos
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
Bootstrap
that you have read and understood our Cookie Policy & Privacy Policy
Got It !
6 of 7 12-02-2022, 07:11 pm
Abstraction in C++ - GeeksforGeeks https://www.geeksforgeeks.org/abstraction-in-c/?ref=lbp
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy
Got It !
7 of 7 12-02-2022, 07:11 pm