Thursday, 22 August 2013

Is there a way to use an existing function as a part of a class? If yes, how?

Is there a way to use an existing function as a part of a class? If yes, how?

I have a function that I want to use in different classes. Is this possible?
Example:
int getNumber()
{
// do something here that will use some values like:
int number = num * pi;
return number;
}
class Human
{
int num;
// other member
int getNumber(); // same as above
}
class Robot
{
int num;
// other member
int getNumber(); // same as above
}
The getNumber() function can be short as return num; or long depends on
the computation that will happen inside the function.
The num value inside the getNumber() is the num member of both Human and
Robot class. Although, these classes do not have the parent-child or
friend relationship.

No comments:

Post a Comment