mercredi 30 décembre 2020

C++ Parent Class with Abstracted Function that Uses Functions in a Child Class

I am trying to create a C++ parent class that has two functions, f1 and f2, to be implemented in the child class. This parent class has a function, abstractedFunction that abstracts how f1 and f2 should be used together. Both f1 and f2 are implemented in the child class as shown in the code below.

#include <iostream>

class Parent
{
public:
    int f1();        // To be implemented in the derived class
    void f2(int i);  // To be implemented in the derived class
    void abstractedFunction() { // Abstracted in the parant class 
        auto r = f1();
        f2(r);
    }
};

class Child : public Parent
{
public:
    int f1() {
        std::cout << "f1 is implemented in the child class\n";
        return 1;
    }

    void f2(int i) {
        std::cout << "f2 is implemented in the child class\n";
        std::cout << "Return value for f1 = " << i << "\n";
    }
};

int main() {
    Child ch;
    ch.abstractedFunction();
    return 0;
}

Is such a concept implementable in C++?

Aucun commentaire:

Enregistrer un commentaire