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 7531257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:07:27+00:00 2026-05-30T05:07:27+00:00

I am trying to define interface types in C++ using abstract classes and implement

  • 0

I am trying to define interface types in C++ using abstract classes and implement them with concrete classes. The problem I am running into is that I cannot both inherit and interface from another interface and inherit the implementation from a base concrete class.

My goal is to be able to define a hierarchy of interfaces which may inherit from less complex base interfaces. I also want to be able to extend the implementation of interfaces by inheriting from concrete classes (like inheriting from TObjectA in the example below).

This is what I have. The error I am getting is “object of abstract class type “TObjectB” is not allowed”. I believe I know why, which is because I didn’t implement MethodA() in TObjectB. But I really want is to have the implementation provided by a base class (TObjectA) and still have interface hierarchies (IInterfaceB inherits from IInterfaceA). I also don’t want to repeat all the inherited interface methods in my derived concreate classes. How can I do this?

class IInterfaceA 
{ 
public:
    virtual void MethodA() = 0; 
}; 

class IInterfaceB : IInterfaceA
{ 
public:
    virtual void MethodB() = 0; 
}; 

class TObjectA : public IInterfaceA 
{ 
public:
    void MethodA() { cout << "Method A"; }  
}; 

class TObjectB : public TObjectA, public IInterfaceB 
{ 
public:
    void MethodB() { cout << "Method B"; } 
}; 

void TestInterfaces()
{
    IInterfaceB* b = new TObjectB(); // error: object of abstract class type "TObjectB" is not allowed
    b->MethodB();
    delete b;
}
  • 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-30T05:07:29+00:00Added an answer on May 30, 2026 at 5:07 am

    In your class hierarchy, TObjectB actually has two IInterfaceA base class subobjects: one inherited through IInterfaceB and one inherited through TObjectA. The MethodA() from each of them needs to be implemented.

    You need to inherit from the interface classes using public virtual inheritance, which ensures there is only a single base class subobject of each interface class type:

    class IInterfaceA 
    { 
    public:
        virtual void MethodA() = 0; 
    }; 
    
    class IInterfaceB : public virtual IInterfaceA
    { 
    public:
        virtual void MethodB() = 0; 
    }; 
    
    class TObjectA : public virtual IInterfaceA 
    { 
    public:
        void MethodA() { cout << "Method A"; }  
    }; 
    
    class TObjectB : public TObjectA, public virtual IInterfaceB 
    { 
    public:
        void MethodB() { cout << "Method B"; } 
    }; 
    
    void TestInterfaces()
    {
        TObjectB b_object;
        IInterfaceB& b = b_object;
        b.MethodB();
    }
    

    Whether or not such complex class hierarchies are desirable is another matter altogether.

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

Sidebar

Related Questions

I am trying to define an interface and classes which implement the interface as
I am trying to define a WCF contract that returns an interface, something like
I'm trying to define a task that emits (using echo) a message when a
I'm trying to define a delegate function that will return an IEnumerable. I'm having
I'm trying to define a way to simulate an case on accessing into a
I am using unity as my IoC container. I am trying to implement a
I am trying to implement an IDescription Interface. Basic purpose of this interface is
I am trying to dynamically (using reflection) add types to a MEF catalog and
I have a generic class that I'm trying to implement implicit type casting for.
Using Delphi XE. When trying to access a Delphi interface object from a DLL,

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.