Alex Rivera | Logout

Deriving an abstract class from concrete class

Asked 2008-11-21T22:20:35.153
15

Let's say we have a concrete class Apple. (Apple objects can be instantiated.) Now, someone comes and derives an abstract class Peach from Apple. It's abstract because it introduces a new pure virtual function. The user of Peach is now forced to derive from it and define this new function. Is this a common pattern? Is this correct to do?

Sample:


class Apple
{
public:
    virtual void MakePie();
    // more stuff here
};

class Peach : public Apple { public: virtual void MakeDeliciousDesserts() = 0; // more stuff here };

Now let's say we have a concrete class Berry. Someone derives an abstract class Tomato from Berry. It's abstract because it overwrites one of Berry's virtual functions, and makes it pure virtual. The user of Tomato has to re-implement the function previously defined in Berry. Is this a common pattern? Is this correct to do?

Sample:


class Berry
{
public:
    virtual void EatYummyPie();
    // more stuff here
};

class Tomato : public Berry { public: virtual void EatYummyPie() = 0; // more stuff here };

Note: The names are contrived and do not reflect any actual code (hopefully). No fruits have been harmed in the writing of this question.

Edit
Report

1 Answer

0

Hmmm...by thinking "what a....." for a couple of seconds, I come to a conclusion it is not common... Also, I would not derive Peach from Apple and Tomato from Berry...do you have any better example?:)

It's a lot of weird shit you can do in C++...I can't even think of 1% of it...

About override a virtual with a pure virtual, you can probably just hide it and it will be really weird...

If you can find a stupid C++ compiler that would link this function as a virtual, then you will get runtime pure virtual function call...

I think this can only be done for a hack and I have no idea what kind of hack really...

answered 2008-11-21T22:31:30.653

Your Answer