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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:35:10+00:00 2026-05-18T04:35:10+00:00

I’m having a problem with abstract/virtual classes, a replication of the problem here: #include

  • 0

I’m having a problem with abstract/virtual classes, a replication of the problem here:

#include <iostream>

class A
{
protected:
    virtual std::string getDateTime() = 0;
    virtual void Write(std::string data, bool addDate) = 0;
    virtual bool CheckFile() = 0;
    virtual bool OpenFile(std::string path) = 0;
    virtual void CloseFile() = 0;
};

class B
    : public A
{
public:
    virtual std::string ToString() { return ""; };
    virtual void Write(std::string data) { };
};

class C
    : public A
{
protected:
    std::string getDateTime()
    {
        return "TODAY";
    };

    void Write(std::string data, bool addDate)
    {
        std::cout << "BasicClassA Write" << std::endl;
    };

    bool CheckFile()
    {
        std::cout << "BasicClassA CheckFile" << std::endl;
        return true;
    };

    bool OpenFile(std::string path)
    {
        std::cout << "BasicClassA OpenFile" << std::endl;
        return true;
    };

    void CloseFile()
    {
        std::cout << "BasicClassA CloseFile" << std::endl;
    };
};

class D
    : public B,
      public C
{
public:
    BasicClassB();
    virtual ~BasicClassB();

    std::string ToString()
    {
        return "BasicClassB tostring";
    };

    void Write(std::string data)
    {
        std::cout << "BasicClassB Write" << std::endl;
    };
};

int main(int ac, char *av[])
{
    BasicClassB b;
    std::cout << b.ToString() << std::endl;
    b.Write("");
    return 0;
}

This has a compile error:

../src/main.cpp: In function ‘int main(int, char**)’:
../src/main.cpp:82: error: cannot declare variable ‘b’ to be of abstract type ‘BasicClassB’
../src/main.cpp:64: note: because the following virtual functions are pure within ‘BasicClassB’:
../src/main.cpp:13: note: virtual std::string BaseClassA::getDateTime()
../src/main.cpp:14: note: virtual void BaseClassA::Write(std::string, bool)
../src/main.cpp:15: note: virtual bool BaseClassA::CheckFile()
../src/main.cpp:16: note: virtual bool BaseClassA::OpenFile(std::string)
../src/main.cpp:17: note: virtual void BaseClassA::CloseFile()

Perhaps I’m missing the point here, but the implementation of BaseClassA (being BasicClassA) should contain these functions, and since BasicClassB is subclassed from BasicClassA as well, it should also contain these functions?

What am I missing? What should I do to make this compile?

[edit]
I updated the class names as suggested by the comment
For clarification: I used pure virtual in the class A to force any of the children to implement the functions.

It seems virtual inheritance is what I need, however, I don’t seem to get the correct way on how to do this in my case…

The goal is to have several “base” classes, kind of like interfaces, forcing the children to implement the functions, but any children of those should inherit the overriden function (just like virtual inheritance)

However, using any combination of
class Any : public virtual Anyother { }
doesn’t work out and always gives the same compile error (the one above). Perhaps I need to change more than just the virtual in the inheritance?

  • 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-18T04:35:11+00:00Added an answer on May 18, 2026 at 4:35 am

    It doesn’t work that way by default in C++ – You want a diamond inheritance pattern, but in C++ you get separate roots: So BasicClassA and BaseClassB each have their own BaseClassA (vtable and instance variables).

    You probably want to use Virtual Inheritance.

    For a clearer idea on non-virtual inheritance:

    #include <iostream>
    
    
    class A
    {
        public:
            A(int x) {m_a = x;}
            virtual ~A() {}
            int m_a;
            virtual int getA() {return m_a;}
    };
    
    class B : public A
    {
        public:
            B() : A(1) {}
    };
    
    class C : public A
    {
        public:
            C() : A(2) {}
    };
    
    class D : public B,
              public C
    {
    };
    
    void useB(B* b)
    {
        std::cout << "useB:" << b->getA() << std::endl;
    }
    
    void useC(C* c)
    {
        std::cout << "useC:" << c->getA() << std::endl;
    }
    
    int main()
    {
        D* d = new D();
        useB(d);
        useC(d);
    
        return 0;
    }
    

    This produces the output:

    useB:1
    useC:2
    

    This example shows virtual inheritance, and the kind of mix-in behaviour you want.

    #include <iostream>
    
    
    class A
    {
        public:
            A(int x) {m_a = x;}
            virtual ~A() {}
            int m_a;
            virtual int getA() {return m_a;}
            virtual int virt() = 0;
    };
    
    class B : virtual public A
    {
        public:
            B() : A(1) {}
    };
    
    class C : virtual public A
    {
        public:
            C() : A(2) {}
            virtual int virt() {return 42;}
    };
    
    class D : public B,
              public C
    {
        public:
            D() : A(3) {}
    };
    
    
    
    void useB(B* b)
    {
        std::cout << "useB:" << b->getA() << std::endl;
    }
    
    void useC(C* c)
    {
        std::cout << "useC:" << c->getA() << std::endl;
        std::cout << "useC-virt:" << c->virt() << std::endl;
    }
    
    int main()
    {
        D* d = new D();
        useB(d);
        useC(d);
    
        return 0;
    }
    

    Output:

    useB:3
    useC:3
    useC-virt:42
    

    Note: The constructors from C and B don’t get a say in setting m_a, which is controller by the D() constructor initialisation list.

    EDIT:
    Applying virtual to your code:

    #include <iostream>
    
    class A
    {
    protected:
        virtual std::string getDateTime() = 0;
        virtual void Write(std::string data, bool addDate) = 0;
        virtual bool CheckFile() = 0;
        virtual bool OpenFile(std::string path) = 0;
        virtual void CloseFile() = 0;
    };
    
    class B
        : virtual public A
    {
    public:
        virtual std::string ToString() { return ""; };
        virtual void Write(std::string data) { };
    };
    
    class C
        : virtual public A
    {
    protected:
        std::string getDateTime()
        {
            return "TODAY";
        };
    
        void Write(std::string data, bool addDate)
        {
            std::cout << "C Write" << std::endl;
        };
    
        bool CheckFile()
        {
            std::cout << "C CheckFile" << std::endl;
            return true;
        };
    
        bool OpenFile(std::string path)
        {
            std::cout << "C OpenFile" << std::endl;
            return true;
        };
    
        void CloseFile()
        {
            std::cout << "C CloseFile" << std::endl;
        };
    };
    
    class D
        : public B,
          public C
    {
    public:
        std::string ToString()
        {
            return "D tostring";
        };
    
        void Write(std::string data)
        {
            std::cout << "D Write" << std::endl;
        };
    };
    
    int main(int ac, char *av[])
    {
        D b;
        std::cout << b.ToString() << std::endl;
        b.Write("");
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.