Creating Objects + Array

This is a discussion on Creating Objects + Array within the C++ Programming forums, part of the General Programming Boards category; I have a class called "Student". It has members such as first name, last name, gpa...etc. In this program, i ...

  1. #1
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149

    Creating Objects + Array

    I have a class called "Student". It has members such as first name, last name, gpa...etc. In this program, i need to create and array, and be able to add objects into the array. Now, i know how to create an object before running the program:
    Code:
    Student Stud1("Williams","John", 4.0);
    however, i am not sure how to create an object whenever the user is using the program. Using a struct would be something like:
    Code:
    new Student;
    cin Student.First_Name;
    but i'm not sure how to do it with objects.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,317
    You might consider using a std::vector<Student> for an 'array' of Student objects.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    I'm not familiar with that. I tried creating an array:
    Code:
    Student Array1[10];
    but it came up with an error: 'Student' : no appropriate default constuctor available.
    "Student" is a derived class of the base class "Individual".
    Here is my code for the constructors:

    Inheritance help

    I'm not sure why it's not letting me create the array.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Because those classes don't have default constructors. That means that the compiler cannot create 10 objects for the array because it needs strings to call the constructor for each object.

    You could create a default constructor and an initialization function, then create the array and set those variables with the init function. You could also use a vector, which lets you use the constructors you have. However, that won't help much either because you would still have to give all 10 Students the same initial data.

    So I would create a default constructor (maybe by just giving default empty string values for your strings. Then set the data later.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a Spreadsheet using a multidimensional array
    By Apiatan in forum C++ Programming
    Replies: 1
    Last Post: 03-21-2009, 04:18 PM
  2. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Help with Array for objects
    By curwa in forum C++ Programming
    Replies: 5
    Last Post: 05-17-2003, 11:21 AM
  5. dynamic array of objects
    By mrukok in forum C++ Programming
    Replies: 7
    Last Post: 03-30-2003, 08:35 AM

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