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

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.

constexpr in C++

|  Let’s dive into the depths of constexpr in C++! constexpr is short for "constant expression." It was introduced in C++11 and further enhanced in C++14 and C++20. The primary purpose of constexpr is to allow the evaluation of expressions at compile-time, enabling several powerful optimizations. Here’s a detailed breakdown: Purpose of constexpr The idea behind constexpr  is to inform the compiler that the value of a variable or the result of a function can be determined at compile-time. It will be if the expression can be evaluated at compile-time, resulting in performance benefits. It’s beneficial for: - Compile-time constants: Values that don’t change at runtime. - Optimizations: Allowing the compiler to optimize code more effectively. - Template metaprogramming: Enhancing the power of templates. Usage in Variables A constexpr  variable must be initialized with a constant expression.  Here’s an example: constexpr int length = 10; constexpr int width = 5; conste...

When do we use Initializer List in C++?

An initializer list is used to initialize the data members of a class. This list of members to be initialized is specified in the constructor as a comma-separated list, followed by a colon. Here is an example that demonstrates the use of an initializer list to initialize the variables x and y in the Point class. #include<iostream>  using namespace std;     class Point {  private:      int x;      int y;  public:      Point(int i = 0, int j = 0):x(i), y(j) {}       /*  The above use of the Initializer list is optional as the           constructor can also be written as:          Point(int i = 0, int j = 0) {              x = i;              y = j;          }      */              ...