I have hard time understaning of what is Inheritance?

Can someone help on this?

Dani AI

Generated

Good starter replies from and . The posts cover the basics; the following concentrates on the practical C++ issues beginners commonly miss and gives a small, safe example to try.

Decide to use inheritance only when substitutability holds (derived objects must behave like base objects), and when runtime polymorphism is required. If the goal is only reuse of implementation, composition is usually a better choice. The Liskov Substitution Principle is the formal guideline for substitutability (see Liskov substitution principle). For language details and rules, consult the C++ reference on inheritance and virtual functions (, cppreference: virtual).

A minimal, commonly recommended pattern is to give polymorphic bases a virtual destructor and to prefer the override specifier on overrides:

struct Base {
    virtual ~Base() = default;
    virtual void action() const = 0;
};

struct Derived : Base {
    void action() const override { std::cout << "done\n"; }
};

// use via pointer or reference to avoid slicing
std::unique_ptr<Base> p = std::make_unique<Derived>();
p->action();

Quick checklist of common pitfalls:

  • If derived cleanup is not happening when deleting through a base pointer, make sure the base destructor is virtual.
  • If an override is not called, check the function signature and add override to catch mismatches at compile time.
  • Avoid object slicing by storing polymorphic objects via pointers or references, not by value.
  • Diamond/multiple-inheritance issues often indicate a design problem; consider virtual base classes only when explicitly needed or prefer composition.
  • Prefer composition over inheritance for "has-a" or reuse-only relationships.

For more patterns and rules, the C++ Core Guidelines are useful (CppCoreGuidelines).

Recommended Answers

All 4 Replies

What do you mean by inheritance in real life? The child gets what his parents have if they let him have it. That is the same in programming too.

If you have a class A, whose child is class B, class B gets the things class A that has been declared public or protected by the programmer. Anything declared private will not be inherited.

Inheritance simply means Reusing the Interface of the base class. Two classes have something in common for example consider the following example. There are two classess Shape(a base/parent class) and Circle(a derived/child class ) you might have guessed there is some relationship between shape and Circle. Every shape has color including circle. So if we make the interface of the shape and inherit the interface of the shape class to circle we just need to mention the information specific to Circle

class Shape {
public:
void draw();
void fill_color();

};

//inheritance syntax
class Shape: public Shape {
public:
void Set_radius();
private:
size_t radius;
};

This is the simplest information i can provide hope you got the basic idea....:confused:

Y dont u search in web. U can get tons of material related to inheritance.
Just give the string "Inheritance tutorials"(google)

Thank you all for your prompted reply. I got it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.