Need example of how to use INHERITANCE!

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: Need example of how to use INHERITANCE!

 
0
  #11
Apr 15th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Need example of how to use INHERITANCE!

 
0
  #12
Apr 15th, 2005
>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.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: Need example of how to use INHERITANCE!

 
0
  #13
Apr 21st, 2005
GOT THIS FROM MY BOOK


  1. Employee e, *ePtr = &e;
  2. HourlyWorker n, *hPtr = &h;
  3. ePtr->print(); //call base-class print function
  4. hPtr->print(); //call derived-class print function
  5. ePtr = &h; //allowable implicit conversion
  6. 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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Need example of how to use INHERITANCE!

 
0
  #14
Apr 21st, 2005
>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.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: Need example of how to use INHERITANCE!

 
0
  #15
Apr 21st, 2005
^^^

gotcha!

no im not that person, why is he asking stupid questions too
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,867
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Need example of how to use INHERITANCE!

 
0
  #16
Apr 22nd, 2005
>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.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: Need example of how to use INHERITANCE!

 
0
  #17
Apr 22nd, 2005
>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:
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 2
Reputation: wilsonian is an unknown quantity at this point 
Solved Threads: 0
wilsonian wilsonian is offline Offline
Newbie Poster

Re: Need example of how to use INHERITANCE!

 
0
  #18
Apr 28th, 2005
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)
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: Need example of how to use INHERITANCE!

 
0
  #19
Apr 28th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 2
Reputation: wilsonian is an unknown quantity at this point 
Solved Threads: 0
wilsonian wilsonian is offline Offline
Newbie Poster

Re: Need example of how to use INHERITANCE!

 
0
  #20
Apr 28th, 2005
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)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 5317 | Replies: 19
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC