How do I make multiple instances of a class? (C++)

I'm currently working on classes in C++ to prepare me for my foundation degree starting next year. I've gotten to the point where I understand class definitions, scope & constructors and how to make a single object. But if I want to make multiple instances of the class in such a way that a user might, for example, click a button and another instance will be created and the old one not overridden... How do I do that?


Example:

<#include <iostream>
#include <windows.h>

using namespace std;

class memory_card{
    public:
        int storage;
        float dimensions[3];
        memory_card(int capacity, float width, float height, float length){
            storage = capacity;
            dimensions[0] = width;
            dimensions[1] = height;
            dimensions[2] = length;
            printDetails();
        }
    private:
        void printDetails(){
            cout << "Size: " << storage << "KB" << endl;
            cout << "Dimensions: " << dimensions[0] << endl;
        }
};


int main(){
    while(1!=2){
        Sleep(300);
        memory_card card1(50, 44.4, 43.7, 55.5);

    }

return 0;
}>

I can declare a class, declare an object of that class with data, but how could I have multiple instances of that class going at once? Or have I already done that?

Thanks a bunch!
~pip

Like this...

int main(){
    while(1!=2){
        Sleep(300);
// like this.. just declare more variables of type memory_card.
        memory_card card1(50, 44.4, 43.7, 55.5);
        memory_card card2(50, 44.4, 43.7, 55.5);
        memory_card card3(50, 44.4, 43.7, 55.5);

// or this... it defines an array of empty class objects. Each will need to be initialized before being used.
        memory_card cards[10];

// or this (This is a vector class though, don't worry about this for now)
        vector<memory_card> cards;
        cards.push_back(new memorycard());

 }

So I can create the objects manually, define a bunch at once or do whatever that funky thing at the bottom is?

From thinking- Is the last option the "best" one for creating programs that need to be able to produce results outside of expected ranges? I'll use Dota2 as an example:

In dota, a wave of 4 creeps spawn every half-minute. Every 7 minutes, another creep is added to each wave. By the time the game reaches 3 hours (happened once) the game is struggling to cope with the amount of stuff going on.

To me, It feels like the second option would not be able to do this, too many objects linked to one variable? Whereas the 3rd one can hold as many instances as it wants?
And I've heard the "new" command is bad practice because it causes memory leaks?

I'm still new to C++, I've learned some basic programming principles in Python and JavaScript but C++ and OOP are still pretty new and mind-melting to me.

Thanks for helping me!
~pip

Correct, the last one I mentioned is best for your dota case.

A vector is a dynamic array library that comes in C++.

http://www.cplusplus.com/reference/vector/vector/?kw=vector

It is resizeable on the fly and is very fast.

You can add and remove objects from it as you go.

Here's a simple example.

vector<myClass> myObjects;

for (int t = 0; t < myObjects.size(); t++)
{
    myObjects[t].callMyFunction();   // call a member's function
    myObjects[t].myVar = t;          // set the members variable
}
1 Like