Skip to main content

Concrete Class & Abstract Class

|

In C++, a concrete class is a class that can be instantiated to create objects, while an abstract class cannot be instantiated and typically contains at least one pure virtual function. Here's a simple C++ program to illustrate the difference between a concrete class and an abstract class:


#include <iostream>

using namespace std;

// Abstract Class

class AbstractClass {

public:

    // Pure virtual function

    virtual void display() = 0;


    // Concrete method

    void show() {

        cout << "This is a concrete method in an abstract class." << endl;

    }

};

// Concrete Class

class ConcreteClass : public AbstractClass {

public:

    // Implementation of the pure virtual function

    void display() override {

        cout << "This is the implementation of the pure virtual function in a concrete class." << endl;

    }

};

int main() {

    // AbstractClass obj; // This will cause a compilation error

    ConcreteClass obj; // Instantiating the concrete class

    obj.display(); // Calling the implemented method

    obj.show(); // Calling the concrete method from the abstract class

    return 0;

}


Explanation:


- Abstract Class:

  - Defined using the keyword `class`.

  - Contains a pure virtual function: `virtual void display() = 0;`.

  - Cannot be instantiated directly.


- Concrete Class:

  - Inherits from the abstract class: `class ConcreteClass : public AbstractClass`.

  - Implements the pure virtual function: `void display() override`.

  - Can be instantiated to create objects.


In the `main()` function, attempting to instantiate `AbstractClass` will result in a compilation error, while instantiating `ConcreteClass` is allowed and we can call both the implemented pure virtual function and the inherited concrete method.


This example illustrates the key differences between concrete and abstract classes in C++. Feel free to experiment with this code and let me know if you have any questions or need further details! 🚀

Comments

Popular posts from this blog

Reasons for a C++ Program Crash

C++ programs may crash unexpectedly for various reasons. Here are some typical causes of such crashes: Segmentation Fault A segmentation fault is a major cause of program crashes. It occurs when: Attempting to access a memory location that doesn’t exist. Trying to write to a read-only memory location. Accessing protected memory locations, such as kernel memory. Example: int main() {     char *text;     // Stored in the read-only part of the data segment     text = "ABC";     // Problem: trying to modify read-only memory     *(text + 1) = 'n';     return 0; }   Stack Overflow Stack overflow happens due to non-terminating recursion, which exhausts the stack memory. Example: #include <stdio.h> void functionRecursive(int num)  {     if (num =...

Understanding push_back and emplace_back in C++

| Understanding push_back and emplace_back in C++ C++ provides several mechanisms to add elements to its containers, and two often used are push_back and emplace_back . Understanding the difference between these methods can help you write more efficient and expressive code. Let's delve into these concepts with examples to illustrate their usage and benefits.

Local Classes in C++: A Deep Dive

Understanding Local Classes in C++: A Deep Dive A class declared within a function is known as a local class in C++. These classes are specific to the function in which they are declared and offer several unique characteristics and constraints. What is a Local Class? A local class is a class defined within a function. For instance, in the following example, the Test  class is local to the fun()  function: #include<iostream>  using namespace std;  void fun()    {      class Test  // local to fun      {          // members of the Test class     };  }  int main()  {      return 0;  } Interesting Facts About Local Classes Scope Limitation A local class type name can only be used within the enclosing function. For example, in the following program, the LocalClass is valid within myFunction() , but not in main() . #include <iostream> void myFunct...