max_int number

Discussion in 'C++' started by ballurohit, Apr 17, 2012.

  1. ballurohit

    ballurohit New Member

    Joined:
    Nov 18, 2011
    Messages:
    43
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    retired
    Location:
    Gujarat
    #include <iostream.h>
    class max{
    public:
    void get(int[], int);
    int sh(int[], int);
    };
    void max::get(int *a, int b)
    {
    for (int i=0; i<b; i++)
    cin>> a;
    }
    void max::sh(int *a, int b)
    {
    int mx=a[0];
    for (int i=1; i,b; i++)
    if (a>mx)
    mx=a;
    return mx;
    }
    void main()
    {
    max j;
    int x[3];
    j.get(x,3);
    int m=j.sh(x,3);
    cout <<"\n MAX= "<<m;
    }

    NB:
    Line number 18 'max::sh(int *,int' cannot return a value
    Line number 25(a) 'declaration for m'
    Line number 25(b) 'cannot convert 'void' to 'max'
    Line number 26 ' Illegal structure operation
     
  2. ballurohit

    ballurohit New Member

    Joined:
    Nov 18, 2011
    Messages:
    43
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    retired
    Location:
    Gujarat
    Kindly advice.
     
  3. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    member function "sh" is prototyped as returning an int, but the definition is void. fix that and you *should* be okay.

    why not make your array as a private member of your class?

    Code:
    class max {
       public : max(int init_size = 3) {
                     a = new int[init_size];
                     asize = init_size;
               }
    
               void get();
               int sh() const;
    
               ~max() { delete [] a; }
                  ...
       private : int *a;
                 int asize;
    };
    
    void max::get() {
       ...
    }
    
    int max::sh() const {
       ...
    }
    ...
    when you instantiate an object, the constructor will use the default size of 3 elements or you can specify a size; either case will store the number of elements in the variable asize.

    Code:
    max test1,  /* default size of 3 */
        test2(10); // create 10 elements
    
     
  4. ballurohit

    ballurohit New Member

    Joined:
    Nov 18, 2011
    Messages:
    43
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    retired
    Location:
    Gujarat
    I do not really understand the meaning
    void max::get() & int max::sh()const
     
  5. ballurohit

    ballurohit New Member

    Joined:
    Nov 18, 2011
    Messages:
    43
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    retired
    Location:
    Gujarat
    This point has now been resolved. So nothing is required for any action.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice