Inheritance

This is a discussion on Inheritance within the C++ Programming forums, part of the General Programming Boards category; My text is useless in this area I need clearer picture of Inheritance with pointers. Can someone give me a ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    34

    Question Inheritance

    My text is useless in this area

    I need clearer picture of Inheritance with pointers.

    Can someone give me a brief synopsis or example

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Say you have a base class called CShape that looks 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);
    };
    From which you derive 2 classes CRect and CTri that look like this:

    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
    }
    And here is how you might call it using pointers:
    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();
    **edit** sorry, just realized I didn't really explain much...hehe...
    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:

    Code:
     virtual void Draw(); //notice no "=0"
    In this case Draw has a default implementation defined by the
    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 06:14 PM.

  3. #3
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Talking

    Just Curious: what text are you using??

    A simple example of inheritance could have been used.
    Mr. C: Author and Instructor

  4. #4
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Originally posted by Mister C
    Just Curious: what text are you using??

    A simple example of inheritance could have been used.
    How was this not simple? This is just a generic example of inheritance using pointers as was asked.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    34

    Cool

    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..

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    34

    Talking

    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

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

  8. #8
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Is the only way to redefine by using derived classes to call virtual functions?
    Yes

    Could you call Cshape::Draw? since the implementation was not defined
    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:

    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();

  9. #9
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Wink

    The Deitel text is not always the best about explaining things.
    Mr. C: Author and Instructor

  10. #10
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    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.
    Yes, you can call pure virtual functions. Here's an example:
    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;
    }

  11. #11
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Originally posted by Eibro
    Yes, you can call pure virtual functions.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Virtual inheritance
    By 6tr6tr in forum C++ Programming
    Replies: 13
    Last Post: 05-07-2008, 11:20 AM
  3. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 03:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21