Click to See Complete Forum and Search --> : Can u make an array of obects and how


potluck
September 26th, 2002, 10:59 PM
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.

jfaust
September 26th, 2002, 11:14 PM
I'd suggest std::vector<application*>.

Jeff

potluck
September 26th, 2002, 11:22 PM
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

potluck
September 27th, 2002, 02:18 AM
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);
}

jfaust
September 27th, 2002, 09:33 AM
Not real clear on your question, but maybe this will help:



#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

TruBic
September 29th, 2002, 12:23 AM
You're close to your goal potluck, just a little bit more


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

Axter
September 29th, 2002, 06:50 PM
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;
}

Axter
September 29th, 2002, 07:03 PM
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;
}

potluck
September 29th, 2002, 07:30 PM
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.

jfaust
September 29th, 2002, 07:42 PM
instead of

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

use

pform[i]->askName();


Also, I suggest investing in a C++ book. Some good examples will do you wonders.

Jeff

potluck
September 29th, 2002, 08:08 PM
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.
*********************************

jfaust
September 29th, 2002, 08:15 PM
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

SolarFlare
October 11th, 2002, 07:47 PM
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;
}

jfaust
October 11th, 2002, 10:42 PM
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

potluck
October 12th, 2002, 01:22 AM
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.

jfaust
October 12th, 2002, 10:21 AM
// works
pform[i]->askName();
pform[i]->setName(gchar);

...

// doesn't
pform[i]->osap::askamount();
pform[i]->osap::setamount(gchar);


I am trying to help, and I'm not judging you. There are a lot of issues when answering this question. 1) There is a way to accomplish what you want, but it's frowned upon, and points to an error in your design. 2) This is a bad class hierarchy. It's not natural. It doesn't correctly model "isa". 3) What you are attempting suggests to me that a book would help you further than I could.

I dislike answering questions that further propagate a bad design, or that use techniques that aren't exactly "kosher", but here goes:

From what I gather askamount() and setamount() are part of osap, but not part of form, and you are attempting to access these methods through a base class pointer. To do this, you need to 1) add those methods to the base class as virtual or 2) cast the pointer to the derived class type, as in:

dynamic_cast<osap*>(pform[i])->askamount();


Jeff

potluck
October 12th, 2002, 10:39 AM
thanks but what i ended up doing was putting everything i needed in the default constructers