What Is Overloadding in C# Again
C++ Overloading (Operator and Part)
C++ allows you to specify more than one definition for a office name or an operator in the same telescopic, which is called office overloading and operator overloading respectively.
An overloaded declaration is a announcement that is declared with the same name equally a previously alleged declaration in the aforementioned scope, except that both declarations take different arguments and obviously different definition (implementation).
When you call an overloaded office or operator, the compiler determines the most appropriate definition to use, by comparing the argument types yous accept used to call the office or operator with the parameter types specified in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution.
Part Overloading in C++
You can have multiple definitions for the same office proper name in the aforementioned scope. The definition of the function must differ from each other past the types and/or the number of arguments in the argument listing. You cannot overload function declarations that differ simply past return type.
Following is the example where same function impress() is being used to print different data types −
#include <iostream> using namespace std; class printData { public: void print(int i) { cout << "Press int: " << i << endl; } void print(double f) { cout << "Printing float: " << f << endl; } void impress(char* c) { cout << "Printing character: " << c << endl; } }; int main(void) { printData pd; // Call impress to impress integer pd.print(five); // Call impress to print bladder pd.print(500.263); // Phone call impress to impress grapheme pd.impress("Hello C++"); render 0; } When the above lawmaking is compiled and executed, it produces the post-obit outcome −
Printing int: 5 Printing bladder: 500.263 Printing grapheme: Hello C++
Operators Overloading in C++
You can redefine or overload most of the built-in operators bachelor in C++. Thus, a programmer can use operators with user-defined types equally well.
Overloaded operators are functions with special names: the keyword "operator" followed by the symbol for the operator existence defined. Like any other function, an overloaded operator has a return type and a parameter list.
Box operator+(const Box&);
declares the add-on operator that tin can be used to add together two Box objects and returns terminal Box object. Well-nigh overloaded operators may exist defined as ordinary not-member functions or as course member functions. In case we define above function as non-member function of a form so we would have to laissez passer ii arguments for each operand as follows −
Box operator+(const Box&, const Box&);
Following is the case to show the concept of operator over loading using a fellow member function. Here an object is passed equally an argument whose backdrop volition be accessed using this object, the object which will telephone call this operator tin can be accessed using this operator as explained below −
#include <iostream> using namespace std; class Box { public: double getVolume(void) { return length * breadth * height; } void setLength( double len ) { length = len; } void setBreadth( double bre ) { breadth = bre; } void setHeight( double hei ) { height = hei; } // Overload + operator to add together two Box objects. Box operator+(const Box& b) { Box box; box.length = this->length + b.length; box.breadth = this->latitude + b.breadth; box.meridian = this->height + b.height; return box; } private: double length; // Length of a box double breadth; // Breadth of a box double top; // Acme of a box }; // Main function for the program int main() { Box Box1; // Declare Box1 of blazon Box Box Box2; // Declare Box2 of type Box Box Box3; // Declare Box3 of blazon Box double book = 0.0; // Store the book of a box here // box 1 specification Box1.setLength(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0); // box two specification Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.0); // book of box 1 book = Box1.getVolume(); cout << "Volume of Box1 : " << volume <<endl; // volume of box 2 book = Box2.getVolume(); cout << "Volume of Box2 : " << volume <<endl; // Add 2 object as follows: Box3 = Box1 + Box2; // book of box three volume = Box3.getVolume(); cout << "Volume of Box3 : " << volume <<endl; return 0; } When the to a higher place lawmaking is compiled and executed, information technology produces the following result −
Volume of Box1 : 210 Volume of Box2 : 1560 Book of Box3 : 5400
Overloadable/Non-overloadableOperators
Post-obit is the list of operators which tin can exist overloaded −
| + | - | * | / | % | ^ |
| & | | | ~ | ! | , | = |
| < | > | <= | >= | ++ | -- |
| << | >> | == | != | && | || |
| += | -= | /= | %= | ^= | &= |
| |= | *= | <<= | >>= | [] | () |
| -> | ->* | new | new [] | delete | delete [] |
Following is the list of operators, which can not be overloaded −
Operator Overloading Examples
Here are various operator overloading examples to help you lot in agreement the concept.
| Sr.No | Operators & Example |
|---|---|
| 1 | Unary Operators Overloading |
| 2 | Binary Operators Overloading |
| 3 | Relational Operators Overloading |
| four | Input/Output Operators Overloading |
| 5 | ++ and -- Operators Overloading |
| 6 | Assignment Operators Overloading |
| 7 | Office phone call () Operator Overloading |
| eight | Subscripting [] Operator Overloading |
| 9 | Class Member Access Operator -> Overloading |
Useful Video Courses
Video
Video
Video
Video
Video
Video
Source: https://www.tutorialspoint.com/cplusplus/cpp_overloading.htm
0 Response to "What Is Overloadding in C# Again"
Postar um comentário