Hello all,

I am trying to figure out how to create new class objects when the user is asked if he/she would like to.

What I have is a program we have to write that takes a certain number of students and keeps track of their name, assignments, and the grade for each assignment.

I figure I would create a class called Students and for each student, there will be a class object..

Ex: class students
    {
     public:
             int studentID;
             string firstName
             string lastName
             vector <string> assignments<x>
             vector <int> grades<x>
    };

    students.student1
    students.student2
         or
    students.0168283 (student ID)

The question is, how do I dynamically create the class objects such as students.student1 and students.student2 depending on if the user wants to create additional ones.

Not really sure what you mean. Take this for example.

#include<iostream>
#include<string>

using namespace std;

class record
{
public:
       string name;
       string bankAccountNum;
       double accountBalance;
       void output();
};
       
int main()
{
    record customer1;
    customer1.name="David R. Sanders";
    customer1.bankAccountNum="324235634";
    customer1.accountBalance=12392.23;
    
    cout << customer1.name << endl;
    cout << customer1.bankAccountNum << endl;
    cout << customer1.accountBalance << endl;
    customer1.output();
    
    system("pause");
    
    return (0);   
}

Lets say I have a program that takes in customer information and outputs it to a screen.

What if I need the program to create a new class member (ex: customer2, customer3) when ever the the user wants to.

Are you saying I can create a class member as a vector?

class record
{
};

record vector <int> customer<1>


If so, what value type does the vector have to be since the class uses different value types (int, string, char,)?

Let's have a quick terminology check here to make sure we're on the same page:

an object is a collection of data and methods to access that data.
a class is a definition of an object.
a member is a data element within an object.

(I just whipped these definitions up; if someone wants to improve upon them, feel free)

In your example above: name, bankAccountNum, and accountBalance are members of the class (or object). As such, you're not going to create a new member on the fly.

If you need to, you can create new objects dynamically. That is, new instantiations of a particular class. A common way to do this is to use the new() operator, and reference your newly-made object via a pointer. If your application may need a large or unknown number of objects, then it makes sense to keep them in a vector, as vectors elements can be created dynamically as well.

And, when you refer to an object, you don't bother with the class name. So,

students.student1

would be just

student1

and what you're calling class members, would really be objects of that particular class.

Does this help?

Thank You, I think this helps.

So I can create new class objects by using the new() function and reference the newly made object via a pointer somehow.

I'm not sure how you would go about dong this.

class new(*newObjectName) ?


Or I can create the object as a vector?

class record
{

};

record vector<int> customer<30>;

Whether you need to introduce the use of vectors depends on how many of these new objects you might need. If it's just a few, it might be simpler to pre-allocate pointers and just use them as you need them.

If it could be a larger number, then vectors would be handy. Rather than my trying to explain their use, I'd recommend you start here.

You can simply store your objects in a vector, that is, you can do:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class record
{
public:
       string name;
       string bankAccountNum;
       double accountBalance;
       void output();
};
       
int main()
{
    //create a vector container of customers:
    vector<record> customers;

    record customer;

    //set the members of one customer.
    customer.name = "David R. Sanders";
    customer.bankAccountNum = "324235634";
    customer.accountBalance = 12392.23;
    
    customers.push_back(customer);  //add the customer to the vector.

    //set the members of another customer.
    customer.name = "John Doe";
    customer.bankAccountNum = "89745243";
    customer.accountBalance = 24542.67;
    
    customers.push_back(customer);  //add the customer to the vector.

    //iterator through the customers and print out their info:
    for(int i = 0; i < customers.size(); ++i) {
      cout << customer[i].name << endl;
      cout << customer[i].bankAccountNum << endl;
      cout << customer[i].accountBalance << endl;
      customer[i].output();
    };

    system("pause");
    
    return (0);   
}

Thank you so much. I'm still a little confused when it comes to understanding how the vector relates to the class object.

#
vector<record> customers;
#

#
record customer;

Isn't this doing the same thing, except one is a vector? And how does the customer object get put inside the vector? looks like it is just declaring a vector, and then declaring a class object with nothing to do with each other.

Sorry for the questions, its just everything I find on vectors doesn't explain how to use them with classes. And the stuff that I do find is like reading another language.

Well, a vector is basically a list. So vector<record> is a list of records, which you can use like this:

// make new vectors
vector<int> listOfNumbers;
vector<record> listOfRecords;

// add items to the vectors
listOfNumbers.push_back(5); // push 5 to the end of the list
record customer;
listOfCustomers.push_back(customer); // add customer to the end of the list

// access stuff
int firstNumber = listOfNumbers[0]; // get first number in list (list is zero-indexed)
record firstCustomer = listOfRecords[0];

You can have a look at this for a list of functions and sample code. On a further note, I recommend you to not use system("pause"); in your C++ code since it's not platform independent ;)

Cheers, xfbs

EDIT: added missing brackets

Edited 5 Years Ago by xfbs: n/a

I think line 7 is missing a semi-colon.

And, if you're using vectors, I'd recommend you avoid using brackets ([]) for referencing elements, to avoid confusion with arrays.

Good example otherwise, though.

Thanks for the hint, I added the missing bracket ;).

Actually I like using brackets for referencing elements in vectors... if the [] operator is there I feel like I should be using it ;)

This article has been dead for over six months. Start a new discussion instead.