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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:35:06+00:00 2026-06-10T07:35:06+00:00

Consider the following program: class Base { private: int m_nID; public: Base() { m_nID

  • 0

Consider the following program:

class Base
{
private:
    int m_nID;
public:
    Base()
    {
        m_nID = ClassID();
    }

    // ClassID returns a class-specific ID number
    virtual int ClassID() { return 1; }

    int GetID() { return m_nID; }
};

class Derived: public Base
{
public:
    Derived()
    {
    }

    virtual int ClassID() { return 2; }
};

int main()
{
    Derived cDerived;
    cout << cDerived.GetID(); 
    return 0;
}

In the above example,The derived id is surprisingly 1 instead of 2.I have already found a similar question regarding the same question here.But what i don’t understand is that if this is wrong,Then how are we supposed to identify a (derived) class member and use it?I mean suppose i want to dedicate a unique id or typename to each class (base class, the first derived class , the second derived class which is either a derivation of the base class or the second derived class and so on),In this regard how can i go on and act?
I think the correct way is that i assign an id/name in the constructor upon the instantiation of any class object so that the type is known immediately.The above approach failes,What other options do i have in this regard?

  • 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-06-10T07:35:08+00:00Added an answer on June 10, 2026 at 7:35 am

    To elaborate on what Karoly said, maybe you can just avoid using the class ID in the constructor, but after construction you can just do:

    cout << cDerived.ClassID();
    

    There is no reason to have two functions that return the same thing and there is no reason to waste memory by having your int m_nID; stored in every object.

    Also, you should change your base class so that it says:

    virtual int ClassID() = 0;
    

    This should make it a compiler error if you try to call ClassID in your Base constructor, though I have not tried it. Also, it will make Base be an abstract class, so you can’t create new instances of it (which is good).

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

Sidebar

Related Questions

consider the following program: class Base { public: virtual void foo() const { cout
Consider this demo program: #include <stdio.h> class Base { public: virtual int f(int) =0;
Consider the following program. #include<iostream> using namespace std; class base { public: int _bval;
consider the following program: namespace NS2 { class base { }; template<typename T> int
Consider the following sample code: class Base { public: void f(); virtual void vf();
Consider the following program: class A { public static void Foo() { } }
Consider the following program: #include <stdio.h> int main(void) { return 0; } When i
Consider the following C program: int i = 0; int post_increment_i() { return i++;
Consider the following code: class Program { public static explicit operator long(Program x) {
Consider the following program #include <iostream> #include<cstdlib> using namespace std; class E { public:

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.