Simple C++ method question (overloading?)

  • C/C++
  • Thread starter zeion
  • Start date
  • Tags
    C++ Method
In summary, the conversation discusses the possibility of reusing a method that takes in 5 new variables and assigns 5 class variables to these values, with the ability to only change one variable instead of all 5. This can be achieved by either overloading the method with different parameter options or by using a pointer function to change the specific variable.
  • #1
zeion
466
1
Hi,

If I have a method that takes in say 5 new variables and assigns 5 class variables to these new values, and I only wanted to change one of these variables, is it possible to reuse this method and only have it take in say 1 new variable and change one class variable without having to write different methods for changing each individual variable?

ie.

class testClass
{
void changeFive (int first, second, third, fourth, fifth)
{
testClassFirst = first;
testClassSecond = second;
...
}

void changeOne (int first)
{
testClassFirst = first;
}
}
 
Technology news on Phys.org
  • #2
Two different methods can share the same name (overloading), but other than a common name, they are different methods.
 
Last edited:
  • #3
zeion said:
Hi,

If I have a method that takes in say 5 new variables and assigns 5 class variables to these new values, and I only wanted to change one of these variables, is it possible to reuse this method and only have it take in say 1 new variable and change one class variable without having to write different methods for changing each individual variable?

ie.
Code:
class testClass
{
void changeFive (int first, second, third, fourth, fifth)
{
   testClassFirst = first;
   testClassSecond = second;
   ...
}

void changeOne (int first)
{
   testClassFirst = first;
}
}
Assuming the 5 class variables are all int, this should work.

Code:
void changeVar(int *pl, int r)
{
       *pl = r;
}

You would call it as
Code:
changeVar(&testClassFirst, first);
changeVar(&testClassSecond, second);
etc

Though I am not sure how this helps you.
 
Last edited:
  • #4
You can write it like this

class testClass
{
void changeFive (int first, second, third, fourth, fifth)
{
testClassFirst = first;
testClassSecond = second;
...
}

void changeFive (int first)
{
testClassFirst = first;
}

void changeFive (int first, int second)
{
testClassFirst = first;
}

void changeOne (int first)
{
testClassFirst = first;
}

void changeOne (int first)
{
testClassFirst = first;
}

phiby's suggestion of using the following is where I would go
void changeVar(int *pl, int r)
{
*pl = r;
}
 
  • #5


Yes, it is possible to reuse the same method and only change one class variable by using method overloading in C++. Method overloading allows you to define multiple methods with the same name but different parameter lists. In this case, you can create a new method called "changeOne" and have it take in only one parameter, while the original "changeFive" method remains unchanged. This way, you can call either method depending on your needs, without having to write separate methods for each individual variable. This is a useful feature in C++ that allows for more efficient and organized coding.
 

Related to Simple C++ method question (overloading?)

1. What is method overloading in C++?

Method overloading in C++ refers to the ability to have multiple functions with the same name but different parameters. This allows for more flexibility and readability in code.

2. How does method overloading work in C++?

In C++, method overloading works by using the same function name for multiple functions, but with different parameters. When a function is called, the compiler will determine which version of the function to use based on the parameters passed in.

3. What is the purpose of method overloading in C++?

The purpose of method overloading in C++ is to improve code organization and readability. It allows for multiple functions to have the same name, making it easier to remember and use in different contexts.

4. Can methods with different return types be overloaded in C++?

No, methods with different return types cannot be overloaded in C++. Method overloading is based on the function name and parameters, not the return type.

5. When should I use method overloading in C++?

Method overloading can be useful in situations where you want to perform similar operations on different types of data. It can also be used to create more intuitive and readable code, especially when dealing with mathematical or string operations.

Similar threads

  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
5
Views
6K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top