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

Initializer and comma seperated lists

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.