user963241
user963241

Reputation: 7048

Does the code get copied to derived class?

Whenever I derive a new class from base class say:

#include <iostream>

class A {

protected:
     int f;

public:

    void get() {
        std::cout << "The address is: "
        << &f << std::endl;
        }
};

class B : public A {
        // ....
};

int main() {

     A a;
     a.get();

     B b;
     b.get();

     return 0;
}

The address is: 0xbfb0d5b8
The address is: 0xbfb0d5bc

Does this means that all the code from class A will be copied to class B? Since I have nothing in class B i.e. no data members or functions So, when I create an instance of class B, then I find that it has its own variable at different address and it also has a member function. How can it have its own of copy members if it they aren't copied?

Is that what do we mean by code reuse in inheritance?

Edit:

Updated my code to reflect what I meant by copying of variables.

Upvotes: 1

Views: 200

Answers (6)

Oktalist
Oktalist

Reputation: 14714

§1.8(2) of the C++ language standard defines what is meant by a subobject:

Objects can contain other objects, called subobjects. A subobject can be a member subobject (9.2), a base class subobject (Clause 10), or an array element. An object that is not a subobject of any other object is called a complete object.

These are examples of member subobjects and array elements, which you should be familiar with:

int a[5];
a[2]; // the third array element subobject of the complete object a

struct S { int x; }
S s;
s.x; // a member subobject of the complete object s

That leaves the remaining kind of subobject, the one which you are interested in: base class subobjects.

struct B { int x; }
struct D : public B { int y; }

D d;
d.y; // a member subobject of the complete object d
d.x; // a member subobject of a base class subobject of the complete object d

B &b = d; // a reference to a base class subobject of the complete object d

Every instance of a derived class contains an instance of its base class, as a base class subobject.

Upvotes: 1

didierc
didierc

Reputation: 14730

In your example, you are comparing two different instances of two different classes. different instances means different base addresses to store instances data.

Perhaps a better test of whether a field of a class is copied over in derived classes is the following:

#include <iostream>

class A {

protected:
 int f;

public:

void get() {
    std::cout << "The address is: " << &f << std::endl;
    }
};

class B : public A {
    // ....
};

int main() {

 B b;
 b.get();

 A *a = &b;
 a->get();

 return 0;
}

And the output is:

The address is: 0x7fff41d523f0
The address is: 0x7fff41d523f0

With that program, we can see that even though we have an instance of class B, its inherited content is physically the same as the one in the original class A. Note that it is also possible to redefine a class member in a derived class, but the original member will still be available if we coerce an instance of the derived class to the parent class (as I did in my example).

Upvotes: 1

ravi
ravi

Reputation: 10733

I also have some doubts regarding relation between inheritance and code re-use. This is my take on this.

Inheritance is a mechanism used to categorize and facilitate polymorphism. Using inheritance we can build a hierarchy of concepts separated in categories at different levels of abstraction. By doing this, we can efficiently use another OOP concept, polymorphism, which allows the same control code to manage all objects in a category even if they are different in their implementation.

I don't think that we use inheritance for code re-use purpose.

Upvotes: 0

wolfPack88
wolfPack88

Reputation: 4203

I'm not sure "copied" is the right word (the compiler will only compile the code in class A once). As you have used public inheritance, class B actually is a special type of class A. As such, class B does have access to every (non-private) member of class A, so the code is re-used.

In addition, from the conversation in the comments:

No. Nothing is "copied", each instance of A and B has their own value for their own variables, except for any static data members. For static data members, again there is no copying going on; there is simply only one variable and that is shared by all instantiations of A and B.

Upvotes: 1

Charles Matanda
Charles Matanda

Reputation: 44

Code is never copied during inheritance. But when the child object (class B) is created or instantiated at run time, it inherits the functionality and attributes of the parent class/object (class A).

Upvotes: 2

No Idea For Name
No Idea For Name

Reputation: 11577

the code from class A does NOT copied to class B in the sense that there is only one place the code is written.

however, and here cones the Reusable part, when using class b u can call the method and use the members, with respect to private, public, etd and thus does not have to write the same code for two class that do the same thing

for example if i have a circle and a square, and they both have a member called color that i want a method that change it, i do not need to write the method and the member twice, but have them inherit class Shape that will implement it once and then they both will be able to use that method, thus reusing one method in two places

Upvotes: 1

Related Questions