I kind of know the definition through studying Java.]

Inheritance is like when a child class inherits some value, object, or function from a parent class, right?

I need an example of how its done in C++

Recommended Answers

All 19 Replies

>I need an example of how its done in C++
Inheritance in C++ is far more complicated than Java; it would take me too much space to do it justice. You would be better off getting a good book on the subject. Thinking in C++ covers it well from what I hear, and Scott Meyers' Effective C++ series covers the trickier details. Of course, you could also ask a more pointed question that can be easily answered in a single post.

p.s. Clean out your PM box, it's full.

>I need an example of how its done in C++
Inheritance in C++ is far more complicated than Java.

So that's why, dave said he didn't really understand it!

What is a Virtual Function?

>So that's why, dave said he didn't really understand it!
Well, inheritance in C++ has a lot of facets to it. There are multiple ways of inheriting, for example. You can have public inheritance, private inheritance, protected inheritance, each of them can be virtual, and they can all be mixed and matched using multiple inheritance. Compare that with Java. ;)

>What is a Virtual Function?
A virtual function is a member function that can be redefined in derived classes to do something different than the base class. It's different from how non-virtual functions in derived classes override their base class counterparts because using virtual functions you can create behavioral polymorphism. That's a fancy way of saying that you can create an object of the derived class, assign it to a pointer or reference to the base class, and the redefined member function will be called instead of the base class function:

class Base {
public:
  void f1() { cout<<"Base class"<<endl; }
  virtual void f2() { cout<<"Base class"<<endl; }
};

class Derived: public Base {
public:
  void f1() { cout<<"Derived class"<<endl; }
  void f2() { cout<<"Derived class"<<endl; }
};

int main()
{
  Base a;
  Derived b;
  Base& r = b;
}

In the above code, if you say a.f1(), you'll get "Base class" as expected. If you say a.f2(), you'll also get "Base class" as expected. The same goes for b, b.f1() and b.f2() both print "Derived class". The fun part comes when you call f1 and f2 from r.

C++ will look at the type of the object itself when calling member functions unless the member function is virtual. That is, because r is a reference to Base, the Base member functions will be called instead of the Derived member functions. You can verify that by calling r.f1(). It prints "Base class". r's static type is reference to Base.

Now, since r actually references an object of Derived, the dynamic type of r is Derived. This is where virtual member functions come in. A virtual member function will be called on the dynamic type of an object rather than the static type, so calling r.f2() will print "Derived class" because f2 is virtual and r's dynamic type is Derived.

That's really all a virtual function is; it's a way to implement behavioral polymorphism.

>So that's why, dave said he didn't really understand it!
Well, inheritance in C++ has a lot of facets to it. There are multiple ways of inheriting, for example. You can have public inheritance, private inheritance, protected inheritance, each of them can be virtual, and they can all be mixed and matched using multiple inheritance. Compare that with Java. ;)

>What is a Virtual Function?
A virtual function is a member function that can be redefined in derived classes to do something different than the base class. It's different from how non-virtual functions in derived classes override their base class counterparts because using virtual functions you can create behavioral polymorphism. That's a fancy way of saying that you can create an object of the derived class, assign it to a pointer or reference to the base class, and the redefined member function will be called instead of the base class function:

class Base {
public:
  void f1() { cout<<"Base class"<<endl; }
  virtual void f2() { cout<<"Base class"<<endl; }
};

class Derived: public Base {
public:
  void f1() { cout<<"Derived class"<<endl; }
  void f2() { cout<<"Derived class"<<endl; }
};

int main()
{
  Base a;
  Derived b;
  Base& r = b;
}

In the above code, if you say a.f1(), you'll get "Base class" as expected. If you say a.f2(), you'll also get "Base class" as expected. The same goes for b, b.f1() and b.f2() both print "Derived class". The fun part comes when you call f1 and f2 from r.

C++ will look at the type of the object itself when calling member functions unless the member function is virtual. That is, because r is a reference to Base, the Base member functions will be called instead of the Derived member functions. You can verify that by calling r.f1(). It prints "Base class". r's static type is reference to Base.

Now, since r actually references an object of Derived, the dynamic type of r is Derived. This is where virtual member functions come in. A virtual member function will be called on the dynamic type of an object rather than the static type, so calling r.f2() will print "Derived class" because f2 is virtual and r's dynamic type is Derived.

That's really all a virtual function is; it's a way to implement behavioral polymorphism.

That's a big bite, let me chew on it, digest and regurgitate and we'll talk

give me a few ;)

>So that's why, dave said he didn't really understand it!
Well, inheritance in C++ has a lot of facets to it. There are multiple ways of inheriting, for example. You can have public inheritance, private inheritance, protected inheritance, each of them can be virtual, and they can all be mixed and matched using multiple inheritance. Compare that with Java. ;)

>What is a Virtual Function?
A virtual function is a member function that can be redefined in derived classes to do something different than the base class. It's different from how non-virtual functions in derived classes override their base class counterparts because using virtual functions you can create behavioral polymorphism. That's a fancy way of saying that you can create an object of the derived class, assign it to a pointer or reference to the base class, and the redefined member function will be called instead of the base class function:

