Luv
Luv

Reputation: 5451

Overriding in inheritance in C++

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

Upvotes: 0

Views: 805

Answers (2)

Jerry Coffin
Jerry Coffin

Reputation: 490018

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.

Upvotes: 5

K-ballo
K-ballo

Reputation: 81349

Please note that there is no overriding in your code, only name hiding.

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.

Inheritance does not overwrite variables. The i in class Y will hide the name i from class X, but both will exist, hence the size of 8.

X::set(ii) Can we call the function like this? Is this function of class X invoked for any object?

From within class Y, this qualified call request that the set from X is invoked in a static way instead of the set of Y.

Upvotes: 4

Related Questions