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

  • SEARCH
  • Home
  • 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 6214605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:54:28+00:00 2026-05-24T06:54:28+00:00

I have the following code: #include <iostream> class Grandma { public: virtual void foo()

  • 0

I have the following code:

#include <iostream>

class Grandma
{
public:
    virtual void foo() = 0;
};

class Mom : public Grandma{};

class Dad
{
public:
    void foo() { std::cout << "dad's foo impl"; }
};

class Me : public Mom, public Dad{};

int main()
{
    Me m;
    m.foo();
}

and getting : cannot instantiate abstract class

I know what this error means, I know I can’t instantiate Grandma because of pure-virtual.

But why can’t I instantiate Me, when compiler knows I am derived from Mom and Dad and Dad has foo implemented?

I know I can fixed it by adding foo into Me and inside it call e.g. Dad::foo, but I am afraid it is not solution for my case.

Is it really necessary to have virtual method implementation between its declaration and instantiated object (when traversing “class hierarchy graph”)? See ASCI graph

    A
    |
B1  B2
  \ |   
   C1  C2
    | /
    |/
    D
    |
    E

When I want to instantiate E and have virtual declaration in A, the only way to make it run is to define it in A, B2, C1, D or E?
and similarly when is virtual declaration in C2 only way is to define it in C2, D or E?

I know this may be silly question, but I had a loooong day and can not think anymore.

Please, do not answer just with – “It is not possible”, but try to add explanation why not.

Thank you!

EDIT — foo() in Dad is of course should not be private

  • 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. Editorial Team
    Editorial Team
    2026-05-24T06:54:29+00:00Added an answer on May 24, 2026 at 6:54 am

    There is no relation between Dad class and Grandma class. Dad and Grandma are two completely different classes. Given that the method in Dad is not considered as an implementation of the pure virtual method in Grandma and has to be implemented in the Me class.

    When you derive from an Abstract class(class containing atleast one pure virtual function) the deriving class needs to override and implement ALL the pure virtual member functions of the Base Class. If not, the deriving class becomes Abstract too, same applies to the classes deriving from the derived class further down the hierarchy.

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

Sidebar

Related Questions

I have the following code #include <iostream> #include <vector> class Entity { public: virtual
I have the following code #include <iostream> using namespace std; class Object { public:
i have following code #include <iostream> #include <utility> using namespace std; namespace rel_ops{ template<class
I have following code: #include <iostream> using namespace std; template<class T> T max(T *data,int
I have the following code: #include <iostream> using namespace std; class testing{ int test()
I have the following code : #include <cstdarg> #include <iostream> using namespace std; class
I have written the following code: #include <iostream> using namespace std; template <class T>
i have following code #include <iostream> #include <set> #include <string> using namespace std; template<class
I have the following code: #include <iostream> using namespace std; template <class T> class
I have the following code: #include <iostream> #include boost/shared_ptr.hpp using boost::shared_ptr; class Base {

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.