Make an instance of a class dynamic

J

john

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!
 
U

unmeshghosh

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
 
J

Jerry Coffin

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.
 
H

Howard

Please don't top-post. Replies belong at the end, or interspersed with what
they're responding to. [re-arranged]
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
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top