class Base {
public:
  void f1() { cout<<"Base class"<<endl; }
  virtual void f2() { cout<<"Base class"<<endl; }
};

class Derived: public Base {
public:
  void f1() { cout<<"Derived class"<<endl; }
  void f2() { cout<<"Derived class"<<endl; }
};

int main()
{
  Base a;
  Derived b;
  Base& r = b;  //Does that mean that Base& in now an alias for the class called Bass or r is now the alias for call by reference purposes?
}

In the above code, if you say a.f1(), you'll get "Base class" as expected. If you say a.f2(), you'll also get "Base class" as expected. The same goes for b, b.f1() and b.f2() both print "Derived class" (I'm with you so far!). The fun part comes when you call f1 and f2 from r.

I'm lost Na :( let me keep reading though!

C++ will look at the type of the object itself when calling member functions unless the member function is virtual. That is, because r is a reference to Base, the Base member functions will be called instead of the Derived member functions (STOP STOP STOP!...then shouldn't r = a, I'm confused). You can verify that by calling r.f1(). It prints "Base class". r's static type is reference to Base.

Now, since r actually references an object of Derived, the dynamic type of r is Derived (Huh?...Okay so rvalue r is now b which is a memeber of the class Derived). This is where virtual member functions come in (LOL, I see now that my interviewer was WAY ahead of me!)

. A virtual member function will be called on the dynamic type of an object rather than the static type, so calling r.f2() will print "Derived class" because f2 is virtual and r's dynamic type is Derived.

That's really all a virtual function is; it's a way to implement behavioral polymorphism.

Okay, I'm going to be straight up, I am not even in the ballpark!

1. What is polymorphism? Does it mean many Morphs?
2. I don't see static or dynamic anywhere in your code, how can I tell which type is which? What makes it static and what makes it dynamic?
3. This is by far the most confusing aspect of C++ we've discussed, I'll get it though ;)

>1. What is polymorphism? Does it mean many Morphs?
Yes.

>2. I don't see static or dynamic anywhere in your code, how can I tell which type is which?
The static type is how you declared it. r is a reference to Base because it was declared:

Base& r;

The dynamic type is what the object really is. r was initialized with a Derived object, so the dynamic type is Derived.

>3. This is by far the most confusing aspect of C++ we've discussed
Yes. Yes, it is. The good news is that you can do a lot of useful programming without touching inheritance.

>1. What is polymorphism? Does it mean many Morphs?
Yes.

>2. I don't see static or dynamic anywhere in your code, how can I tell which type is which?
The static type is how you declared it. r is a reference to Base because it was declared:

Base& r;

The dynamic type is what the object really is. r was initialized with a Derived object, so the dynamic type is Derived.

>3. This is by far the most confusing aspect of C++ we've discussed
Yes. Yes, it is. The good news is that you can do a lot of useful programming without touching inheritance.

I must not be clear on what a static and dynamic class or object is, what is your definition of a dynamic or static object?

I've been doing some reading on inheritance and working my way up to the section on virtual functions. As soon as I do I want to continue our discussion on inheritance :D

>what is your definition of a dynamic or static object?
Static or dynamic type. A static type is the type that you declare an object as:

int i; // Static type is int
string s; // Static type is string

The dynamic type is determined with what is called dynamic binding, or late binding. Your book may talk about them when referring to virtual member functions. The best explanation of dynamic binding I can think of right now is a costume. A wolf puts on a sheep costume. The wolf looks like a sheep, but it's actually a wolf:

Sheep *kid = new Wolf;

kid's static type is Sheep, but it's dynamic type is Wolf. kid can go anywhere that a Sheep can, so it's polymorphic.

>what is your definition of a dynamic or static object?
Static or dynamic type. A static type is the type that you declare an object as:

int i; // Static type is int
string s; // Static type is string

The dynamic type is determined with what is called dynamic binding, or late binding. Your book may talk about them when referring to virtual member functions. The best explanation of dynamic binding I can think of right now is a costume. A wolf puts on a sheep costume. The wolf looks like a sheep, but it's actually a wolf:

Sheep *kid = new Wolf;

kid's static type is Sheep, but it's dynamic type is Wolf. kid can go anywhere that a Sheep can, so it's polymorphic.

^^^ okay i kind of understand that to some degree. I think if I read more on Inheritance, Polymorphism and Virtual Functions I'll understand it better and can ask better questions


=========================


Narue

I am close to returning to this thread and talking more about inheritance, I've been studying the chapter and one thing keeps leading to another.

The last thing I remember was reading up on "Casting Base-Class Pointer to Derived-Class Pointers" some how ended up in the "Data Structures" chapter reading up on Linked Lists. I read the first line of text on that and had to study Self-referential Classes and Nodes which is where I am right, confused.

On top of that, last night I discovered that my text book has a whole chapter dedicated to Polymorphisms and Virtual Functions....arrrrrrgh

Give me some time to learn a little more. Oh I also came across what you was talking about, Bitwise operators:
1. AND (&)
2. inclusive OR (|)
3. exclusive (^)

There's a whole chapter on that called, "Bits, Characters, Strings, and Structures" :eek:

