Newbie Question About Abstract classes

This is a discussion on Newbie Question About Abstract classes within the C++ Programming forums, part of the General Programming Boards category; Hi guys im a little stuck atm and wonder if anyone could help me I have a base abstract class ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    118

    Newbie Question About Abstract classes

    Hi guys im a little stuck atm and wonder if anyone could help me

    I have a base abstract class and 3 classes that all inherit from it. These 3 classes are all different of course.

    Now what i would like to do is to use an array of the baseclass to use overloaded functions from the derived classes.

    My code compiles and runs but it doesnt call the derived functions it uses the abstract base class functions(i assume it does as the derived functions output dosent happen).

    I may of done something wrong this is sort of how my code goes.
    Code:
    class bass
    {
    virtual void somefn()=0
    {};
    }// this isnt my actual class :)
    
    class a
    {
    virtual void somefn()
    }
    //ect ect
    
    a Aclass;
    b Bclass;
    c Cclass;
    base myarray[3] = {Aclass,Bclass,Cclass};
    myarray[0].somefn(); //in my program this doesnt work how id like it to
    I had compile issues before so i added the
    Code:
    {}
    and now it compiles i can post the code but its spread over a few files.

    Sorry if i havent provided enough information im just thinking if somebody could give me an example of creating an array of type base with 3 different derived class objects in it and how to call its functions. yeah i know i dont ask for much

    Thanks in advanced

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    5,860
    While it is not necessary to provide all your source files, it would generally be a good idea to create a small but representative sample of actual code that could be posted. That reduces the need for other forum members to invest in training so they can practice mindreading. Also, by writing something that you think might look the same, you run the risk of leaving out relevant details.

    In any event, you need to use an array of pointers to mybase to avoid slicing the objects. One effect of slicing is that polymorphic behaviour (calling a virtual function of the base and having it resolve to the most derived version) does not work.

    More generally, to exploit polymorphism, it is necessary to use a pointer or reference to the (polymorphic) base class.

    For example;
    Code:
    //   your base class and derived classes a,b,c declared here
    
    int main()
    {
          a A;
          b B;
          c C;
          
          base *array[] = {&A, &B, &C};
    
         array[0]->somefn();
         array[1]->somefn();
    }
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    Hi Grumpy sorry for the mangled attempt at showing what i mean but it seems you have hit the nail on the head.
    I will try your example, i did try a similar way but ran into errors i had pointers to my derived class and an array of pointers to base class.

    Thanks for your help i appreciate it

    EDIT: i changed my code they way you showed and now it works as desired
    It would seem that any investments you have made in mind reading were worth every penny
    either that or your just very smart
    Last edited by thestien; 07-23-2011 at 08:02 PM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    5,860
    No problem on that; I failed Telepathy 101, but managed to deduce your meaning from your post anyway.

    Just try to remember to make things a little clearer in future - a little effort to ask your question clearly tends to increase chances of a useful response from someone.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie question about classes.
    By placidjw in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2010, 02:50 PM
  2. abstract classes 2
    By Aisthesis in forum C++ Programming
    Replies: 15
    Last Post: 09-25-2009, 05:42 PM
  3. abstract classes 1
    By Aisthesis in forum C++ Programming
    Replies: 2
    Last Post: 09-22-2009, 06:39 PM
  4. Issue with abstract classes
    By DavidP in forum C# Programming
    Replies: 1
    Last Post: 08-18-2008, 03:03 PM
  5. Abstract classes
    By cisokay in forum C++ Programming
    Replies: 17
    Last Post: 05-29-2005, 09:39 AM

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