Using Inheritance Without Polymorphism in SDL/C++

Do I *have* to use polymorphism if I use inheritance?

No, you don't have to use polymorphism just because you're using inheritance. Inheritance and polymorphism are related but distinct concepts in C++.

What Inheritance Provides Alone

Inheritance, by itself, provides several key benefits:

  • Code Reuse: A derived class (like GreenRectangle) automatically inherits the members (data and functions) of its base class (Rectangle). You only need to write code for what's new or different in the derived class.
  • Is-A Relationship: Inheritance establishes an "is-a" relationship. A GreenRectangle is a Rectangle. This reflects a logical relationship between the classes.
  • Specialization: You can add new members or override non-virtual methods (though overriding non-virtual methods is often discouraged) to specialize the behavior of the derived class.

You can use inheritance purely for these reasons without ever needing polymorphism. For example, if you simply wanted a GreenRectangle type that always started green, you could use it directly:

// GreenRectangle.h
#pragma once
#include "Rectangle.h"

class GreenRectangle : public Rectangle {
 public:
  GreenRectangle(const SDL_Rect& Rect)
  : Rectangle{Rect} {
    // Set specific properties after base
    // class construction
    SetColor({0, 255, 0});
    SetHoverColor({0, 150, 0}); // Maybe a darker green
  }
  // Can add specific GreenRectangle methods here
};
// main.cpp (Conceptual Example)
#include "GreenRectangle.h"
#include <vector>
// ...

int main(int argc, char** argv) {
  // ...

  GreenRectangle SpecialRect{SDL_Rect{10, 10, 50, 50}};
  std::vector<GreenRectangle> GreenRects;

  GreenRects.emplace_back(SDL_Rect{70, 10, 50, 50});
  GreenRects.emplace_back(SDL_Rect{130, 10, 50, 50});
  
  // ...
}

In this scenario, we are using GreenRectangle and maybe even storing it in a vector of GreenRectangle, but we aren't treating it polymorphically through a Rectangle* or Rectangle&.

When Polymorphism is Needed

Polymorphism becomes essential when you want to treat objects of different derived types uniformly through a pointer or reference to their common base class.

This is exactly what we did in the lesson when we created a std::vector<std::unique_ptr<Rectangle>>. This vector could hold pointers to both Rectangle objects and GreenRectangle objects. When we iterated through the vector and called ptr->Render(Surface), polymorphism (enabled by the virtual keyword, discussed elsewhere) ensures the correct Render function (either the base Rectangle version or the derived GreenRectangle version) gets called based on the actual type of object the pointer points to at runtime.

So, use inheritance alone for code reuse and specialization. Add polymorphism (using virtual functions and base class pointers/references) when you need to manage and operate on collections of related but distinct object types through a common interface.

Structuring SDL Programs

Discover how to organize SDL components using manager classes, inheritance, and polymorphism for cleaner code.

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Removing UI Components from a std::vector in SDL/C++
How would I remove a component from the UI manager's std::vector?
The virtual Keyword and Polymorphism in C++
What does the virtual keyword do, and why might I need it for polymorphism in SDL UI components?
How C++ Calls the Correct Method with virtual Functions (Dynamic Dispatch)
How does the computer know whether to call Rectangle::Render() or GreenRectangle::Render() when using pointers?
Controlling Drawing Order for Overlapping UI Components in SDL
How is the drawing order determined if components overlap in this UI structure?
Importance of Event Handling Order for UI Components in SDL
Does the order I call HandleEvent() on children matter?
Implementing Resizable UI Components in SDL
What's the best way to handle resizable UI components in SDL?
Creating a Scrollable Container in SDL
How do I create a scrollable container for UI elements that exceed the window size?
Implementing a Modal Dialog in SDL UI
How would I implement a modal dialog box using this component hierarchy?
Animated UI Transitions in SDL
How can I create animated transitions between different UI states or screens?
Implementing Responsive UI Design in SDL
What's the best approach for implementing a responsive design that adapts to different window sizes?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant