CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Jul 2002
    Posts
    56

    Can u make an array of obects and how

    i need to make an array of objects called application
    can only have five applications each need all the information from class apply. class apply is derived from baseclass form and classes school, bank, and osap, which are derived from baseclass form.
    No my real question is do i make an array of objects or do i make an array of pointers to that object.
    Just Starting Dont Hit

  2. #2
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    I'd suggest std::vector<application*>.

    Jeff

  3. #3
    Join Date
    Jul 2002
    Posts
    56
    std is standard. not sure what a vector is and it looks like a standard header file with a pointer. Not sure what it means please explain
    Just Starting Dont Hit

  4. #4
    Join Date
    Jul 2002
    Posts
    56

    so lost

    i just cant get it.
    im not understanding how i can make an array of objects dynamically.

    main(){

    apply appl; // I need 5 appl of type apply.

    int i = 1;

    apply* ppappl[5]; //is this making an array of pointers to apply?

    ppappl[i] = new apply;//this makes those pointers dynamic
    //but how do i call the info to store it in one
    //particular appl.??


    create(gchar);
    }
    Just Starting Dont Hit

  5. #5
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    Not real clear on your question, but maybe this will help:

    Code:
    #include <vector>
    
    ...
    
    apply* a = ??? // however you get the apply
    
    const int APP_COUNT = 5;
    
    std::vector<application*> vApp;
    for( int i = 0; i < APP_COUNT; ++i )
    {
         // here we can pass in a to the application constructor.
         vApp.push_back(new application(a));
    }

    Jeff

  6. #6
    Join Date
    Sep 2002
    Posts
    33
    You're close to your goal potluck, just a little bit more

    Code:
    main()
    {
       // Note: this one is not dynamic
       apply appl; // I need 5 appl of type apply.
    
       int i = 1;
    
      apply* ppappl[5]; //is this making an array of pointers to apply?
    
      ppappl[i] = new apply;//this makes those pointers dynamic
      //but how do i call the info to store it in one
      //particular appl.??
      ...
    }
    You have the "i" (index to the array), the array "ppappl". To use a particular object in the array you just do ppappl[i]->SomeFunction(...).

    Note: there's two way to access to class members use the dot if you have a reference to the object, or use the "->" if you have a pointer to the object

  7. #7
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968
    Example:

    #include <stdlib.h>

    class foo
    {
    public:
    foo(int ID):MyID(ID){}
    int GetID(){return MyID;}
    private:
    int MyID;
    };

    int main(int argc, char* argv[])
    {
    const int QtyFoo = 7;
    //First create an array of pointers
    foo ** FooPtrArray = new foo*[7];

    int i = 0;

    //Now create Objects for each item in the array
    for (i = 0;i < QtyFoo;++i)
    {
    FooPtrArray[i] = new foo(i*10);
    }

    //Test each item in the array
    for (i = 0;i < QtyFoo;++i)
    {
    printf("This Foo ID equals %i\n", FooPtrArray[i]->GetID());
    }

    //Now delete each item in the array
    for (i = 0;i < QtyFoo;++i)
    {
    delete FooPtrArray[i];
    }

    //Delete the array of pointers
    delete [] FooPtrArray;//Notice this detele uses '[]'

    system("pause");
    return 0;
    }
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  8. #8
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968
    The following example uses a virtual class.

    class fruit
    {
    public:
    virtual const char* GetName(void) = 0;
    ~fruit(){}
    };

    class apple : public fruit
    {
    public:
    apple(int Qty):m_Qty(Qty){strcpy(m_Name, "Apple");}
    const char* GetName(void){return m_Name;}
    private:
    char m_Name[32];
    int m_Qty;
    };

    class orange : public fruit
    {
    public:
    orange(int Qty):m_Qty(Qty){strcpy(m_Name, "Orange");}
    const char* GetName(void){return m_Name;}
    private:
    char m_Name[32];
    int m_Qty;
    };

    class peach : public fruit
    {
    public:
    peach(int Qty):m_Qty(Qty){strcpy(m_Name, "Peach");}
    const char* GetName(void){return m_Name;}
    private:
    char m_Name[32];
    int m_Qty;
    };

    int main(int argc, char* argv[])
    {
    const int QtyFoo = 3;
    //First create an array of pointers
    fruit ** FruitePtrArray = new fruit*[QtyFoo];

    FruitePtrArray[0] = new apple(9);
    FruitePtrArray[1] = new orange(5);
    FruitePtrArray[2] = new peach(4);

    //Test each item in the array
    for (int i = 0;i < QtyFoo;++i)
    {
    printf("This fruit's name is %s\n", FruitePtrArray[i]->GetName());
    }

    //Now delete each item in the array
    for (i = 0;i < QtyFoo;++i)
    {
    delete FruitePtrArray[i];
    }

    //Delete the array of pointers
    delete [] FruitePtrArray;//Notice this detele uses '[]'

    system("pause");
    return 0;
    }
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  9. #9
    Join Date
    Jul 2002
    Posts
    56

    thank u

    im gettin it now just one question.
    i understand the using the pointers to point to the classes.
    my base class is abstract.

    form ** pform = new form*[5];
    pform[i] = new osap();

    pform[i]->form::askName();
    pform[i]->form::setName(gchar);

    //this right here works perfect. but how do i now using the same pointer point to information in the osap class. ive been trying but cant seem to get in.
    Just Starting Dont Hit

  10. #10
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    instead of
    Code:
    pform[i]->form::askName();
    use
    Code:
    pform[i]->askName();
    Also, I suggest investing in a C++ book. Some good examples will do you wonders.

    Jeff

  11. #11
    Join Date
    Jul 2002
    Posts
    56
    form * pform[5];
    pform[i] = new osap()

    pform[i]->askName();
    pform[i]->setName(gchar);
    this works fine.
    ********************
    pform[i]->osap::askamount();
    pform[i]->osap::setamount(gchar);

    i cant seem to access these functions.
    *********************************
    Just Starting Dont Hit

  12. #12
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    pform[i]->osap::askamount();
    pform[i]->osap::setamount(gchar);
    i cant seem to access these functions.
    Because you should not be doing this to access the functions. The methods askamount and setamount should be declared virtual. Then, if you create the pform pointer as an osap object, it will access the osap versions of the methods. This is the most basic concept of object oriented programming. Before venturing further, you should really look into a book or at least a web tutorial.

    If you spent two hours in any beginning C++ book, you would not have to ask the questions that you are asking. I don't mind helping you, but I'd feel better about it if you would take some initiative and learn C++.

    Jeff

  13. #13
    Join Date
    Sep 2002
    Location
    Philadelphia ***Epoch: Timeless***
    Posts
    560
    int main()
    {
    cout << "\aBelow the belt!";
    jfaust.go(penaltybox,5minutes);
    /*the reason this forum is here is so that beginners can ask personal questions to experienced programmers! You were once a beginner... admit it, it's easier to learn when people answer your questions */
    return 0;
    }
    SolarFlare

    Those who cling to life die and those who defy death live. -Sun Tzu

    cout << endl;
    return 0;
    }

  14. #14
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    Maybe I was a little strong, but this is very fundamental. When answering questions here, I assume we are at least speaking the same language (programming-wise). Asking questions like this is a horrible way to learn C++. At the level that this question is aked, the asker doesn't even know what questions to ask. Stumbling around until you get lucky is not the way to do it. Right from my very first response to this thread, I was wasting my breath. My answer did not help. Not because it wasn't a good answer, but because it wasn't the right question. There is no way this will be better than any decent beginning C++ book.

    I'm not trying to sound rude or condescending, but I don't mince words. What will help more than any answer I can provide is almost any beginning C++ book.

    Jeff

  15. #15
    Join Date
    Jul 2002
    Posts
    56

    whateva

    my question was why i couldnt access the member functions by using a base pointer. theoretically it was right, having a base pointer u should be able to point to anything that is derived from the base. i was just having a bit of trouble. But thanks for the scolding. ive read a book , i was just having some trouble. im sorry i cant be a wizard at c++ like u. id love to be but i dont have much experience. i started over and did what i had to do. not because of your negative feedback but because thats the way programs work. if u cant find a way to do something then try a different way. but thanks for nothing anyways.
    people come here for help not to be judged, its not called code gurus only.
    Just Starting Dont Hit

Page 1 of 2 12 LastLast

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