You can define a function outside of a class, and then simply call it from any other function, whether in a class or not. You don't need to re-define the function 100 times.
100 very similar classes implies a design problem. I would post more words here to see if someone can give you a better approach. Its doable, depending on what you want, in a number of ways, but you are throwing up all kinds of red flags that something isnt quite right.
usingnamespace std;
int MyFunc(int a, int b)
{
int c;
c = a + b;
return c;
}
class MyClass1 {
// ...
public:
int aa, bb;
int cc;
MyClass1(int, int);
int sum(void);
// ...
};
MyClass1::MyClass1(int a0, int b0)
{
aa = a0;
bb = b0;
cc = 0;
}
int MyClass1::sum(void)
{
cc = MyFunc(aa, bb);
}
int main()
{
MyClass1 mc1(2,3);
mc1.sum();
cout << "Print Test " << endl;
cout << "Sum= " << mc1.cc << endl;
return 0;
}
@keskiverto, @jonnin
Sorry about the number 100. I wrote it to emphasize my aim. But it will be definitely more than one. The function is coordinate transformation. It will transform coordinates in a class and transform displacements, forces, moment forces in other corresponding classes. It may increase.
Turn on compiler warnings. Your sum function requires an int to be returned, but you don't return anything. But beyond that, it #worksforme, because I see 5 printed. But it's undefined behavior, so anything could happen.
Also, small tip: Next time, if you post a compilable example, also post the #includes, as it makes it so we don't have to type them ourselves.
It may increase.
design as if it will, then.
you may want to use inheritance to re-use some code blocks, for example ... put ALL the reused stuff in one, and pull it into the others.... same idea just design it in by intent going forward.
@ Ganado:
It works now. Thank you. This is my first post, I will.
@jonnin:
I don't want to use classes because it will be multi multi inheritance which freaks me out. And there will be variable adjustments. May be I am thinking function oriented still because I just switched to object oriented.