Can anyone tell how to call a class function from a switch statement

example

number number::operator+(number)
                 {
                    inputs
                    }
switch (option)
       {
         case 1:
              number number::operator+(number);
              break;

IS THAT THE CORRECT WAY TO CALL THE ABOVE FUNCTION?

class number
{
private:

int Num1, Num2;
public:
number(int, int);
number operator+(number);
number operator-(number);
number operator/(number);
};

number number::operator+(number)
{
inputs
}

number number::operator-(number)
{
inputs
}

number number::operator/(number)
{
inputs
}

cout << "1 addition"<<endl;
cout << "2 subtraction"<<endl;
cout << "3 division"<<endl;

int option;

switch (option)
{
case 1:
number number::operator+(number)
break;

case 1:
number number::operator-(number)
break;

case 3:
number number::operator/(number)
break;

default:
cout<<"Invalid Option";
break;
}

I want to know is that the correct way to call the Class functions

Calling a class function is identical to calling a regular function.

Please learn to format your code to make it understandable and use CODE Tags.

Presumably if you had three objects of type number(number1, number2, number3), you would call the operator like this:

number3 = number1 + number2;

Get rid of the words "operator" and "number" and get rid of the "::" in the call.

so basicall what you are saying is that

number3 = number1 + number2; would call this function

#
number number::operator+(number)

{

inputs

}

so basicall what you are saying is that

number3 = number1 + number2; would call this function

#
number number::operator+(number)

{

inputs

}

number1 + number2

calls the function. Adding "number3 = " in front harnesses the return value and sticks it into an object you can use later. You'll want to write an assignment (=) operator too, and constructors, stuff like that.

You might as well just write the function rather than do this:

number number::operator+(number)
{
inputs
}

It's a short enough function to write presumably and it lessens the confusion. "inputs" doesn't make any sense to me here. And I would imagine it actually looks more like this:

number number::operator(const number& aNumber) const
{
    // code here
}

Google "C++ operator overloading tutorial" for some good examples.

yes you're correct but the question is, Whats the syntax to call the function in the switch statement?

cout << "1 addition"<<endl;
cout << "2 subtraction"<<endl;
cout << "3 division"<<endl;

int option;

switch (option)
{
case 1:
number number::operator+(number)
break;

case 1:
number number::operator-(number)
break;

case 3:
number number::operator/(number)
break;

default:
cout<<"Invalid Option";
break;
}

WOULD THE ABOVE SYNTAX BE THE CORRECT WAY?

I already answered that question earlier. No it would not be the correct syntax. Correct syntax would be this, assuming number1, number2, number3 are number objects:

number1 = number2 + number3;

Read my earlier post. In particular, this part of the post:

Get rid of the words "operator" and "number" and get rid of the "::" in the call.

Also note Walt P's post. Inside of a switch, outside of a switch, it doesn't matter. It's the same syntax. A call is a call and you don't need the word "number" in the call and you don't need the "::" in the call.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.