0

I'm reading the book Head First Design Patterns and am also trying to learn C++. In the book it says to program to an interface/supertype instead of programming to an implementation and the authros give this java example:

//programming to implementation
Dog d = new Dog();
d.bark();
//programming to an interface/supertype (here Animal)
Animal animal = new Dog();
animal.makeSound();

To do the same in C++ would you use an abstract class or generic programming? And how exactly would that look like?

Thanks for any help!

Smyke
  • 87
  • 1
  • 6
  • 1
    If you want to learn C++ I would suggest [a decent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) as complement (or rather as the first book, and the *Head First Design Patterns* book as the complement). – Some programmer dude Jul 01 '20 at 09:52
  • 3
    I think it's pretty difficult to transfer abstract programming concepts to a language that you don't know. Probably you should first learn a basic fundament of C++. After you learned polymorphism you can implement it as abstract class and after you learned generic programming you can implement it as template. You will understand the pros and cons of both options. – Thomas Sablik Jul 01 '20 at 09:56
  • 2
    Yes, the equivalent of interfaces in C++ are abstract classes with no data members. Whether you should use those are not depends on the specific problem you have. I would start by by aiming to solve some real or at least interesting problem, then research patterns that might help you do that - rather than looking at lists of patterns and trying to work out how you could use them. Patterns exist to aid development, not the other way around. – underscore_d Jul 01 '20 at 09:59
  • Hey, I am learning C++ with *A Tour of C++* and am simultaneously looking at *Head First Design Patterns*, and not using the latter as a way to learn C++. I was reading that passage and wondering how you would go about it in C++, if someone could provide a minimal example, that would be really nice :) – Smyke Jul 01 '20 at 10:14

1 Answers1

3

Disclaimer: Translating Java code/patterns/best practices directly to C++ leads you to a world of pain.

Java has interfaces. What comes closest to that in C++ is an abstract base class:

struct Animal {
    virtual void makeSound() const = 0;   // <- pure virtual, ie no implementation in Animal
    virtual ~Animal() = default;
};

struct Dog : Animal {
    void bark() const { std::cout << "Moo"; }
    void makeSound() const override {  bark(); }
};


//programming to implementation
Dog d;                            // C++ has values, not everything is a reference like in Java
d.bark();
//programming to an interface/supertype (here Animal)
std::shared_ptr<Animal> animal = std::make_shared<Dog>();  // Use smart pointers, not raw ones
                                                           // and don't use new in C++
animal->makeSound();

Whether you could do this also with templates is difficult / impossible to answer, because this is nothing more than an example to illustrate the use of an interface / abstract base class and thats the only requirement.

As has been pointed out in a comment, patterns do not exist for their own sake, but to aid development. Once you tackle real problems and you have some familiarity with patterns you will notice when a certain problem requires the use of a pattern.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185