I need some help/pointers/comments to get me started, I am stuck

T

Tommy Lang

I am working on this project and I need some help/pointers/comments to
get me started, I am stuck.
The program will be used to store information in an array while it is
running.
I need to store objects of my classes Person(superclass),
Student(inherit Person), Teacher(inherit Person) in that array.
The name will be the unique key. These classes are all working ok. I
want to be able to add, remove, find etc. objects.

To all of this, I have created a menue class to print out a menue on
the screen for the user where he can select what he wants to do(add,
remove...) and a class "List" to tie it all together.
In the menue class I have a reference(List &refList;) to the List
class and in the listclass I have a pointer to array that will hold
all the objcts(List *pList[10];).
I init the reference in the constructor of Menu class like
menue::menue(const List &L):refList(L){}

Questions:
When I initiate the object List should I make sure to set all items in
array to NULL in the constructor ?
for(int i=0;i<10;i++)
pList=NULL;
Can I have only one array ex. List *pList[10]; that can hold all types
of objects(Person, Student, Teacher)?
Do I need typecasting to identify objects before adding removing..?
What methodes would I put in List class? (insert, remove, find, print)
When I am searching for an object in array, should I return the object
itself or a pointer to the object?
In case of returning a pointer, how do I return a pointer to the
object I am searching for?


Thankful for any help

/Tommy
 
R

Rolf Magnus

Tommy said:
I am working on this project and I need some help/pointers/comments to
get me started, I am stuck.
The program will be used to store information in an array while it is
running.
I need to store objects of my classes Person(superclass),
Student(inherit Person), Teacher(inherit Person) in that array.
The name will be the unique key.

Sounds like a map would be better than an array.
These classes are all working ok. I want to be able to add, remove,
find etc. objects.

To all of this, I have created a menue class to print out a menue on
the screen for the user where he can select what he wants to do(add,
remove...) and a class "List" to tie it all together.
In the menue class I have a reference(List &refList;) to the List
class and in the listclass I have a pointer to array that will hold
all the objcts(List *pList[10];).
I init the reference in the constructor of Menu class like
menue::menue(const List &L):refList(L){}

Questions:
When I initiate the object List should I make sure to set all items in
array to NULL in the constructor ?

You should make sure to not access uninitialized pointers. But null
pointers must not be dereferenced either.
for(int i=0;i<10;i++)
pList=NULL;
Can I have only one array ex. List *pList[10]; that can hold all types
of objects(Person, Student, Teacher)?


Yes. Make your Person class polymorphic, i.e. give it a virtual
destructor and make all functions that your derived classes need to
override also virtual or pure virtual.
Do I need typecasting to identify objects before adding removing..?

No. A virtual destructor makes it possible to correctly delete a derived
class object through a pointer to its base class.
What C++ book are you reading? Those are the basics of object oriented
programming in C++.
What methodes would I put in List class? (insert, remove, find, print)
When I am searching for an object in array, should I return the object
itself or a pointer to the object?

If you want polymorphism to work, you need to return either a pointer or
reference to it. Both are ok.
In case of returning a pointer, how do I return a pointer to the
object I am searching for?

What do you mean?
 
D

David White

Tommy Lang said:
I am working on this project and I need some help/pointers/comments to
get me started, I am stuck.
The program will be used to store information in an array while it is
running.
I need to store objects of my classes Person(superclass),
Student(inherit Person), Teacher(inherit Person) in that array.
The name will be the unique key. These classes are all working ok. I
want to be able to add, remove, find etc. objects.

To all of this, I have created a menue class to print out a menue on
the screen for the user where he can select what he wants to do(add,
remove...) and a class "List" to tie it all together.
In the menue class I have a reference(List &refList;) to the List
class and in the listclass I have a pointer to array that will hold
all the objcts(List *pList[10];).

So you have an array of 10 pointers to lists. It's not clear to me from your
description what these are for.
I init the reference in the constructor of Menu class like
menue::menue(const List &L):refList(L){}

Questions:
When I initiate the object List should I make sure to set all items in
array to NULL in the constructor ?
for(int i=0;i<10;i++)
pList=NULL;


For an array of pointers, you should initialize each element with either a
null pointer or a valid pointer to an object. But I still don't know why you
have 10 lists.
Can I have only one array ex. List *pList[10]; that can hold all types
of objects(Person, Student, Teacher)?

If you want an array that can hold all these types, then it needs to be an
array of Person*. For example:
Person *persons[10];
You can store a pointer to an object of class Person or any class derived
from Person in any element of this array. You cannot have an array that can
store actual objects of different classes.
Do I need typecasting to identify objects before adding removing..?

You would only need to downcast from Person * to a derived class if you need
to access a member that is uniqe to the derived class. In general,
programmers try to avoid this, but sometimes it's necessary. Ideally, if you
have an array of Person * you would like to use only members that are in
class Person. These members are often virtual functions that are overridden
in the derived class, which results in behaviour suitable for each derived
class.
What methodes would I put in List class? (insert, remove, find, print)

I don't know what your List is for.
When I am searching for an object in array, should I return the object
itself or a pointer to the object?

It will already be a pointer, so you can just return that, or you can return
a reference. You can't return an actual object because that would require
the type of object to be known at compile time, but that can't be since the
array has pointers to objects of different types.
In case of returning a pointer, how do I return a pointer to the
object I am searching for?

For a built-in array:
return persons[index];
where 'index' is the index of the element you want to return.
Thankful for any help

If you want to locate a person based on a name (you said that the name is
the key), then a std::map<std::string, Person*> would be an appropriate type
to use, if you are allowed to. This type would find a person from a name for
you. It also will do insertions and deletions for you. Not as good, but
still better than a built-in array, is a std::list<Person*>. This will do
efficient insertions and deletions, but you'll have to do the search
yourself.

DW
 
K

Kalyan

The first and foremost important thing while using C++ is, do not use
arrays. It's a lot better to use a vector if at all you want use an
array for any functionality. This is being stressed by the inventor
himself.
Have something like:

std::vector<Person *> m_vecPerson;

to contain a list of Persons (i.e; Students and Teachers). Provide two
iterators like:

typedef std::vector<Person *>::const_iterator const_person_iterator;
typedef std::vector<Person *>::iterator person_iterator;

These iterators will be useful to traverse the vector of Persons.

In the List class you can functions like:

List::AddPerson(Person * person)
{
m_vecPerson.push_back(person);
}

List::RemovePerson(Person * person)
{
person_iterator result =
std::find(m_vecPerson.being(),m_vecPerson.end(),person);
if(result != m_vecPerson.end())
{
m_vecPerson.erase(person);
}
}

Similarly you can add the other functions also. Now in your menu, you
can have a reference to the List.

Hope this helps.

Thanks,
Kalyan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top