Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C++ When to use abstraction and when inheritance

Velpus Captiosus

Well-Known Coder
Isnt abstraction the opposite process of inheritance in C++?When we create a abstract class , we create the basis for other classes while inherited classes take some basis and add details.
 
Well, in a very simple and short line, when you want to define a common interface and hide implementation details, you should use abstraction, and when you want to promote code usability, classes, and other implementations, you should use inheritance.

Example of Abstraction

Code:
class Shape {
public:
    virtual double getArea() = 0; // Pure virtual function, must be implemented by derived classes
    virtual void printInfo() {
        std::cout << "This is a shape." << std::endl;
    }
};

Example of inheritance


Code:
class Circle : public Shape {
private:
    double radius;


public:
    Circle(double r) : radius(r) {}


    double getArea() override {
        return 3.14159265 * radius * radius;
    }


    void printInfo() override {
        std::cout << "This is a circle with radius " << radius << "." << std::endl;
    }
};

I hope you are clear now.
Thanks
 
Well, in a very simple and short line, when you want to define a common interface and hide implementation details, you should use abstraction, and when you want to promote code usability, classes, and other implementations, you should use inheritance.

Example of Abstraction

Code:
class Shape {
public:
    virtual double getArea() = 0; // Pure virtual function, must be implemented by derived classes
    virtual void printInfo() {
        std::cout << "This is a shape." << std::endl;
    }
};

Example of inheritance

Code:
class Circle : public Shape {
private:
    double radius;


public:
    Circle(double r) : radius(r) {}


    double getArea() override {
        return 3.14159265 * radius * radius;
    }


    void printInfo() override {
        std::cout << "This is a circle with radius " << radius << "." << std::endl;
    }
};

I hope you are clear now.
Thanks
I guess the difference isnt between Abstraction and Inheritance but between Abstraction and Inheritance+Polymorphism.Thanks a lot btw!
 
Isnt abstraction the opposite process of inheritance in C++?When we create a abstract class , we create the basis for other classes while inherited classes take some basis and add details.

No, abstraction and inheritance are not opposite processes in C++. Abstraction involves simplifying complex reality by creating abstract classes that provide a common interface or blueprint for other classes. Inheritance, on the other hand, allows classes to inherit properties and behaviors from other classes, including abstract classes, to build on that foundation and add specific details. Abstraction sets the foundation, and inheritance builds upon it, so they are complementary concepts.
 
No, abstraction and inheritance are not opposite processes in C++. Abstraction involves simplifying complex reality by creating abstract classes that provide a common interface or blueprint for other classes. Inheritance, on the other hand, allows classes to inherit properties and behaviors from other classes, including abstract classes, to build on that foundation and add specific details. Abstraction sets the foundation, and inheritance builds upon it, so they are complementary concepts.
Thanks a lot!Have a good day!
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom