Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 12
Function overloading is 2 or more function having same name but differ in argument Eg: Func (int) { //Do SOMETHING } Func (int,float) { //Do SOMETHING } Operator overloading means u can perform different operation on same opprator Eg: Sum =1+2;//adding 2 numbers Str3="hello"+"hi";//adding 2 string Obj1=obj1+obj2;//adding 2 object
21st Jul 2019, 12:09 PM
Vijay(v-star🌟)
Vijay(v-star🌟) - avatar
+ 7
~ swim ~ At class level, parameters can be of primitive type as one of them will always be of the class type After some tests, I found out one of the operators that must be at class level: cast operator :)
21st Jul 2019, 10:58 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 5
Function overloading is a feature in C++ where two or more functions can have the same name but different parameters. #include <iostream> using namespace std; void print(int i) { cout << " Here is int " << i << endl; } void print(double f) { cout << " Here is float " << f << endl; } void print(char const *c) { cout << " Here is char* " << c << endl; } int main() { print(10); print(10.10); print("ten"); return 0; } Operator Overloading In C++, we can make operators to work for user defined classes. This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. For example, we can overload an operator ‘+’ in a class like String so that we can concatenate two strings by just using +. Other example classes where arithmetic operators may be overloaded are Complex Number, Fractional Number, Big Integer, etc. A simple and complete example filter_none edit play_arrow brightness_4 #include<iostream> using namespace std; class Complex { private: int real, imag; public: Complex(int r = 0, int i =0) {real = r; imag = i;} // This is automatically called when '+' is used with // between two Complex objects Complex operator + (Complex const &obj) { Complex res; res.real = real + obj.real; res.imag = imag + obj.imag; return res; } void print() { cout << real << " + i" << imag << endl; } }; int main() { Complex c1(10, 5), c2(2, 4); Complex c3 = c1 + c2; // An example call to "operator+" c3.print(); } Output: 12 + i9
21st Jul 2019, 8:01 AM
ArSlan Mehmood
+ 1
Function overloading is used to overload a functions and operator overloading is used to overload the operators. And the both are done within the class block 😉😉
7th Aug 2019, 1:53 PM
Nithish Nithish
Nithish Nithish - avatar