{"id":80887,"date":"2021-06-08T09:00:50","date_gmt":"2021-06-08T03:30:50","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=80887"},"modified":"2021-06-08T09:00:50","modified_gmt":"2021-06-08T03:30:50","slug":"abstraction-in-cpp","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/abstraction-in-cpp\/","title":{"rendered":"Abstraction in C++ with Examples"},"content":{"rendered":"<p>Abstraction is one of the salient concepts of Object-Oriented Programming. This topic is not a complex one, but still, many are not well versed with it. We at TechVidvan will help you to understand abstraction from the very basics.<\/p>\n<h3>What is Abstraction in C++?<\/h3>\n<p>Abstraction refers to the act of representing the crucial requisite features without including the additional explanations of such features. Hence, it means providing the end-user with their needs but without the details of how the needs were fulfilled.<\/p>\n<p>For instance, we know how the switches at our houses work. Say for the fans, we just simply turn on the switch if we want to switch it on, or turn off the switch if we want to switch it off. In the case of fans, we can even use the regulator to adjust the speed according to our needs. Thus, we know all the features but we don\u2019t know what the background implementation is. This is a real-life example of data abstraction.<\/p>\n<h3>Types of Abstraction in C++<\/h3>\n<p>There are 2 types of abstraction in C++:<\/p>\n<p><strong>1. Data Abstraction:<\/strong> It hides the information about the data.<br \/>\n<strong>2. Control Abstraction:<\/strong> It hides the information about the implementation.<\/p>\n<h3>Implementation of Abstraction in C++<\/h3>\n<p>The concept of data abstraction can be implemented in two different ways:<\/p>\n<h4>1. Using classes<\/h4>\n<p>The simplest way to achieve or implement abstraction is to use classes. A class can be used to group every data member and function into a solo unit with help of access specifiers. A class can determine which data of the class could be accessible to the outside world. In this way with the use of access specifiers, classes help to implement abstraction.<\/p>\n<h4>2. Through header files<\/h4>\n<p>Another type of abstraction in C++ is with header files. For example, if we put the header file for mathematical operations i.e. &lt;cmath&gt; or &lt;math.h&gt;, at the start of the program we can use the sqrt() function to find the square root of any given number. In this way, we know what sqrt() does, but we do not know the background details of how it finds the square root.<\/p>\n<p>Let us take a simple program to find the square root of a number given by the user.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\n#include &lt;cmath&gt;\nusing namespace std;\n\nint main()\n{\n  double num, answer;\n  cout&lt;&lt;\"Enter a number: \";\n  cin&gt;&gt;num;\n  answer = sqrt(num);\n  cout &lt;&lt; \"\\n Square root of \" &lt;&lt; num &lt;&lt; \" is \" &lt;&lt; answer ;\n}\n\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\"><span style=\"font-weight: 400\">Enter a number: <\/span><span style=\"font-weight: 400\">49<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\"> Square root of <\/span><span style=\"font-weight: 400\">49<\/span><span style=\"font-weight: 400\"> is <\/span><span style=\"font-weight: 400\">7<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\">&#8230;Program finished with <\/span><span style=\"font-weight: 400\">exit<\/span><span style=\"font-weight: 400\"> code <\/span><span style=\"font-weight: 400\">0<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\">Press ENTER to <\/span><span style=\"font-weight: 400\">exit<\/span><span style=\"font-weight: 400\"> console.<\/span><\/div>\n<p>So, we can see that we can simply use the sqrt() function from the &lt;cmath&gt; library to find the square root, but we don\u2019t even know the background details.<\/p>\n<h3>Access specifiers in C++<\/h3>\n<p>Access specifiers are the main feature to hold the implementation data abstraction intact in C++. We can use them to determine which class members can be visible to the outside world and which can not be. Such restrictions can be put with the help of public, private, and protected keywords.<\/p>\n<p>1. Data members prefixed with the public keyword are accessible throughout the program.<\/p>\n<p>2. Data members prefixed with the private keyword are accessible only within the class where they are declared. Hence, they are restricted to that class and can not be accessed by the members outside the class.<\/p>\n<p>3. Data members prefixed with the protected keyword are accessible to the members of the class. It is a special kind of access specifier and is similar to private access specifiers.<\/p>\n<p>Let us consider a simple C++ program to find the sum of two integer values.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\nclass dataAbstraction {\npublic:\n    int a, b;\n\npublic:\n    void data()\n    {\n        cout &lt;&lt; \"Welcome to TechVidvan! \\n\";\n        cout &lt;&lt; \"Enter Two values : \"; \n        cin &gt;&gt; a &gt;&gt; b;\n    }\npublic: \n    void display()\n    {\n        cout &lt;&lt; \"The sum is \"&lt;&lt; a+b;\n        cout &lt;&lt; \"\\n\";\n    }\n};\nint main()\n{\n    dataAbstraction dA;\n    dA.data();\n    dA.display();\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\"><span style=\"font-weight: 400\">Welcome to TechVidvan! <\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\">Enter Two values : <\/span><span style=\"font-weight: 400\">4<\/span> <span style=\"font-weight: 400\">9<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\">The sum is <\/span><span style=\"font-weight: 400\">13<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\">&#8230;Program finished with <\/span><span style=\"font-weight: 400\">exit<\/span><span style=\"font-weight: 400\"> code <\/span><span style=\"font-weight: 400\">0<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\">Press ENTER to <\/span><span style=\"font-weight: 400\">exit<\/span><span style=\"font-weight: 400\"> console.<\/span><\/div>\n<p>We can see that everything gets displayed properly. It is because of the fact that every data member was declared public. Since they were all declared public, they had access to each other.<\/p>\n<p>Now let us see what happens if we declare the display function private.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\nclass dataAbstraction {\npublic:\n    int a, b;\n\npublic:\n    void data()\n    {\n        cout &lt;&lt; \"Welcome to TechVidvan! \\n\";\n        cout &lt;&lt; \"Enter Two values : \"; \n        cin &gt;&gt; a &gt;&gt; b;\n    }\nprivate: \n    void display()\n    {\n        cout &lt;&lt; \"The sum is \"&lt;&lt; a+b;\n        cout &lt;&lt; \"\\n\";\n    }\n};\nint main()\n{\n    dataAbstraction dA;\n    dA.data();\n    dA.display();\n}<\/pre>\n<p>If we run the above program code, the private data member will now be invisible to the other classes and hence it will throw an error on our screen.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">\n<p><span style=\"font-weight: 400\">Compilation failed due to following error(s).<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400\">main.cpp:26:16: error: &#8216;void dataAbstraction::display()&#8217; is private within this<\/span><span style=\"font-weight: 400\"> context<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\"> \u00a0 \u00a0 dA.display();<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ^<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\">main.cpp:<\/span><span style=\"font-weight: 400\">16<\/span><span style=\"font-weight: 400\">:<\/span><span style=\"font-weight: 400\">10<\/span><span style=\"font-weight: 400\">: note: declared <\/span><span style=\"font-weight: 400\">private<\/span><span style=\"font-weight: 400\"> here<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\"> \u00a0 \u00a0 <\/span><span style=\"font-weight: 400\">void<\/span> <span style=\"font-weight: 400\">display<\/span><span style=\"font-weight: 400\">()<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ^~~~~~~<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3>Benefits of Abstraction in C++<\/h3>\n<p>1. Abstraction increases the reusability of the code because of the proper partitioning.<\/p>\n<p>2. It reduces the complexity as well as the redundancy of the code, hence increasing the readability.<\/p>\n<p>3. Using classes and objects increases the security of the code. We can declare the parts of the code as private to keep them secure.<\/p>\n<p>4. Due to abstraction, the important parts of the code are secure as only the essential features are provided to the user and they don\u2019t know the background details.<\/p>\n<p>5. Since the programmer can use the same code repeatedly, it helps us perform similar tasks for similar operations.<\/p>\n<p>6. Abstraction allows changing internal implementations without affecting its user-level code.<\/p>\n<p>7. Only the programmer can make changes to his data and methods, no one else.<\/p>\n<p>8. The programmer need not write low-level code.<\/p>\n<h3>Design Strategy<\/h3>\n<p>Data Abstraction divided the code into two parts, interface and implementation. Hence, while writing the code we should maintain implementation as it is. That is, we should keep the interface independent of implementation. It even plays an important role in avoiding code duplication.<\/p>\n<p>Hence, we can use the code again and again as it is and change the implementation, which even helps protect the data from the outside world.<\/p>\n<h3>Summary<\/h3>\n<p>In this tutorial, we learned about what data abstraction is and how we can implement it in two different ways. We even got to learn about the access specifiers used for data abstraction.<\/p>\n<p>Besides the real-life example, we even went through some coding aspects of abstraction. Hopefully, this tutorial cleared the concept of data abstraction for you!<\/p>\n<p>Keep learning at TechVidvan!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Abstraction is one of the salient concepts of Object-Oriented Programming. This topic is not a complex one, but still, many are not well versed with it. We at TechVidvan will help you to understand&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":80904,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3405],"tags":[3500,3501,3502],"class_list":["post-80887","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cpp","tag-abstraction-in-c","tag-benefits-of-abstraction-in-c","tag-data-abstraction-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Abstraction in C++ with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"Abstraction is one of the feature of Object Oriented Programming, where you show only relevant details to the user. Learn more about it in C++\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/abstraction-in-cpp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Abstraction in C++ with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Abstraction is one of the feature of Object Oriented Programming, where you show only relevant details to the user. Learn more about it in C++\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/abstraction-in-cpp\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-06-08T03:30:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Abstraction-in-C.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Abstraction in C++ with Examples - TechVidvan","description":"Abstraction is one of the feature of Object Oriented Programming, where you show only relevant details to the user. Learn more about it in C++","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/abstraction-in-cpp\/","og_locale":"en_US","og_type":"article","og_title":"Abstraction in C++ with Examples - TechVidvan","og_description":"Abstraction is one of the feature of Object Oriented Programming, where you show only relevant details to the user. Learn more about it in C++","og_url":"https:\/\/techvidvan.com\/tutorials\/abstraction-in-cpp\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-06-08T03:30:50+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Abstraction-in-C.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/abstraction-in-cpp\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/abstraction-in-cpp\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Abstraction in C++ with Examples","datePublished":"2021-06-08T03:30:50+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/abstraction-in-cpp\/"},"wordCount":967,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/abstraction-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Abstraction-in-C.jpg","keywords":["Abstraction in C++","Benefits of Abstraction in C++","Data abstraction in C++"],"articleSection":["C++ Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/abstraction-in-cpp\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/abstraction-in-cpp\/","url":"https:\/\/techvidvan.com\/tutorials\/abstraction-in-cpp\/","name":"Abstraction in C++ with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/abstraction-in-cpp\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/abstraction-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Abstraction-in-C.jpg","datePublished":"2021-06-08T03:30:50+00:00","description":"Abstraction is one of the feature of Object Oriented Programming, where you show only relevant details to the user. Learn more about it in C++","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/abstraction-in-cpp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/abstraction-in-cpp\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/abstraction-in-cpp\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Abstraction-in-C.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Abstraction-in-C.jpg","width":1200,"height":628,"caption":"Abstraction in C++"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/abstraction-in-cpp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Abstraction in C++ with Examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/80887","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=80887"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/80887\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/80904"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=80887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=80887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=80887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}