473,320 Members | 2,003 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,320 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 1906
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.