My text is useless in this area
I need clearer picture of Inheritance with pointers.
Can someone give me a brief synopsis or example
My text is useless in this area
I need clearer picture of Inheritance with pointers.
Can someone give me a brief synopsis or example
Say you have a base class called CShape that looks like this:
From which you derive 2 classes CRect and CTri that look like this:Code:struct CornerCoord2d { long x; long y; }; class CShape { public: CShape() : TotalCorners(0) , CornerPoints(0) , Size(0) {}; ~CShape() //where allocated mem is freed from CornerPoints int TotalCorners; CornerCoord2d *CornerPoints; int Size; virtual void Draw() = 0; void Create(const int CornerCount, CornerCoord2d *cc2d, int sz); };
And here is how you might call it using pointers:Code://CRect class CRect : public CShape { public: void Draw(); }; void CRect::Draw() { //code to draw a rectangle } //CTri class CTri : public CShape { public: void Draw(); } void CTri::Draw() { //code to draw a triangle }
**edit** sorry, just realized I didn't really explain much...hehe...Code://declare a pointer to a Shape object CShape *pcs=new CRect; pcs->Draw(); //calls CRect::Draw(); CShape *pcs2=new CTri; pcs2->Draw(); //calls CTri::Draw();
when you declare a derived class as derived from a base class like
that (public) you give the derived class an "is-a" relationship to
the base. In other words, CRect "is a" CShape, + whatever you
add to CRect, however CShape is NOT a CRect. So all the member
variables and functions of CShape are now members of CRect.
A pure virtual function (as we've used here) is a function who's
interface only is inherited and who's implementation MUST be
defined by the derived classes. So you can't call CShape::Draw(),
you must call the Draw function as defined by the derived classes.
If we wanted to declare a virtual function that would have a
default implementation we'd declare it like this:
In this case Draw has a default implementation defined by theCode:virtual void Draw(); //notice no "=0"
CShape object, however derived classes have the option of
defining their own implementation.
Functions like CShape::Create() are inheritted but CAN NOT be
redefined by derived classes because it is nonvirtual.
Last edited by jdinger; 02-24-2003 at 07:14 PM.
Just Curious: what text are you using??
A simple example of inheritance could have been used.
Mr. C: Author and Instructor
How was this not simple? This is just a generic example of inheritance using pointers as was asked.Originally posted by Mister C
Just Curious: what text are you using??
A simple example of inheritance could have been used.
THANKS FOR THAT EXAMPLE
by the way the text is Deitel & Deitel "C++:How to program"
I had to re-read it a couple of times and my picture is becoming unfogged..
This brings up another question
Is the only way to redefine by using derived classes to call virtual functions?
Am I right in assuming that this could be the concept of polymorphism?
In the last example:
Could you call Cshape:raw? since the implementation was not defined
Here's a definition of polymorphism from Webopedia:
polymorphism:
Generally, the ability to appear in many forms. In object-oriented programming, polymorphism refers to a programming language's ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes. For example, given a base class shape, polymorphism enables the programmer to define different circumference methods for any number of derived classes, such as circles, rectangles and triangles. No matter what shape an object is, applying the circumference method to it will return the correct results. Polymorphism is considered to be a requirement of any true object-oriented programming language (OOPL).
Under this definition, it would appear that overloaded functions as well as virtual functions could be considered examples of polymorphism.
YesIs the only way to redefine by using derived classes to call virtual functions?
No you can't call it, because it's a pure virtual function (remember the =0). If you declared it as a "regular" virtual function and defined it then you could call it. Here's an example:Could you call Cshape::Draw? since the implementation was not defined
Code://changed declaration of CShape::Draw in Shape.h virtual void Draw(); //in Shape.cpp void CShape::Draw() { //generic shape drawing code here. } //calling CShape::Draw from a CRect object CShape *ps=new CRect; ps->Draw(); //calls CRect::Draw(); ps->CShape::Draw(); //calls CShape::Draw();
The Deitel text is not always the best about explaining things.
Mr. C: Author and Instructor
Yes, you can call pure virtual functions. Here's an example:No you can't call it, because it's a pure virtual function (remember the =0). If you declared it as a "regular" virtual function and defined it then you could call it.
Code:#include <iostream> class foo { public: virtual void dosomething() = 0 { std::cout << "foo::dosomething"; } }; class bar : public foo { public: virtual void dosomething() { std::cout << "bar::dosomething"; } }; int main(int argc, char** argv) { foo* obj = new bar; obj->foo::dosomething(); return 0; }
You're right, E. You can, but it's not recommended. The whole purpose of a pure virtual function is to inherit interface only (not implementation). If the derived class needs to inherit interface and a default implementation that can be overridden that's a case for a simple virtual function.Originally posted by Eibro
Yes, you can call pure virtual functions.