I'm trying to learn the syntax for initializing objects in a comma separated
list. Below is an example program I wrote to learn how to do this (among
other things). While I understand how to initialize a primitive data type
with a comma separated list, e.g.
int a[3] = { 11, 23, 43 };
I know how to initialize the example object's "student1" and "student2"
below, e.g.
Student student1( "Isaac", "Newton" ), student2( "Albert", "Einstein" );
What I don't know is how to initialize an array of objects of class Student.
Suppose I had the following
int a1 = 11, a2 = 23, a3 = 43;
and I then decided to make my program work a bit differently and changed to
int a[3] = { 11, 23, 43 };
In this sense how do I go from
Student student1( "Isaac", "Newton" ), student2( "Albert", "Einstein" );
to
Student students[2] = ????
I tried what seemed like the logical thing to do, i,.e
Student students[2] = { { "Isaac", "Newton"}, { {"Albert", "Einstein"} };
That won't compile. Is there a way to create an array of objects with a
comma seperated list for initialization?
Thanks
Pmb
______________________________________
#include <iostream.h>
class Student{
public:
Student( char* = "", char* = "");
~Student();
void print();
static int getCount();
private:
char *firstName;
char *lastName;
static int count;
};
Student::Student( char *first, char *last )
{
firstName = new char[ strlen( first ) + 1 ];
strcpy( firstName, first );
lastName = new char[ strlen( last ) + 1 ];
strcpy( lastName, last );
++count;
}
Student::~Student()
{
delete firstName;
delete lastName;
--count;
}
void Student::print()
{
cout << "Student Name: " << firstName << " " << lastName << endl;
cout << endl;
}
int Student::getCount(){ return count;}
int Student::count = 0;
int main()
{
Student student1( "Isaac", "Newton" ), student2( "Albert", "Einstein" );
student1.print();
student2.print();
cout << "Number of students: " << Student::count << endl;
return 0;
}
______________________________________ 2 2700
"Pmb" <so*****@somewhere.com> wrote in message
news:9t********************@comcast.com... I'm trying to learn the syntax for initializing objects in a comma
separated list. Below is an example program I wrote to learn how to do this (among other things). While I understand how to initialize a primitive data type with a comma separated list, e.g.
int a[3] = { 11, 23, 43 };
I know how to initialize the example object's "student1" and "student2" below, e.g.
Student student1( "Isaac", "Newton" ), student2( "Albert",
"Einstein" ); What I don't know is how to initialize an array of objects of class
Student. Suppose I had the following
int a1 = 11, a2 = 23, a3 = 43;
and I then decided to make my program work a bit differently and changed
to int a[3] = { 11, 23, 43 };
In this sense how do I go from
Student student1( "Isaac", "Newton" ), student2( "Albert",
"Einstein" ); to
Student students[2] = ????
I tried what seemed like the logical thing to do, i,.e
Student students[2] = { { "Isaac", "Newton"}, { {"Albert", "Einstein"} };
That won't compile. Is there a way to create an array of objects with a comma seperated list for initialization?
Yes
Student students[2] = { Student( "Isaac", "Newton"), Student("Albert",
"Einstein") };
Curly brackets at the beginning and end, round brackets elsewhere.
john
> Student students[2] = { Student( "Isaac", "Newton"), Student("Albert", "Einstein") };
Curly brackets at the beginning and end, round brackets elsewhere.
john
Excellent! Muchus Gracias!
Pmb This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Craig Keightley |
last post by:
is it possible to compare acomma separated list aginst another
eg comma list 1 => 1,2,3,4,5
comma list 2 => 3,5
can you check that 3 is in both, and 5 is in both, therfore they match???
the...
|
by: N |
last post by:
Hi,
I would like to parse out each value that is seperated
by a comma in a field and use that value to join to another table.
What would be the easiest way to do so without having to
write a...
|
by: Craig Keightley |
last post by:
I have a mysql database with a list of companies who supply specific
products
tblSuppliers (simplified)
sID | sName | goodsRefs
1 | comp name | 1,2,3,4,5
2 | company 2 | 2,4
|
by: muzamil |
last post by:
To get rid of redundant data in a table, my cleint will be providing
something like this:
IDtokeep Ids to delete
34 24,35,49
12 14,178,1457
54 32,65,68
I have to write a...
|
by: Gary Smith |
last post by:
Hi, I've got a field that contains a list of rooms.
In most cases, this contains a single ID. However, under some
circumstances, the field may contain a list of two IDs which
are broken by a...
|
by: Serve Laurijssen |
last post by:
Is code like the following allowed? I am talking about the comma after the
last function in the initializer.
void f(void) {puts("f");}
void g(void) {puts("g");}
struct Funcs { void...
|
by: Shawn Odekirk |
last post by:
Some code I have inherited contains a macro like the following:
#define setState(state, newstate) \
(state >= newstate) ? \
(fprintf(stderr, "Illegal...
|
by: nehashri |
last post by:
i hv a database in access wid Asp as front end.
ven given a word in search command(of the front end) it shud go to a table in which each field has words serated by comma. each word shud b checked...
|
by: Kristi |
last post by:
I need to create a CL program that will take a PF, and create a tab
delimited file that has comma seperated column headings as the first
record. I know i can use cpytostmf/cpytoimpf to create the...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |