-2
class department
{
    void max() ***HOW TO PASS ARRAY OF OBJECT HERE , WHAT PARAMETERS SHOULD I PASS***
    {
    } 
};

class B : public department {
};

int main()
{
    B a[10];
   
    // a.max(a,n);  ***HOW TO CALL THIS max FUNCTION***
    return 0;
} 

I want to pass the array of object a[10] to the max function. How do I call it and pass it?

I don't want to do it like this:

for(i = 0; i < n; i++)
{
    a[i].max
}
Ronny
  • 1
  • 3
  • Why don't you want to use a `for` loop? That seems like an acceptable solution. – jtbandes Oct 05 '21 at 21:02
  • You made `max` a member function of the class, so you have to call it on an instance. If you want to have a function that takes an array of `department`s then you have to write such a function – UnholySheep Oct 05 '21 at 21:03
  • The exact syntax `a.max(a,n);` is impossible, because `a` is not a class (but rather an array). – HolyBlackCat Oct 05 '21 at 21:03
  • Nitpick: `max` is implicitly a _private_ member function of `department`. The child class `B` therefore does not have access to `max`, so even given an instance of `B` to call `max` on, you will get an error. – Chris Oct 05 '21 at 22:04

2 Answers2

1

You implemented max() as a non-static method of department, so you need a department object to call it on, like each B object in your array, eg:

for(int i = 0; i < 10; ++i)
{
    a[i].max();
}

If this is not what you want, then max() needs to be taken out of department, or at least made to be static instead. Either way, you will have to change its input parameters to accept the array.

Try something more like this instead:

class department
{
public:
    static void max(department *depts, int count)
    {
        //...
    } 
};

class B : public department {
};

int main()
{
    B a[10];
    department::max(a, 10);
    return 0;
} 

Online Demo

Alternatively:

class department {
};

class B : public department {
};

void max(department *depts, int count)
{
    //...
} 

int main()
{
    B a[10];
    max(a, 10);
    return 0;
} 

Online Demo

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

How to pass object array to a function?

The parameter of a function cannot be an array in C++. A parameter can be a reference to an array. Alternatively, it is common to pass iterator pointing to an element of an array. Object pointer is an iterator for an array.

department::max() is a non-static member function. It has empty parameter list, so it accepts no arguments at all, except for the implicit class instance that is used as the left hand operand of a member access operator. Since this function accepts no reference to array parameter nor a pointer parameter, there's no way to pass an array as an argument.

Here is an example of a function that does accept a reference to an array as a parameter, and of how to call such function:

void max(B (&arr)[10]);

int main()
{
    B a[10];
    max(a);
}
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • yeah i got it , I was also asking what parameters should I have to pass in function max. – Ronny Oct 05 '21 at 21:13
  • @VishalOjha There is nothing that you can do to pass an array into your function `department::max()`. I have shown how to pass an array to a different `max` function that does accept a reference to an array as parameter. – eerorika Oct 05 '21 at 21:14