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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:11:12+00:00 2026-06-05T19:11:12+00:00

class X { int i; public: X() { i = 0; } void set(int

  • 0
class X {

  int i;

public:

  X() { i = 0; }

  void set(int ii) { i = ii; }

  int read() const { return i; }

  int permute() { return i = i * 47; }
};

Above is the definition of class X

Another class Y is there as

class Y : public X {

    int i; // Different from X's i

public:

    Y() { i = 0; }

    int change() {
        i = permute(); // Different name call
        return i;
    }

    void set(int ii) {
        i = ii;
        X::set(ii); // Same-name function call
    }
};

My doubt is that class X also consists of a variable named i and it is been inherited by class Y, but i of class Y should overwrite it, but the size of class(Y) is coming 8.

Secondly, for the line

X::set(ii)

Can we call the function like this?
Is this function of class X invoked for any object?

Many many thanks in advance

  • 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-05T19:11:13+00:00Added an answer on June 5, 2026 at 7:11 pm

    Y::i doesn’t override anything (you can only override a virtual function). It hides X::i, so there are two different is, one in the base class and one in the derived.

    To your second question, outside of the class you can only use syntax like X::set(ii); when set is a static member function, not a normal or virtual member function. Inside the class you can use it to force a particular class’ definition of the member function to be used.

    Edit: I should probably answer the tricky (somewhat related) question: if the static type differs from the dynamic type, which i is used? For example, let’s consider a simplified version:

    class base { 
    protected:  // we'll make `i` protected, so `derived` can access it if necessary.
        int i;
    public:
        base() : i(0) {}
        void hide() { i = 2; }
        virtual void set() { i = 10; }
    };
    
    class derived : public base { 
        int i;
    public:
        derived() : i(0) {}
        void hide() { i = 1; }
        void set() { i = 5; }
    };
    

    Now, since set is virtual, the call in main is to derived::set. Since hide is not virtual, the call in main will be to base::hide(). The question is, which class’ i will each of them assign to?

    The answer is fairly simple: even when the function is virtual, the variable is not, so each function refers to the variable in its own class. Having/lacking virtual controls which function you call, but not which variable is referred to by that function.

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

Sidebar

Related Questions

class a { private: b *b_obj; public: void set(int); }; a::a() { b_obj =
class CRectangle { int x, y; public: void set_values (int,int); int area (void); }
class A{ int a=10; public void show(){ System.out.println(Show A: +a); } } class B
class A{ A(int x){} } class B extends A{ B(int x){} public static void
public class SendImage { public delegate int DWatch(int bytesLeftToSend, IntPtr Response); ret=0xffff; public void
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
Imagine this: Class A{ private int a; } Class B{ public void fn(){ A
class MyClass { public: void method2() { static int i; ... } }; Will
class Foo { public: void bar(); }; void Foo::bar() { static int n =
class classe (){ public: int key; static void *funct(void *context){ printf(Output: %d, , key);

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.