| | |
Need example of how to use INHERITANCE!
![]() |
>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.
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'm here to prove you wrong.
>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:
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.
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:
C++ Syntax (Toggle Plain Text)
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; }
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.
I'm here to prove you wrong.
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Narue
>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:
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++ Syntax (Toggle Plain Text)
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; }
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
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Narue
>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:
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.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? }
I'm lost Nalet 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.
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:
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.
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:
C++ Syntax (Toggle Plain Text)
Base& r;
>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'm here to prove you wrong.
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Narue
>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:
The dynamic type is what the object really is. r was initialized with a Derived object, so the dynamic type is Derived.C++ Syntax (Toggle Plain Text)
Base& r;
>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'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
>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:
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:
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.
Static or dynamic type. A static type is the type that you declare an object as:
C++ Syntax (Toggle Plain Text)
int i; // Static type is int string s; // Static type is string
C++ Syntax (Toggle Plain Text)
Sheep *kid = new Wolf;
I'm here to prove you wrong.
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Narue
>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:
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:C++ Syntax (Toggle Plain Text)
int i; // Static type is int string s; // Static type is string
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.C++ Syntax (Toggle Plain Text)
Sheep *kid = new Wolf;
^^^ 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
![]() |
Similar Threads
- need idea for project using classes and inheritance (C++)
- Inheritance (C++)
- C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays (C++)
- Inheritance & Derived Classes (C++)
Other Threads in the C++ Forum
- Previous Thread: passing a vector to a function? :errors:
- Next Thread: help with closing a 2d array file
| Thread Tools | Search this Thread |
action api array auto based beginner binary bitmap c++ c/c++ calculator challenge char class classes code coding compile console conversion count createcopyofanyfileinc delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game garbage givemetehcodez graph gui hmenu homeworkhelp homeworkhelper iamthwee ifstream input insert int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node noob output parameter pointer primenumbersinrange problem program programming project python random read recursion reference rpg sockets string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets







let me keep reading though!