473,500 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Make an instance of a class dynamic

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
3 2478
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
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

<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4294
by: yvan | last post by:
I am using Excel to save data. Everything works as i intend it to if no other instance of Excel is running. If another instance is running, it will do the job, but also close that instance. How...
6
7729
by: Andre Meyer | last post by:
Hi all I have been searching everywhere for this, but have not found a solution, yet. What I need is to create an object that is an instance of a class (NOT a class instance!) of which I only...
2
4549
by: Phil | last post by:
I am using a Pascal like language (Wealth-Lab) on W2K and call this server: class HelloWorld: _reg_clsid_ = "{4E797C6A-5969-402F-8101-9C95453CF8F6}" _reg_desc_ = "Python Test COM Server"...
37
2787
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
7
3946
by: ‘5ÛHH575-UAZWKVVP-7H2H48V3 | last post by:
(see end of message for example code) When an instance has a dynamically assigned instance method, deepcopy throws a TypeError with the message "TypeError: instancemethod expected at least 2...
1
14884
by: MKoleoso | last post by:
Problem: C#- Unable to create instance of a class implementing from an interface I have: namespace someNamespace { public __gc class SomeClass1 { }
9
28428
by: Bo Yang | last post by:
Hi, I know in java , we can use class.ForName("classname") to get an instance of the class 'classname' from a string , in python , how do I do that ? Thanks in advance !
2
1928
by: wilkinson.philip | last post by:
I have a javascript object which dynamically generates a table adding, deleting and moving rows as the user clicks on buttons or links. Problem is when I generate a table row and add the javascript...
7
1292
by: Andy B | last post by:
I have an instance of an object that needs to be accessed by all members of a page like page_load, button_click events and so on. Where in the codebehind would I put the creation of the object...
0
7136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7018
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6906
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7397
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4611
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3110
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.