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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:39:45+00:00 2026-05-30T13:39:45+00:00

I have this code: #include <iostream> using namespace std; class FooA { public: virtual

  • 0

I have this code:

#include <iostream>

using namespace std;

class FooA {
    public:
    virtual void meth () {cout << "hiA\n";}
};
class FooB : public FooA{
    public:
    void meth () {cout << "hiB\n";}
};
class FooC : public FooB {
    public:
    void meth () {cout << "hiC\n";}
};

int main() {
    FooA a;
    a.meth();
    FooB b;
    b.meth();
    FooC c;
    c.meth();
    FooA* ptr = &a;
    ptr->meth();
    ptr = &b;
    ptr->meth();
    ptr = &c;
    ptr->meth();
    FooB* ptrB = &b;
    ptrB->meth();
    ptrB = &c;
    ptrB->meth();
}

The output is this:

hiA
hiB
hiC
hiA
hiB
hiC
hiB
hiC

The code is basic and self-explanatory. I tried different combinations of making meth() in each class virtual or not. This will change how the output, obviously, especially if FooA::meth() is not declared as virtual. I have come to the conclusion that it is probably best and most clear to just make every instance of that method virtual. Is there any reason not to?

Edit:
I have another question. When you declare a class, and then define it outside of the class declaration, using ::

Like this

class Foo{
public:
virtual void bar();
};

void Foo::bar {
cout << "Hello";
}

Why can’t I do something like

virtual void Foo::bar {
cout << "Hello";
}
  • 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-30T13:39:46+00:00Added an answer on May 30, 2026 at 1:39 pm

    Is there any reason not to?

    It’s superfluous.

    If you have a method in a base class declared as virtual, this virtuality will be “inherited” by the overriding method.

    Other than that, maybe for documentation purposes it might be acceptable. However, remember that the best virtuals are private, employ the non-virtual interface idiom.

    With that done, the “for documentation” reason falls to the ground, since private methods need no public documentation.

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

Sidebar

Related Questions

I have this code #include <iostream> using namespace std; class Test{ public: int a;
Consider this code #include <iostream> #include <cstdio> using namespace std; class Dummy { public:
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {
I have this code from programming pearls #include <iostream> //#include <string> using namespace std;
When I compile and run this code: #include <iostream> using namespace std; class Base
This is the code: #include <iostream> #include <exception> using namespace std; class excp1:exception {
I have this code main.cpp #include <iostream> #include functs.h using namespace std; int main()
I have the following code: #include <iostream> using namespace std; class testing{ int test()
If I have this kind of hierarchy: #include <iostream> using namespace std; template<class T>
i have following code #include <iostream> #include <set> #include <string> using namespace std; template<class

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.