Junwei WANG
Junwei WANG

Reputation: 239

About overload in C++

I am following an online course now. I have a doubt in the lecture. They give an example of overloading, but I don't think it is correct. So I need your help.

Class Location {
  private: int x,y;
  public: 
    void valueX (int val) {x = val;}
    int valueX() {return x;}
}

Are the two functions named valueX but with different return types an example of overloading? Please help me! Thank you in advance.

Upvotes: 1

Views: 79

Answers (3)

gazza
gazza

Reputation: 24

yes, the first function accept an integer value and assign x to this value integer, the scecond don't accept a value and return an integer; try to make cast of type of x or change his type and try to pass to second function another type, not integer, to see how the function work; you can use a template type for replace two function with only one

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 60037

One takes a parameter. The other does not. That is why they are overloaded

Upvotes: 0

Yes, this is overloading. Overloading is when you have two functions with the same name in the same scope but with different parameter types. The return type might also be different, but it doesn't have to be.

Upvotes: 3

Related Questions