Initializer and comma seperated lists

Discussion in 'C++' started by Pmb, May 26, 2004.

  1. Pmb

    Pmb Guest

    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;

    }
    ______________________________________
    Pmb, May 26, 2004
    #1
    1. Advertising

  2. "Pmb" <> wrote in message
    news:...
    > 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
    John Harrison, May 26, 2004
    #2
    1. Advertising

  3. Pmb

    Pmb Guest

    > Student students[2] = { Student( "Isaac", "Newton"), Student("Albert",
    > "Einstein") };
    >
    > Curly brackets at the beginning and end, round brackets elsewhere.
    >
    > john


    Excellent! Muchus Gracias!

    Pmb
    Pmb, May 26, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. steven
    Replies:
    2
    Views:
    599
    steven
    Oct 26, 2003
  2. Kamyk
    Replies:
    0
    Views:
    359
    Kamyk
    Oct 7, 2004
  3. Stan R.
    Replies:
    6
    Views:
    447
    Joe Kesselman
    Jun 5, 2006
  4. Serve Laurijssen

    comma at end of initializer allowed?

    Serve Laurijssen, Apr 20, 2004, in forum: C Programming
    Replies:
    12
    Views:
    738
    glen herrmannsfeldt
    May 13, 2004
  5. MSNEWS

    Splitting comma-seperated items

    MSNEWS, Apr 21, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    306
    Cowboy \(Gregory A. Beamer\)
    Apr 21, 2007
Loading...

Share This Page