{"id":5124,"date":"2025-03-26T09:00:00","date_gmt":"2025-03-26T04:00:00","guid":{"rendered":"https:\/\/afzalbadshah.com\/?p=5124"},"modified":"2024-12-31T08:58:41","modified_gmt":"2024-12-31T03:58:41","slug":"abstract-class-pure-abstract-class-and-interface-in-c-oop","status":"publish","type":"post","link":"https:\/\/afzalbadshah.com\/index.php\/2025\/03\/26\/abstract-class-pure-abstract-class-and-interface-in-c-oop\/","title":{"rendered":"Abstract Class, Pure Abstract Class, and Interface in C++ (OOP)"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><strong>Introduction to Abstract Classes<\/strong><\/h3>\n\n\n\n<p>An <strong>abstract class<\/strong> in C++ is a class designed to be specifically used as a base class. It cannot be instantiated on its own and typically includes at least one <strong>pure virtual function<\/strong>.<\/p>\n\n\n\n<p>A <strong>pure virtual function<\/strong> is a function declared within a class that has no implementation relative to the base class and must be implemented by all derived classes.<\/p>\n\n\n\n<p>Abstract classes are crucial in object-oriented programming to enforce a contract for derived classes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Key Definitions<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Abstract Class<\/strong>: A class with at least one pure virtual function. It serves as a blueprint for derived classes but cannot be instantiated directly.<\/li>\n\n\n\n<li><strong>Pure Abstract Class<\/strong>: An abstract class where all member functions are pure virtual.<\/li>\n\n\n\n<li><strong>Interface Class<\/strong>: A class that only contains pure virtual functions and no data members. It defines the &#8220;interface&#8221; that other classes must implement.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Code Example: Abstract Class with Pure Virtual Functions<\/strong><\/h3>\n\n\n\n<p>Here\u2019s the provided code that demonstrates the concept of abstract classes, including how they are used and enforced through inheritance:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Abstract Class and Derived Classes<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>#include &lt;iostream&gt;<br>#include &lt;string&gt;<br>using namespace std;<br><br>class shape { \/\/ Abstract base class<br>    public:<br>    virtual double calculatearea() = 0; \/\/ Pure virtual function<br>    virtual string getcolor() = 0;      \/\/ Pure virtual function<br>};<br><br>class circle : public shape { \/\/ Derived class<br>    private:<br>        string color;<br>        double radius;<br>    public:<br>        circle(string c, double r) : color(c), radius(r) {} \/\/ Constructor<br>        double calculatearea() override {<br>            return 3.14 * radius * radius; \/\/ Circle area formula<br>        }<br>        string getcolor() override {<br>            return color;<br>        }<br>};<br><br>class rectangle : public shape { \/\/ Derived class<br>    private:<br>        string color;<br>        double length, width;<br>    public:<br>        rectangle(string c, double l, double w) : color(c), length(l), width(w) {} \/\/ Constructor<br>        double calculatearea() override {<br>            return length * width; \/\/ Rectangle area formula<br>        }<br>        string getcolor() override {<br>            return color;<br>        }<br>};<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Explanation of the Classes<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Abstract Base Class (<code>shape<\/code>)<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Contains two pure virtual functions:\n<ul class=\"wp-block-list\">\n<li><code>calculatearea()<\/code>: Requires derived classes to implement area calculation logic.<\/li>\n\n\n\n<li><code>getcolor()<\/code>: Requires derived classes to implement color retrieval logic.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Acts as a blueprint for all shape types (e.g., <code>circle<\/code>, <code>rectangle<\/code>).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Derived Class (<code>circle<\/code>)<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Implements the <code>calculatearea()<\/code> function using the formula for a circle&#8217;s area (\u03c0r2\\pi r^2\u03c0r2).<\/li>\n\n\n\n<li>Implements the <code>getcolor()<\/code> function to return the color of the circle.<\/li>\n\n\n\n<li>Contains private members: <code>color<\/code> (a <code>string<\/code>) and <code>radius<\/code> (a <code>double<\/code>).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Derived Class (<code>rectangle<\/code>)<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Implements the <code>calculatearea()<\/code> function using the formula for a rectangle&#8217;s area (length\u00d7width\\text{length} \\times \\text{width}length\u00d7width).<\/li>\n\n\n\n<li>Implements the <code>getcolor()<\/code> function to return the color of the rectangle.<\/li>\n\n\n\n<li>Contains private members: <code>color<\/code> (a <code>string<\/code>), <code>length<\/code>, and <code>width<\/code> (both <code>double<\/code>).<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Main Function<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>int main() {<br>    circle c1(\"Black\", 26); \/\/ Create a circle object<br>    rectangle R1(\"Blue\", 23, 45); \/\/ Create a rectangle object<br><br>    cout &lt;&lt; \"The area of the circle = \" &lt;&lt; c1.calculatearea() &lt;&lt; endl;<br>    cout &lt;&lt; \"The color of the circle = \" &lt;&lt; c1.getcolor() &lt;&lt; endl;<br>    cout &lt;&lt; \"The area of the rectangle = \" &lt;&lt; R1.calculatearea() &lt;&lt; endl;<br>    cout &lt;&lt; \"The color of the rectangle = \" &lt;&lt; R1.getcolor() &lt;&lt; endl;<br><br>    return 0;<br>}<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Explanation of the <code>main<\/code> Function<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Creating Objects<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The program creates a <code>circle<\/code> object <code>c1<\/code> with color <code>\"Black\"<\/code> and radius <code>26<\/code>.<\/li>\n\n\n\n<li>It also creates a <code>rectangle<\/code> object <code>R1<\/code> with color <code>\"Blue\"<\/code>, length <code>23<\/code>, and width <code>45<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Calling Methods<\/strong>:\n<ul class=\"wp-block-list\">\n<li>For <code>c1<\/code>, the program calls:\n<ul class=\"wp-block-list\">\n<li><code>calculatearea()<\/code>: Computes the area of the circle using \u03c0r2\\pi r^2\u03c0r2.<\/li>\n\n\n\n<li><code>getcolor()<\/code>: Retrieves the color of the circle.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>For <code>R1<\/code>, the program calls:\n<ul class=\"wp-block-list\">\n<li><code>calculatearea()<\/code>: Computes the area of the rectangle using length\u00d7width\\text{length} \\times \\text{width}length\u00d7width.<\/li>\n\n\n\n<li><code>getcolor()<\/code>: Retrieves the color of the rectangle.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Output<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>The area of the circle = 2120.64<br>The color of the circle = Black<br>The area of the rectangle = 1035<br>The color of the rectangle = Blue<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Accessing and Understanding Abstract Classes<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Abstract classes cannot be instantiated directly.\n<ul class=\"wp-block-list\">\n<li><strong>Example<\/strong>: <code>shape s; \/\/ Invalid<\/code><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>They must be used through inheritance.<\/li>\n\n\n\n<li>Pure virtual functions enforce the derived classes to implement specific methods.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Difference Between Abstract, Pure Abstract, and Interface Classes<\/strong><\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>Type<\/strong><\/th><th><strong>Definition<\/strong><\/th><th><strong>Example<\/strong><\/th><\/tr><\/thead><tbody><tr><td>Abstract Class<\/td><td>Contains at least one pure virtual function but can have implemented methods.<\/td><td><code>shape<\/code> in this example.<\/td><\/tr><tr><td>Pure Abstract Class<\/td><td>All member functions are pure virtual, no concrete methods allowed.<\/td><td><code>shape<\/code> (if it had no constructors or members).<\/td><\/tr><tr><td>Interface Class<\/td><td>Special form of a pure abstract class with no data members.<\/td><td>Similar to Pure Abstract class.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Abstract classes serve as blueprints for creating derived classes, enforcing a set of rules or behaviors through pure virtual functions.<\/li>\n\n\n\n<li>This tutorial demonstrated how abstract classes allow us to define generalized behavior while leaving the specifics to derived classes.<\/li>\n\n\n\n<li><strong>Key Takeaways<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Use abstract classes to enforce a contract for derived classes.<\/li>\n\n\n\n<li>Use pure virtual functions for mandatory method implementation.<\/li>\n\n\n\n<li>Implement interface-like behavior in C++ using pure abstract classes.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>This makes object-oriented programming more modular, reusable, and maintainable.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction to Abstract Classes An abstract class in C++ is a class designed to be specifically used as a base class. It cannot be instantiated on its own and typically includes at least one pure virtual function. A pure virtual function is a function declared within a class that has no implementation relative to the base class and must be implemented by all derived classes. Abstract classes are crucial in object-oriented programming to enforce a contract for derived classes. Key&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/afzalbadshah.com\/index.php\/2025\/03\/26\/abstract-class-pure-abstract-class-and-interface-in-c-oop\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":198,"featured_media":5126,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[351,637],"tags":[],"class_list":["post-5124","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-courses","category-oop-with-c"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/12\/Abstract-classes-in-OOP-C.png?fit=1920%2C1080&ssl=1","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pf3emP-1kE","jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/5124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/users\/198"}],"replies":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/comments?post=5124"}],"version-history":[{"count":1,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/5124\/revisions"}],"predecessor-version":[{"id":5125,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/5124\/revisions\/5125"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media\/5126"}],"wp:attachment":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media?parent=5124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/categories?post=5124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/tags?post=5124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}