How do you input size into a Class Function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lolcheelol
    New Member
    • Feb 2010
    • 25

    How do you input size into a Class Function?

    Code:
    class Class1{
    private:
    int size;
    int x1[40];
    
    public:
    void sayit(int size);
    void sayit(int size);
    void doit();
    void writeit(); 
    }; // cs1
    
    void Class1::sayit(int size){
    cout <<"\n\n\t Random Numbers\n\n";
    }//sayit
    
    void Class1::sayit(int size){
    cout <<"\n\n\t Random Numbers\n\n";
    }//sayit
    
    void Class1::doit(){
    for(int i=0; i<size; i++) x1[i] = random(999); 
    }//doit
    
    void Class1::writeit(){
    cout << "\n\n\t Random Numbers...\n";
    for(int i=0; i<size; i++) cout << "\t" << x1[i]; 
    }//writeit
    
    int main(){
    clrscr();
    randomize();
    Class1 c1,c2;
    c1.sayit(20); <----- I want the number of random numbers to be this. 
    c1.doit();
    c1.writeit();
    getch();
    c2.sayit(40); <----- Number of random numbers to be this.
    c2.doit();
    c2.writeit();
    I'm trying to get the random numbers to output different sizes. I'm not really sure how to do it so if you could look at it and see what I'm doing wrong. I know in a regular function you would just put the number in there, so any help is great.
  • mush1578
    New Member
    • Feb 2010
    • 15

    #2
    you ceart the object of class1 in main funcation then call all the funcation on that object

    Comment

    • manontheedge
      New Member
      • Oct 2006
      • 175

      #3
      I believe what you're looking for is a constructor. A constructor is where you initialize your private member variables - in your case that would be 'size' and 'x1'.


      here's basically how your class declaration would look ...

      Code:
      Class1
      {
      private:
          int m_Size; <--- using "m_" for class variables is pretty standard
          int* m_X1;  <--- making this a pointer, in case you want something other than 40 at any point
      
      public:
          Class1( int val );  <-- here's the constructor, getting one value passed in
      
      ...
      ...
      ...
      
      };


      the code for the constructor ...

      Code:
      Class1::Class1( int val ) 
      {
          m_Size = val;  <--- the value you passed in will now be stored in your variable
          m_X1 = new int[40];  <--- this creates your array ( m_X1[40] )
      }
      then the code for your main would look like ...

      Code:
      int main()
      { 
          clrscr(); 
          randomize(); 
          Class1 c1,c2; 
          c1(20); <----- I want the number of random numbers to be this.  
          c1.doit(); 
          c1.writeit(); 
          getch(); 
          c2(40); <----- Number of random numbers to be this. 
          c2.doit(); 
          c2.writeit();
      }

      like I said, I believe that's what you're looking for. If so, there are sites out there that explain constructors and destructors ( one of them being cplusplus.com ). They are very important and useful when using classes.

      Hope this helped.

      Comment

      Working...