Just letting you know I appreciate the help and it's not in vain ;)

Narue,

Basically, a virtual function is a derived class object that references or points to a base class function. My book said something about having a base class Shape with a member function called draw(). Instead of each derived class (i.e. Triangle, Square, Circle, etc.) having it's own member function called draw() they use memebers references or pointers to the draw() function in the base class. And that's how your your basic virtual function works. Am I right?

I'm guessing that polymorphism in this case would be the draw() function in the base class because it is (i'm guessing) dynamic??? And performs a different function for each derived class???

I feel like im getting this :)

please correct me if I'm wrong

>please correct me if I'm wrong
Yes, that's close enough to be considered correct. Your wording is a little funky, but I chalk that up to not being completely comfortable with the terminology.

GOT THIS FROM MY BOOK

Employee e, *ePtr = &e;
HourlyWorker n, *hPtr = &h;
ePtr->print(); //call base-class print function
hPtr->print();  //call derived-class print function
ePtr = &h;  //allowable implicit conversion
ePtr->print();  //still calls base-class print

Our Employee base class and HourlyWorker derived class both have their own print functions defined.

Because the functions were not declared virtual and have the same signature, calling the print function through an Employee pointer results in Employee::print() being called (regardless of whether the Employee pointer is pointing to a base-class Employee object or a derived-class HourlyWorker object) and calling the print function through an HourlyWorker pointer results in HourlyWorker::print() being called.

Why is that and what does declaring it virtual do?

>Why is that and what does declaring it virtual do?
You wouldn't happen to be known as C+++C_forever elsewhere, would you? Think of virtual as meaning "May be redefined in derived classes" and re-read this thread from the beginning.

^^^

gotcha!

no im not that person, why is he asking stupid questions too :(

>why is he asking stupid questions too
Stupid questions I can deal with. C+++C_forever managed to weedle out of me (among other threads, but this is the most memorable) a very long, thorough, and complete description of how overloading the new and delete operators works in C++. He then proceeded to learn nothing from it and asked the original question several more times before finally disappearing from the forum after claiming that he had mastered C++ and was moving to Java.

I had flashbacks to those threads when you asked what virtual did after I went through a rather detailed explanation of just that.

>He then proceeded to learn nothing from it and asked the original question several more times before finally disappearing from the forum after claiming that he had mastered C++ and was moving to Java.

LOL!

>I had flashbacks to those threads when you asked what virtual did after I went through a rather detailed explanation of just that

Ahhhh damn, I knew my questions were/are terrible, but damn Na' not that bad, me no him, NEVER THAT BAD

i hope :)

But seriously i didn't notice/remember how you declared f2() as virtual, remeber you gave me that example when i was really struggling with inheritance/virtual functions/polymorphism. It took a little longer than I'd like to admit but I got it now. C'mon Na' give me a little credit, think back to how much i've learnt (without my own computer i might add) in such little time. Recap: Macros, Functions, Arrarys, Call-by-Reference and Value, Inheritance, Virtual Functions, Polymorphism, Classes/Structs, Data Structures, Pointers, Difference between Parameters and Arguments, etc.

Master Narue you know I'm your best student and you love the hell outta me, admit it :mrgreen:

Virtual functions work like this:

You declare a function virtual in the base class. eg

(Shape.h)

class Shape {
virtual void draw();
};

Then, you define that function in the derived class:

(Square.h)

class Square : public Shape {
void draw();
}

(Square.cpp)

void Square::draw() {
//draw a square - 4 equal-length sides intersecting at right angles
}

Now when you declare a pointer to an object of the base class, and assign to it a pointer to an object of the derived class eg

Shape* s = new Square();

if you call the virtual function on that object ie s->draw()

Square's draw() function will be called even though s is declared as a pointer to a Shape.

This means you don't have to know what type of Shape you are pointing to to call the draw() function and get the behaviour of a particular type of shape; this is polymorphism (one definition of it, anyway)

Virtual functions work like this:

You declare a function virtual in the base class. eg

(Shape.h)

class Shape {
virtual void draw();
};

Then, you define that function in the derived class:

(Square.h)

class Square : public Shape {
void draw();
}

(Square.cpp)

void Square::draw() {
//draw a square - 4 equal-length sides intersecting at right angles
}

Now when you declare a pointer to an object of the base class, and assign to it a pointer to an object of the derived class eg

Shape* s = new Square();

if you call the virtual function on that object ie s->draw()

Square's draw() function will be called even though s is declared as a pointer to a Shape.

This means you don't have to know what type of Shape you are pointing to to call the draw() function and get the behaviour of a particular type of shape; this is polymorphism (one definition of it, anyway)

Shape* s = new Square();

^^^okay so that means, s is an object of the base-class Shape and it points to a function called Square(), but where did the Square() functions come from? To me it seems like s is pointing to something that doesn't exist.

Shape* s is a pointer to a Shape object.

new Square() creates a new Square object (Square() is the constructor that takes no parameters for the Square class) and returns a pointer to that object.

So now Shape* s is a pointer to a Shape object, which in this case just happens to be a Square object (Square is derived from Shape, remember)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.