424,949 Members | 884 Online
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

# creating array with user defined datatype

 P: 10 HI all, let me explain the problem: In my simulation I am using two class: particle and Container. //////////// the outline of particle class///////////////////// Sphere.h----> Expand|Select|Wrap|Line Numbers #ifndef SPHERE_H_ #define SPHERE_H_       Class Particle    {           private:           double x;    // position of the particle         double Vx;  // velocity of the particle        double _R;   // radius of the particle           public:        particle()x(0.0),Vx(0.0),_R(0.0){} //default initialisation        particle(double radius)x(0.0),Vx(0.0),_R(radius){} // user defined one    //   and the rest of the code goes here.     };     #endif   In another class named "Container" i need to create an array of particle. Expand|Select|Wrap|Line Numbers  #ifndef CONTAINER_H_ #define CONTAINER_H_   #include using namespace std;  #include"sphere.h"      class Container     {         private:        vector HardSphere;     // and the rest of the program part goes here.      };   #endif   The problem is that in the "container" i want to have the array of particles with desired radius say 1.0. But i always find it invokes the default constructor which set radius to zero. Of course latter on i can run a do loop to set the radius to a desired value. But i was wondering if there is any other way to invoke the different particle constructor from the default one as shown in the particle class. Or using the do loop is the only way. thank you anupam Jun 13 '07 #1
7 Replies

 Expert 100+ P: 1,764 HI all, let me explain the problem: In my simulation I am using two class: particle and Container. //////////// the outline of particle class///////////////////// Sphere.h----> #ifndef SPHERE_H_ #define SPHERE_H_ Class Particle { private: double x; // position of the particle double Vx; // velocity of the particle double _R; // radius of the particle public: particle()x(0.0),Vx(0.0),_R(0.0){} //default initialisation particle(double radius)x(0.0),Vx(0.0),_R(radius){} // user defined one // and the rest of the code goes here. }; #endif In another class named "Container" i need to create an array of particle. #ifndef CONTAINER_H_ #define CONTAINER_H_ #include using namespace std; #include"sphere.h" class Container { private: vector HardSphere; // and the rest of the program part goes here. }; #endif The problem is that in the "container" i want to have the array of particles with desired radius say 1.0. But i always find it invokes the default constructor which set radius to zero. Of course latter on i can run a do loop to set the radius to a desired value. But i was wondering if there is any other way to invoke the different particle constructor from the default one as shown in the particle class. Or using the do loop is the only way. thank you anupam How are you creating particles? Savage Jun 13 '07 #2

 P: 10 How are you creating particles? Savage Savage, i could not follow you. Actually it is a part of a very long program. so i only showed the relevant part. Could you please explain me your question in details. just to inform that i access a particle in array as: for 2nd particle: HardSphere[1].Pos() // for position HardSphere[1].Vel() // for velocity HardSphere[1].Radius() // for radius. etc. the function Pos() and Vel() are defined in the particle class: which return the position and the velocity respectively. Jun 13 '07 #3

 Expert 100+ P: 1,764 Savage, i could not follow you. Actually it is a part of a very long program. so i only showed the relevant part. Could you please explain me your question in details. just to inform that i access a particle in array as: for 2nd particle: HardSphere[1].Pos() // for position HardSphere[1].Vel() // for velocity HardSphere[1].Radius() // for radius. etc. the function Pos() and Vel() are defined in the particle class: which return the position and the velocity respectively. Sorry,this is how u create array: vector HardSphere; particle will envoke defalut constructor unless you call overrided constructor(one that you have created).Your own constructor will be only called if u specify his argument,in this case that's double radius,so try this: vector HardSphere; Savage Jun 13 '07 #4

 P: 10 Sorry,this is how u create array: vector HardSphere; particle will envoke defalut constructor unless you call overrided constructor(one that you have created).Your own constructor will be only called if u specify his argument,in this case that's double radius,so try this: vector HardSphere; Savage Hi Savage, thank you for your suggestion. But Still i am getting compilation error. So let me write the program in full form. That might help to point out the error message. /////////////// Sphere.h/////////////////////////////////////////// Expand|Select|Wrap|Line Numbers //sphere.h sphere definition #ifndef SPHERE_H_ #define SPHERE_H_   #include using namespace std;   class particle { private:         double r;  // position        double v;  // velocity        double _R ;  // radius of a particle     public:         particle():r(0.0),v(0.0),_T(0.0),_R(0.5){}  //default constructor.         particle(const double x,  const double vx, const double Radius):r(x), v(vx)  {_R=Radius;}          particle(const double Radius):_R(Radius)                {r=0.0, v=0.0;}         ~particle(){};   //talking of postion and velocity  form           Vector & pos(){return r;}      Vector  pos()const{return r;}      Vector & velocity(){return v;}      Vector  velocity()const{return v;}        double Radius()const{return _R;}      friend ostream & operator<<(ostream & os, const particle & v); }; typedef particle Particle; #endif   /////////////////////////Sphere.cpp: contains the description of functions/////////// // definition sphere .h member functions Expand|Select|Wrap|Line Numbers #include using namespace std; #include"sphere.h"   // Now we write the particle class function       ostream &operator<<(ostream &os, const particle & v)      {            cout.setf(ios_base::fixed);            cout.precision(14);            cout.setf(ios_base::showpoint);              cout<<"Wellcome to particle state:\n";            cout<<"Radius of the particle: "< #include using namespace std; #include"sphere.h" int main() {     cout<<"Wellcome to stl particle state\n";     Particle x;     Particle y(4.5);         vector z;      ///LINE NO: 13 the compilation error      z.resize(5);     cout<

 Expert Mod 5K+ P: 9,197 vector z; ///LINE NO: 13 the compilation error You add Particle obects to the vector this way: Expand|Select|Wrap|Line Numbers vector  z; Particle  obj(4.5); z.push_back(obj);   Jun 13 '07 #6

 P: 10 You add Particle obects to the vector this way: Expand|Select|Wrap|Line Numbers vector  z; Particle  obj(4.5); z.push_back(obj);   Thanks for your response. It works perfectly. But i am curious to know whether you can call a user defined constructor in the creation of array like in the above program. But it appears it always call the default constructor. Do you think someway we can overload " new " operator to invoke user defined constructor in this case. I tried, but it didn't work. anyway, thanks for showing me the simple way out. Jun 14 '07 #7

 Expert Mod 5K+ P: 9,197 You need to specify the initial contents when the array is created: For an array of int you can: Expand|Select|Wrap|Line Numbers int arr[2] = { 1,2};   Here you specify the intial contents of the array elements between the braces. The compiler does not care if the int value is hard-coded or the result of a function call: Expand|Select|Wrap|Line Numbers int arr1[2] = { fx(2), fx(3)};   where: Expand|Select|Wrap|Line Numbers int fx(int in) {     return in; }   So do the same thing with an array of class obects: Expand|Select|Wrap|Line Numbers class Test {      private:         int data;      public:         Test(int); }; Test::Test(int in) : data(in) { }   Now the trick is to call the constructor as the array elements are created. Following the array of int example, you could: Expand|Select|Wrap|Line Numbers Test array[2] = {Test(5), Test(6)};   What this really does is call the class copy constructor. Test(5) creates a Test object. That object is copied to the array element and then it dies. Be sure you have the necessary copy constructors if you try this. Jun 14 '07 #8