-
overloading for newbies
Hello. I'm new to C++ but I have some knowledge of C. You may laugh because I don't know what overloading means. I've been doing some reading and I have the vauge idea that overloading means you have two different functions say, but with the same name. Am I in the right direction? Anyway, I need to know how to overload | and & to compute a union of sets and an intersection of sets. Please help.
-
There is overloading functions and overloading operators. Overloading functions is giving two functions the same name, but having the input parameters be different. For example:
Code:
void DisplayData(int integerData)
{
cout << integerData << endl;
}
void DisplayData(float floatData)
{
cout << floatData << endl;
}
I think what you want is operator overloading. This allows you to give a new definition to an operator (like | or & or * or + or =, etc.) based on what types are used with the operator. Usually, this is used for a specific class. So if you have a class that represents a set, you could overload the operator | to combine two instances of the set. Do you already have a class that represents a set? That would be a good start.
-
Ok I understand now. Thanks. But how would you go about overloading an operator? Would it be possible for you to give me a short example? Or a link to a good explanation/example?