{"id":83714,"date":"2021-09-02T09:00:24","date_gmt":"2021-09-02T03:30:24","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=83714"},"modified":"2021-09-02T09:00:24","modified_gmt":"2021-09-02T03:30:24","slug":"cpp-interface","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/cpp-interface\/","title":{"rendered":"C++ Interface with Examples"},"content":{"rendered":"<p>The C++ programming language offers various features and functionalities to the programmers. It also supports object-oriented programming which is a must when you are developing something. If you have learned C programming, then it will be easy for you to learn C++ programming. Because in C++, some concepts are the same as C programming. In this tutorial, we are going to discuss a concept of C++ named Interface.<\/p>\n<h3>What are Interfaces in C++<\/h3>\n<p>In C++, there is a way to describe the behaviour of a class without committing to a particular implementation of that class. This feature is offered by C++ objects and classes. Using abstract classes, you can implement the C++ interfaces.<\/p>\n<p>Let me tell you that data abstraction and abstract classes are not the same. Data abstraction is all about keeping important details separate from associated data. You can say that interfaces and abstract classes convey the same idea.<\/p>\n<h3>Pure Virtual Functions<\/h3>\n<p>Abstract class is nothing but a class with a pure virtual function.<\/p>\n<h4>Virtual Function<\/h4>\n<p>In C++, it is a member of a function in a class that we declare in the base class and we also redefine it in a derived class.<\/p>\n<h4>Pure Virtual Function<\/h4>\n<p>You can declare a pure virtual function but you cannot implement it. It is a virtual function that exists but cannot be implemented. You can declare a pure virtual function by placing <strong>\u20180\u2019<\/strong> in its declaration.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">virtual float fun() = 0;<\/pre>\n<h3>C++ Abstract Class<\/h3>\n<p>In C++, you can make a class abstract by declaring one of its functions as a pure virtual function. As we said, you can declare a pure virtual function by placing <strong>\u20180\u2019<\/strong> in its declaration.<\/p>\n<p>Below is an example of an abstract class that has one abstract method draw(). The implementation is done by the derived classes such as Rectangle and Circle.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;  \nusing namespace std;  \nclass Shapes   \n{    \npublic:   \nvirtual void drawShape()=0; \/\/pure virtual function!    \n};    \nclass Rectangle:Shapes   \n{    \npublic:  \nvoid drawShape()    \n{    \ncout&lt;&lt;\"Drawing a rectangle!\"&lt;&lt;endl;    \n}    \n};    \nclass Circle:Shapes    \n{    \npublic:  \nvoid drawShape()    \n{    \ncout&lt;&lt;\"Drawing a circle!\"&lt;&lt;endl;    \n}    \n};    \nint main() {  \nRectangle r;  \nCircle c;\ncout&lt;&lt;\"TechVidvan Tutorial: C++ Interfaces!\"&lt;&lt;endl;\nr.drawShape();    \nc.drawShape();   \nreturn 0;  \n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">TechVidvan Tutorial: C++ Interfaces!<br \/>\nDrawing a rectangle!<br \/>\nDrawing a circle!<\/div>\n<h3>Importance of C++ Interface<\/h3>\n<p>Suppose, you created a class named OS with data members Windows, Linux and Mac with member functions such as size(), type() and feature().<\/p>\n<p>Let\u2019s say that the size of each operating system is fixed and cannot be altered. But different operating systems have different sizes. In one way, you can implement the class OS for the function size() by making this function abstract. In this way, we can make sure that the size of all operating systems is fixed.<br \/>\nBelow is the code to solve the above problem.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\nclass OS\n{\npublic:\nvirtual void size() = 0;\nvoid type()\n{\ncout&lt;&lt;\"It is a windows operating system!\"&lt;&lt;endl;\n}\n};\nclass Windows: public OS\n{\npublic:\nvoid size()\n{\ncout&lt;&lt;\"The size is 4.90gb!\"&lt;&lt;endl;\n}\n};\n\nint main()\n{\ncout&lt;&lt;\"TechVidvan Tutorial: C++ Interfaces!\"&lt;&lt;endl&lt;&lt;endl;\nWindows data;\ndata.size();\ndata.type();\nreturn 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">\n<p>C++ Interfaces!<\/p>\n<p>The size is 4.90gb!<br \/>\nIt is a windows operating system!<\/p>\n<\/div>\n<h3>Follow the rules before using C++ Interfaces<\/h3>\n<p>You should follow the rules before working with the interfaces in the C++ programming language.<\/p>\n<p>1. You can only declare a pure virtual function but you cannot define it.<\/p>\n<p>2. You can only assign 0 to the pure virtual function.<\/p>\n<p>3. Also you cannot create an instance of a class.<\/p>\n<p>4. You can create a pointer to the instance of the derived class with a reference of base abstract class.<\/p>\n<p><strong>For Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">OS *obj = new Windows();\nobj-&gt;size();<\/pre>\n<h3>Summary<\/h3>\n<p>In this tutorial, we discussed the interface in detail in the C++ programming language. We discussed what interfaces and abstract classes are in C++ and their importance. We also discussed pure virtual functions in C++.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C++ programming language offers various features and functionalities to the programmers. It also supports object-oriented programming which is a must when you are developing something. If you have learned C programming, then it&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":84482,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3405],"tags":[4129,4130],"class_list":["post-83714","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cpp","tag-c-interface","tag-c-interfaces"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C++ Interface with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn what are interfaces in C++ &amp; their importance with syntax &amp; examples.Learn about pure virtual functions &amp; rules for using C++ interface\" \/>\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\/cpp-interface\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Interface with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn what are interfaces in C++ &amp; their importance with syntax &amp; examples.Learn about pure virtual functions &amp; rules for using C++ interface\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/cpp-interface\/\" \/>\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-09-02T03:30:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/C-Interfaces.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=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ Interface with Examples - TechVidvan","description":"Learn what are interfaces in C++ & their importance with syntax & examples.Learn about pure virtual functions & rules for using C++ interface","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\/cpp-interface\/","og_locale":"en_US","og_type":"article","og_title":"C++ Interface with Examples - TechVidvan","og_description":"Learn what are interfaces in C++ & their importance with syntax & examples.Learn about pure virtual functions & rules for using C++ interface","og_url":"https:\/\/techvidvan.com\/tutorials\/cpp-interface\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-09-02T03:30:24+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/C-Interfaces.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":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-interface\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-interface\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"C++ Interface with Examples","datePublished":"2021-09-02T03:30:24+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-interface\/"},"wordCount":532,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-interface\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/C-Interfaces.jpg","keywords":["C++ Interface","C++ Interfaces"],"articleSection":["C++ Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/cpp-interface\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-interface\/","url":"https:\/\/techvidvan.com\/tutorials\/cpp-interface\/","name":"C++ Interface with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-interface\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-interface\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/C-Interfaces.jpg","datePublished":"2021-09-02T03:30:24+00:00","description":"Learn what are interfaces in C++ & their importance with syntax & examples.Learn about pure virtual functions & rules for using C++ interface","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/cpp-interface\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/cpp-interface\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-interface\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/C-Interfaces.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/C-Interfaces.jpg","width":1200,"height":628,"caption":"C++ Interfaces"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/cpp-interface\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"C++ Interface 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\/83714","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=83714"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/83714\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/84482"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=83714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=83714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=83714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}