Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 76185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:38:01+00:00 2026-05-10T20:38:01+00:00

Let’s say we have a concrete class Apple . (Apple objects can be instantiated.)

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:38:02+00:00Added an answer on May 10, 2026 at 8:38 pm

    Re Peach from Apple:

    • Don’t do it if Apple is a value class (i.e. has copy ctor, non-identical instances can be equal, etc). See Meyers More Effective C++ Item 33 for why.
    • Don’t do it if Apple has a public nonvirtual destructor, otherwise you invite undefined behaviour when your users delete an Apple through a pointer to Peach.
    • Otherwise, you’re probably safe, because you haven’t violated Liskov substitutability. A Peach IS-A Apple.
    • If you own the Apple code, prefer to factor out a common abstract base class (Fruit perhaps) and derive Apple and Peach from it.

    Re Tomato from Berry:

    • Same as above, plus:
    • Avoid, because it’s unusual
    • If you must, document what derived classes of Tomato must do in order not to violate Liskov substitutability. The function you are overriding in Berry – let’s call it Juice() – imposes certain requirements and makes certain promises. Derived classes’ implementations of Juice() must require no more and promise no less. Then a DerivedTomato IS-A Berry and code which only knows about Berry is safe.

    Possibly, you will meet the last requirement by documenting that DerivedTomatoes must call Berry::Juice(). If so, consider using Template Method instead:

    class Tomato : public Berry { public:     void Juice()      {         PrepareJuice();         Berry::Juice();     }     virtual void PrepareJuice() = 0; }; 

    Now there is an excellent chance that a Tomato IS-A Berry, contrary to botanical expectations. (The exception is if derived classes’ implementations of PrepareJuice impose extra preconditions beyond those imposed by Berry::Juice).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 110k
  • Answers 110k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer For MFC dialogs, have a look at ResizableLib. May 11, 2026 at 9:33 pm
  • Editorial Team
    Editorial Team added an answer This solution assumes that you have a data structure that… May 11, 2026 at 9:33 pm
  • Editorial Team
    Editorial Team added an answer lock does induce a memory barrier, so if you are… May 11, 2026 at 9:33 pm

Related Questions

Let's say you create a wizard in an HTML form. One button goes back,
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName
Let me try to explain what I need. I have a server that is
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.