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

The Archive Base Latest Questions

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

In C#, a virtual method of base class can be either overridden or hidden

  • 0

In C#, a virtual method of base class can be either overridden or hidden by its derived classes, e.g.

using System;

class A {
 public virtual void M() {
  System.Console.WriteLine("A");
 }
}

class B: A {
 public override void M() {
  System.Console.WriteLine("B");
 }
}

class C: A {
 public new void M() {
  System.Console.WriteLine("C");
 }
}

class P {
 static void Main(string[] args) {
  A b = new B();
  b.M();
  A a = new C();
  a.M();
 }
}

Output:

B
A

How about in C++? Only overridable?

  • 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-28T22:54:22+00:00Added an answer on May 28, 2026 at 10:54 pm

    [Update for C++11]

    Since C++11, you can use the override keyword to tell the compiler that you’re intending to override the base class virtual function.

    void foo() override;
    

    If there is no override keyword but the base class contains a virtual function with the same* signature, then the function is still overriden. Otherwise, hidden.

    [Original answer for C++03]

    Of course, you can both override and hide a virtual function in C++. You just aren’t very explicit about it (not in C++03 anyway).

    example

    class A
    {
    public:
       virtual int f(char);
    };
    
    class B : public A
    {
    public:
        virtual int f(); //hides A::f
    };
    
    class C : public A
    {
    public:
        virtual int f(char); //overrides A::f
    };
    

    If the signature coincides with the Base virtual method* then it’s overriding. Otherwise it’s hiding.

    So sometimes you accidentally forget a const and end up hiding the function instead of overriding it. The C++11 override attribute is called to solve this problem. If you use this override attribute but the signatures are incompatible, you will get a compiler error.

    *(or differs only in that the return types are pointers or references to a Base class and a publicly Derived class respectively)

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

Sidebar

Related Questions

I know that calling a virtual method from a base class constructor can be
Imagine a base class with many constructors and a virtual method public class Foo
I have two classes: public class MyBase { public virtual void DoMe() { }
Let say we have a base class and its two derived classes; The base
consider the following program: class Base { public: virtual void foo() const { cout
I have a base class with a virtual method, and multiple subclasses that override
I have an abstract class defining a pure virtual method in c++: class Base
I recently discovered that a method in a derived class can only access the
Let's suppose we have a base class which has a virtual method: class BaseClass
I need to call a virtual method for all classes derived from a given

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.