473,245 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,245 software developers and data experts.

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

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[i]=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
Jul 22 '05 #1
3 1902
Tommy Lang wrote:
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[i]=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?

Jul 22 '05 #2
"Tommy Lang" <mu*****@yahoo.se> wrote in message
news:78**************************@posting.google.c om...
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[i]=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

Jul 22 '05 #3
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(),pe rson);
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.


mu*****@yahoo.se (Tommy Lang) wrote in message news:<78**************************@posting.google. com>...
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[i]=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

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
7
by: Christian Christmann | last post by:
Hi, in the past I always appreciated your help and hope that you also can help me this time. I've spent many many hours but still can't solve the problem by myself and you are my last hope. ...
11
by: Peter Mount | last post by:
Hello Are there any good online tutorials that explain pointers in ANSI C? I've covered pointers in my course but I'm just having trouble "getting it to sink in". Thanks Peter Mount...
7
by: Jack Addington | last post by:
I've got a fairly simple application implementation that over time is going to get a lot bigger. I'm really trying to implement it in a way that will facilitate the growth. I am first writing a...
5
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
0
by: tgregg6 | last post by:
I need help with my problem I am stuck and I don't know what to do!!! The problem is: This program calculates the charges for DVD rentals where current releases cost $3.5 and all others cast $2.50....
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
11
by: Peted | last post by:
Im using c# 2005 express edition Ive pretty much finished an winforms application and i need to significantly improve the visual appeal of the interface. Im totaly stuck on this and cant seem...
14
by: confusedfusion | last post by:
Not sure how many form submissions that have been lost over the years before I started but the company has a contact form that the required fields when validation fails the error message is going...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.