Initializer and comma seperated lists

P

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;

}
______________________________________
 
J

John Harrison

Pmb said:
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
 
P

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
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,132
Latest member
TeresaWcq1
Top