Thread: Object Arrays and Inheritance

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    3

    Question Object Arrays and Inheritance

    I'm quite new to programming but I've looked all over the web and cant find anything that answers my question, so now I'm trying this, hope you can help!

    So, I'm trying to create a class that has many variables and many functions to access them, plus a few more to use the data. I made this test program to see whether I had the object oriented concepts down. Apparently, I dont. I've gotten the object array to work, but the most important part doesn't work. That is, when I use parentobject->function() the function only gets called to the parent object and not all its children, which is what I want. Is there some TObjArray library or other way of doing this that would be easier. I'm trying to avoid using a for loop every time I want to call a function to each child object (that would slow things down greatly). I'd like to be able to call uniform functions to each object with one line of code. Here's an example of what I'm working with. Thanks in advance for any help.

    const int TOTAL=40;

    class TDot
    {
    public:
    void SetCapacity(int StartCapacity);
    void SoundOff();
    TDot* D[TOTAL];
    int Capacity;
    //and stuff
    private:
    //more stuff
    }

    #include<stuff>

    int NumStartDots=10,StartCapacity=40,wait;
    TDot *Population = new TDot;

    void main(void)
    {


    for (int i=0;i<NumStartDots;i++)
    {
    Population->D[i] = new TDot;
    Population->D[i]->SetCapacity(StartCapacity);
    }

    Population->SoundOff();
    cin>>wait;
    }

    void TDot::SetCapacity(int StartCapacity)
    {
    Capacity=StartCapacity;
    }

    void TDot::SoundOff()
    {
    cout<<Capacity<<endl;
    }


    -AceHigh

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Well first things first. After your class declaration you need to follow it with a semi-colon. Example:

    Code:
    class CSimple
    {
        private:
          // foo!
        public:
          // more foo! (public foo)
    };  // <--- notice here the semi-colon!
    Also when you set in your new values for the capicity that looks fine. Then you only call soundoff on the main class, not the array INSIDE of that class. So you'd need another for loop and have the array indexing like you did to set them, only now you display them. Also make sure you delete that memory! Try using the destructor and letting that delete the memory, and when you are deleting an array use the subscript notation.

    delete [] pointer;

    Hope this clears some things up.

  3. #3
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    I'm a little confused by reading your code (use code tags in future, it helps loads) but I think you are talking about virtual functions. If you call a virtual function via a base class pointer the class type is decided at runtime (late-binding) and the correct function is called. Here is a brief example:
    Code:
    class Shape
    {
    public:
       virtual float area( ) = 0;  //pure virtual function
    };
    
    class Square : public Shape
    {
    public:
       Square(  );
       Square( float l, float w );
       virtual float area( ) { return length*width; }
    
    private:
       float length, width;
    };
    
    class Circle : public Shape
    {
    public:
       Circle( );
       Circle( float r );
       virtual float area( ) { return PI*(radius*radius); }
    
    private:
       float radius
    };

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    3

    Exclamation

    So does this mean that there is no way to send a function to a parent object and have it be called in all its children (all the same class objects, and all the same function)? I thought it was possible (without a for loop for the already stated reason) because when using ROOT which is a CINT used for making graphs etc, there was a way to do this:

    TObjArray *Poly = new TObjArray(40)

    for (i=0;i<40;i++)
    {
    Poly->Add(marker[i]);
    //Set values for each marker
    }

    Poly->Draw; //<---- this being the line that takes each
    //marker in the array and draws it.

    So, does an equivalent not exist in standard C++?

    AceHigh

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I don't understand exactly what you are asking either. I initially thought this was about inheritance but you don't have any classes inheriting anything. I am a little confused here, what do you want to do? If you want to call a same function with your derived class before the base class then as endo said you need to declare the functions as virtual in the base class.

  6. #6
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176
    I think that he's asking...
    If AceHigh has a class TDot, whenever he calls TDot::Soundoff, he wants every instance of TDot to Soundoff.

    I don't know of anything that keeps track of all instances of a class. Maybe use a static array of some sort? Then, whenever a new object is created, have the constructor add itself to the end of the list.

    fletch
    "Logic is the art of going wrong with confidence."
    Morris Kline

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    3
    Right, my question was is there a way to make all of the objects in a class call a function without the use of a for loop. I think though that I've gotten my answer, which is that the only way to access every TDot in Population is to run through the D[] array with a for loop. It's strange though that a C interpreter like ROOT would have the capability to do this while C++ does not. Or maybe I'm just confused and the ROOT class TObjArray has just hidden the for loop that actually makes it work.

    Regardless, thanks for your help.

    -AceHigh

  8. #8
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Yes, use a loop. The question seems much clearer now, if you have an array of 20 class objects use a for loop to change the index and call the function for each object.

    Code:
    Myobject objects[ 20 ];
    
    for( int i = 0; i < 20; i++ )
    {
       objects[ i ].someMemberFunction( );
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struct array's inheritance
    By emorrp1 in forum C Programming
    Replies: 8
    Last Post: 06-27-2008, 05:54 AM
  2. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 09:54 AM
  3. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  4. Inheritance and Arrays
    By codegirl in forum C++ Programming
    Replies: 18
    Last Post: 06-06-2003, 12:46 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM