Results 1 to 7 of 7

Thread: Inheritance in C++ (urgent)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Posts
    18

    Inheritance in C++ (urgent)

    I have a Class Account with some functions like
    withdraw , deposit, Print as public functions
    and I have also classes called Checking , Savings which are inherited from the above ones . I have overided the print function of Class Account in both Checking and Savings classes.
    But my below code does not work properly.

    Account *A[10];

    A[1] = new Checking();
    A[2] = new Savings();

    When I reference A[1]->print() sometimes it is giving some error or just printing from Account but not coming from Checking or savings
    Can Any one Help me

    thanks

  2. #2

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Posts
    18

    inheritance in c++(urgent)

    if my question was not clear please let me know
    can Anyone help me in this ?

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    For this you need to use virtual inheritance, which I explained somewhere else....do a search for Base and Derived

    Make the public functions that get overridden virtual:
    Code:
    class Base {
    public:
        virtual void Print() { ... }
    };
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4

    Thread Starter
    Junior Member
    Join Date
    May 2001
    Posts
    18
    Thanks,
    I guess it works out

  5. #5
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    Like:

    class CObject
    {
    public:
    CObject();
    ~CObject();

    virtual void draw() {}
    };

    class CCube : public CObject
    {
    public:
    CCube();
    ~CCube();

    void Draw()
    {
    // Drawing Code
    }
    };

    Sorry if its a bit late etc but what the hell

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Don't see why people put "C" in front of class names, but oh well

    Anyway, that's just normal inheritance. Virtual inheritance is calling a derived class member function through a base class pointer. The compiler magically knows to call the right one Cool really
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

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