473,396 Members | 1,892 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,396 software developers and data experts.

Initializer and comma seperated lists

Pmb
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;

}
______________________________________
Jul 22 '05 #1
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
Jul 22 '05 #2
Pmb
> Student students[2] = { Student( "Isaac", "Newton"), Student("Albert",
"Einstein") };

Curly brackets at the beginning and end, round brackets elsewhere.

john


Excellent! Muchus Gracias!

Pmb
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
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...
2
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...
11
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
3
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...
3
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...
12
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...
11
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...
2
nehashri
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...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
0
BarryA
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...
0
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...
0
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
jinu1996
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...
0
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
agi2029
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,...

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.