How to initialize array objects?

This is a discussion on How to initialize array objects? within the C++ Programming forums, part of the General Programming Boards category; Hi, I created a constructor "Grade" to let object to pass name and score parameters. However, I don't know how ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    Arrow How to initialize array objects?

    Hi,

    I created a constructor "Grade" to let object to pass name and score parameters. However, I don't know how to do it.

    Could anyone help me?

    Thanks

    gogo

    #include <iostream>
    #include <string>
    using namespace std;

    class Grade
    {
    private:
    char name[30];
    float score;
    public:
    Grade(char student[], float score);
    Grade(void);
    int Compare(char s[]);
    void Read(void);
    };

    Grade::Grade(char student[], float score)
    {
    strcpy(name, student);
    Grade::score = score;
    }

    Grade::Grade()
    {
    *name = 0;
    score = 0.0;
    }

    Grade::Compare(char s[])
    {
    int a = 0;
    a = strcmp(name, s);
    if (a > 0)
    {
    return 1;
    }
    else
    {
    return 0;
    }
    }

    void Grade::Read(void)
    {
    cout << "Please input your name: ";
    cin >> name;
    cout << endl;
    cout << "Please enter your score: ";
    cin >> score;
    cout << endl;
    }

    int main()
    {
    Grade Students[5](("John", 78.3),("Sally", 86.5),("Bob", 58.9),("Donna", 98.3)); //How to correct it? I want to pass 4 records and let the 5th one to be input from console window.
    Read(); //How to call this function?
    return 0;
    }

    P.S.

    Also, I found that it is possible to decompile the "class" file of Java to get the source code. Is there any decompiler to decompile the object files created by C/C++?

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    70
    int main ()
    {

    Grade Students[5] =

    // Copy constructor is used here to copy these instances
    // into Students[] array (default copy constructor in your case)

    {
    Grade ("John", 78.3f),
    Grade ("Sally", 86.5f),
    Grade ("Bob", 58.9f),
    Grade ("Donna", 98.3f)
    };

    Students[4].Read ();

    return 0;
    }

  3. #3
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    You can initialize all the elements of array with the same value, but you can't input different values to the constructor when creating array. If you need different values in objects in array, you must input them with "normal" ways with loop or something.
    Code:
    // here's an example to initialize all objects in array with same value
    MyClass* MyPtr = new MyClass[100]("input value");
    Making error is human, but for messing things thoroughly it takes a computer

  4. #4
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    Golem, well I didn't remember that one
    Making error is human, but for messing things thoroughly it takes a computer

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    Thanks a lot. I forgot to use Copy constructor.

    Cheers

    gogo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of objects
    By adam_no6 in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2007, 08:59 AM
  2. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 03:45 PM
  3. Array of pointers to point objects
    By totalfreeloader in forum C++ Programming
    Replies: 6
    Last Post: 11-27-2003, 09:26 AM
  4. how to initialize a new array
    By Chiel in forum C++ Programming
    Replies: 4
    Last Post: 08-25-2003, 02:30 PM
  5. Adding objects to an array
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2001, 09:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21