By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,806 Members | 1,490 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,806 IT Pros & Developers. It's quick & easy.

Make an instance of a class dynamic

P: n/a
I am VERY new to C++ (1st semester) put not so new to coding and
programming (SQL, VB, etc). We are learning about classes.

I have a program that needs to call many instances (i think this is the
right term) of a class.

What I have been taught so far is to write to a class we would do
something like this in the main.cpp:

a1.setmanSalary(something);

of course all the appropiate coding is set in the class and class body
to handle it.

well, the program I need to write requires that the user does not know
how many entries they would have to enter (employees in this case).

So....In order for me to do this I would have to do:

a1.setmanSalary(something);
a2.setmanSalary(something);
a3.setmanSalary(something);
a4.setmanSalary(something);
etc.... all the way to some huge number

How can I set this up so the class instance
(aX.setmanSalary(something);) can be dynamically created so the program
can make the instances as it is needed? Let me know if you need more
info i will try to explain this better. I dont have to use a class for
this but I am choosing to so I can get a better understanding of it.

Thanks in advance!

Sep 16 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
You can use the concept of Linked List here to created dynamic objects.
Let's suppose you have class named xyz, then inside the public area
you declare a pointer of type xyz like class xyz *next. Then you can
create any new class using the dynamic allocation it next=new xyz(),
and thus you can create newer and newer classes each time user wants.
But however use of doubly linked list would facilitate the matter, but
if you don't know about the pointer and data structures, then you can
very well use arrays like Classname a1[100], if you know that the user
would not exceed hundred you can get your work done this way by
limiting the entered objects to hundreds. Feel free to write back for
help
john wrote:
I am VERY new to C++ (1st semester) put not so new to coding and
programming (SQL, VB, etc). We are learning about classes.

I have a program that needs to call many instances (i think this is the
right term) of a class.

What I have been taught so far is to write to a class we would do
something like this in the main.cpp:

a1.setmanSalary(something);

of course all the appropiate coding is set in the class and class body
to handle it.

well, the program I need to write requires that the user does not know
how many entries they would have to enter (employees in this case).

So....In order for me to do this I would have to do:

a1.setmanSalary(something);
a2.setmanSalary(something);
a3.setmanSalary(something);
a4.setmanSalary(something);
etc.... all the way to some huge number

How can I set this up so the class instance
(aX.setmanSalary(something);) can be dynamically created so the program
can make the instances as it is needed? Let me know if you need more
info i will try to explain this better. I dont have to use a class for
this but I am choosing to so I can get a better understanding of it.

Thanks in advance!
Sep 16 '06 #2

P: n/a
In article <11**********************@i3g2000cwc.googlegroups. com>,
jr*****@yahoo.com says...
I am VERY new to C++ (1st semester) put not so new to coding and
programming (SQL, VB, etc). We are learning about classes.

I have a program that needs to call many instances (i think this is the
right term) of a class.
Take a look at std::vector.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 16 '06 #3

P: n/a

<un*********@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...

Please don't top-post. Replies belong at the end, or interspersed with what
they're responding to. [re-arranged]
john wrote:
>I am VERY new to C++ (1st semester) put not so new to coding and
programming (SQL, VB, etc). We are learning about classes.

I have a program that needs to call many instances (i think this is the
right term) of a class.

What I have been taught so far is to write to a class we would do
something like this in the main.cpp:

a1.setmanSalary(something);

of course all the appropiate coding is set in the class and class body
to handle it.

well, the program I need to write requires that the user does not know
how many entries they would have to enter (employees in this case).

So....In order for me to do this I would have to do:

a1.setmanSalary(something);
a2.setmanSalary(something);
a3.setmanSalary(something);
a4.setmanSalary(something);
etc.... all the way to some huge number

How can I set this up so the class instance
(aX.setmanSalary(something);) can be dynamically created so the program
can make the instances as it is needed? Let me know if you need more
info i will try to explain this better. I dont have to use a class for
this but I am choosing to so I can get a better understanding of it.
You can use the concept of Linked List here to created dynamic objects.
Let's suppose you have class named xyz, then inside the public area
you declare a pointer of type xyz like class xyz *next. Then you can
create any new class using the dynamic allocation it next=new xyz(),
and thus you can create newer and newer classes each time user wants.
Linked lists are hardly needed here, and certainly not something for a
beginner to worry about.
But however use of doubly linked list would facilitate the matter, but
if you don't know about the pointer and data structures, then you can
very well use arrays like Classname a1[100], if you know that the user
would not exceed hundred you can get your work done this way by
limiting the entered objects to hundreds. Feel free to write back for
An array would be appropriate, yes. A vector would be preferable. (But it
seems to be a point of contention here as to whether vectors should be
introduced before or after raw arrays. Most schools still teach raw arrays
first, I believe.)

Look in your book, and your class notes so far. Your professor has likely
already covered any material you need to know, or has assigned reading which
covered it. You'll probably be looking for std::cin to get the information
from the user, and an array to handle storage of the data. Look those up in
your book or notes. You might also do a google search for something like
"c++ std::cin array simple example" and see if there's any examples out
there which would give you an idea of what a simple program of this sort
looks like.

-Howard

Sep 18 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.