| | |
Need example of how to use INHERITANCE!
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
GOT THIS FROM MY BOOK
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?
C++ Syntax (Toggle Plain Text)
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 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.
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.
New members chased away this month: 5
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
>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:
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:
•
•
Join Date: Apr 2005
Posts: 2
Reputation:
Solved Threads: 0
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)
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)
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by wilsonian
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.
•
•
Join Date: Apr 2005
Posts: 2
Reputation:
Solved Threads: 0
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)
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)
![]() |
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
Views: 5317 | Replies: 19
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






