An Array of objects

You will see, I have a Worrior class, and I am using an array in that definition to hold the stats of the object. In the main, I have an array to create my two objects. My problem is I do not know how to create an object in the array with default values.
Here is what I got so far ...
---------------------------- CPP Code ---------------------
// =====================
#include <iostream>
#include <iomanip>


class Worrior{
private :
int arrstats[3]; //stats[0]=Health, stats[1]=Strength, and stats[2]=Magic
public :
Worrior (): arrstats{500,200,300} {} //Structor with no-args
Worrior (int a, int b, int c) : arrstats{a,b,c}{} //Structor with three
// args
void get_val(Worrior &x);
void dsp(Worrior &x);};
/* In the class definition I am using an array to hold the Worrior Stats Data - it is of type int


int main(){
Worrior arrworriorobj[2]; //This array creates two Worrior Obj
// How Do I Create objs with values


return 0;}
//------------- Function Line ---------------
void Worrior ::get_val(Worrior &x){}
void Worrior ::dsp(Worrior &x){}
// ========== EOF ===========


thank you in advance ...
After some messing around I figured this out .... Now, I am posting my solution in case others may find it useful.

------------- Code --------------
int main(){
Worrior arrworriorobj[2]{Worrior(1500,50,100),Worrior(2000,200,55)}; //2 obj w/ init values
cout<<arrworriorobj[0].arrstats[0]<<"\n\n";
cout<<arrworriorobj[1].arrstats[0];
return 0;}

--------------- EOF ---------------
Yes, I temporarily made my class private: a public section so I can test this out and see what my values are.

Thoughts : I am sure this can be handled easier with pointers. I think, I need to refresh myself on pointers again - - it has been a long time (I'm just getting back to cpp programming).
Last edited on
Registered users can post here. Sign in or register to post.