CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2004
    Posts
    1

    [RESOLVED] Problem with class inheritance

    In Java i used to do this:

    - create a parent class:
    Class shape
    - extend the class:
    Class circle extends shape

    ..and then when i needed to declare a subclass:

    shape myCircle = new circle();

    My questions are:
    1) what is the name of this technique?
    2) I'm having trouble doing this in C++,how can it be done?

    I've tried this:

    class shape {...};
    class circle : public shape {...};

    and then when i need to declare the class:

    shape *myShape;
    myShape = (circle*)new circle();

    It compiles OK,and works fine unless i'm trying to access methods of the circle class that are not included in the parent class.
    In this case it gives me an error saying that method is not part of the shape class.

    Thank you!

  2. #2
    Join Date
    Nov 2006
    Posts
    1,611

    Re: Problem with class inheritance

    You have the general concept. It's called polymorphism.

    myShape = (circle*)new circle();

    This uses a C style cast, and isn't required. Since circle inherits from shape there ought to be little trouble with,

    myShape = new circle();

    Since myShape is the base class - there can be complicated exceptions that you'll not likely encounter.

    The complaint of the compiler is entirely appropriate.

    You can't access member functions in circle which are not virtual from myShape - they are unknown from that generic scope.

    You could cast the myShape to a circle, but ask yourself, how would the compiler know to call a circle member as opposed to a square, rectangle or triangle, similarly derived but with their own unique non-virtual members?

    Frankly, the notion that you want to call functions specific to circle when you only know the item as a generic shape is an error.

    It is also error to pollute the base shape with specific functions of all the possible derivatives you have.

    Review, instead, why you need to call these functions. The purpose of using a base to such a family is to construct functions to be generic to shapes. There's no logic to fashion a generic viewpoint and then violate that by making specific calls in the generic routines.

    Instead, you should think to fashion generic operations which then perform the specific requirements in the descendant classes.

    I have a post on this same subject

    http://www.codeguru.com/forum/showthread.php?t=480570

    Seems to be a similar subject matter, too.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured