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:
rint()
{
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;
}
______________________________________
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:
{
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;
}
______________________________________