accessing class methods

Hey everyone!
Suppose we have a class (CLASS A) having a number of data members and methods.
And then i create an array of objects.There is a member function in CLASS A,to which i have to pass this array of objects,and manipulate the data of the array.How will i access this function?From what i have learnt about c++,we can access a member function through an object.But what if i have an array of objects and i dont want to use a member function exclusively for an object,but for an array of objects of the same class.It makes sense,right?
thanks a ton to whoever who answers :)
Last edited on
Haha so in other words, how would you access an individual method from a single instance of a class in an array?

so you have something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class bucket{
     string message;
     string speak_oh_mighty_bucket();
     bucket();
};
bucket::bucket():message("I Haz A Bukkit"){}
string speak_oh_mighty_bucket(){
     return message;
}
//<....>


int main(){
     bucket yellerBukketz[5];
     //how do I have the first bucket tell us his extremely important message?
}


right?
hey ultifinitus!!


#include <iostream>

using namespace std;

class fruits
{
private:
int seeds;


public:
void inputseeds(fruits frr[])
{ int i;
for(i=0;i<5;i++)
frr[i].seeds=i;
}

};


int main()
{
fruits f[5];
inputseeds(f);
return 0;
}


see,i have an array of objects f[5],,of class fruits.Suppose i want to input the seeds of all fruits.One easy way would be to apply a for loop in main,,and call a function inputseeds,,which would input the seeds in every object INDIVIDUALLY. But there could be another way,when i pass the entire array as an argument,,and then i manipulate the data of the array.
I just want to know,,that in my main function,,is the second line correct? i am not using any object to call the function inputseeds.Do we need an object of a class to access a method of the same class? or would this code work,coz i am calling the function regardless of any object of fruits.I wrote this code in an online compiler,but it says In function 'int main()': 26:17: error: 'inputseeds' was not declared in this scope. *cries and hides under the table*
you need an object to call a class member function, or you can use static function following way.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

class fruits
{
private:
	int seeds;

public: 
	static void inputseeds(fruits frr[])
	{ 
		int i;
		for(i=0;i<5;i++)
		frr[i].seeds=i;
	}

};


int main()
{
	fruits f[5];
	
	fruits::inputseeds(f);
	
return 0;
}

Hey!
Thankyou very very much for that tip!!
It works perfectly!!Thanx a ton!!!! :D:D:D:D
By the way,is this the way programmers do it?U know,passing an array into a function and then manipulating its data,or do they use a for loop in main and then pass objects individually for individual manipulation of a function?

And thanx to ULTIFINITUS too !!:D:D
Thanx for the quick response!i love this site !!
but the log massange say this
|fatal error: iostream: No such file or directory|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

why!!
Topic archived. No new replies allowed.