Getters And Setters C++ -


ok guys question here simple.. want construct getter , setter diffrent value type.. function overloading getters , setters.. tried this

#include <iostream>;  class vectors { public:     vectors() {};     vectors(int a, int b) {         x = a, y = b;     }     int getx() {         return x;     }     int gety() {         return y;     }     float getx() {         return (float)x;     }     float gety() {         return (float) y;     }      friend vectors operator+(const vectors& v1, const vectors& v2);      friend vectors operator/(const vectors& v1, const vectors& v2); protected:     int x, y; private:  };  vectors operator+(const vectors& v1, const vectors& v2) {     vectors brandnew;     brandnew.x = v1.x + v2.x;     brandnew.y = v1.y + v2.y;     return (brandnew); };  vectors operator/(const vectors& v1, const vectors& v2) {     vectors brandnew(v1.x / v2.x, v1.y/v2.y);     return brandnew; }  int main() {     vectors v1(2, 3);     vectors v2(4, 5);     vectors v3;     v3 = v1 + v2;     vectors v4 = v1 / v2;      std::cout << "vector 4 x : " << v4.getx() << std::endl;     std::cout << "vector 4 y : " << v4.gety() << std::endl;      std::cout << "vector v3 x : " << v3.getx() << std::endl;     std::cout << "vector v3 y : " << v3.getx() << std::endl; } 

but said cant function overloading , type diffrent return type..

there's no way overload function without changing arguments i'm aware of. need either change function name (call getxfloat() or something) or cast after calling function like:

float the_x_value = static_cast<float>(vec.getx()); 

i go second option.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -