logo

C++ Overbelasting (functie en operator)

Als we twee of meer leden maken met dezelfde naam, maar verschillend in aantal of type parameter, staat dit bekend als C++-overbelasting. In C++ kunnen we het volgende overbelasten:

  • methoden,
  • constructeurs, en
  • geïndexeerde eigenschappen

Dit komt omdat deze leden alleen parameters hebben.

Soorten overbelasting in C++ zijn:

  • Overbelasting van functies
  • Overbelasting van de machinist
C++ Overbelasting

C++-functie overbelasting

Functie-overbelasting wordt gedefinieerd als het proces waarbij twee of meer functies met dezelfde naam zijn, maar met verschillende parameters. Dit wordt in C++ functie-overbelasting genoemd. Bij overbelasting van functies wordt de functie opnieuw gedefinieerd door verschillende typen argumenten of een ander aantal argumenten te gebruiken. Alleen door deze verschillen kan de compiler onderscheid maken tussen de functies.

welke collectie in Java

De voordeel van Functie-overbelasting is dat het de leesbaarheid van het programma vergroot, omdat u geen verschillende namen voor dezelfde actie hoeft te gebruiken.

Voorbeeld van overbelasting van C++-functie

Laten we eens kijken naar het eenvoudige voorbeeld van overbelasting van functies, waarbij we het aantal argumenten van de methode add() wijzigen.

// programma voor overbelasting van functies wanneer het aantal argumenten varieert.

 #include using namespace std; class Cal { public: static int add(int a,int b){ return a + b; } static int add(int a, int b, int c) { return a + b + c; } }; int main(void) { Cal C; // class object declaration. cout&lt;<c.add(10, 20)<<endl; cout<<c.add(12, 20, 23); return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> 30 55 </pre> <p>Let&apos;s see the simple example when the type of the arguments vary.</p> <p>// Program of function overloading with different types of arguments.</p> <pre> #include using namespace std; int mul(int,int); float mul(float,int); int mul(int a,int b) { return a*b; } float mul(double x, int y) { return x*y; } int main() { int r1 = mul(6,7); float r2 = mul(0.2,3); std::cout &lt;&lt; &apos;r1 is : &apos; &lt;<r1<< std::endl; std::cout <<'r2 is : ' <<r2<< return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> r1 is : 42 r2 is : 0.6 </pre> <h2>Function Overloading and Ambiguity</h2> <p>When the compiler is unable to decide which function is to be invoked among the overloaded function, this situation is known as <strong>function overloading</strong> .</p> <p>When the compiler shows the ambiguity error, the compiler does not run the program.</p> <p> <strong>Causes of Function Overloading:</strong> </p> <ul> <li>Type Conversion.</li> <li>Function with default arguments.</li> <li>Function with pass by reference.</li> </ul> <img src="//techcodeview.com/img/c-tutorial/89/c-overloading-function-2.webp" alt="C++ Overloading"> <ul> <li>Type Conversion:</li> </ul> <p> <strong>Let&apos;s see a simple example.</strong> </p> <pre> #include using namespace std; void fun(int); void fun(float); void fun(int i) { std::cout &lt;&lt; &apos;Value of i is : &apos; &lt; <i<< std::endl; } void fun(float j) { std::cout << 'value of j is : ' <<j<< int main() fun(12); fun(1.2); return 0; < pre> <p>The above example shows an error &apos; <strong>call of overloaded &apos;fun(double)&apos; is ambiguous</strong> &apos;. The fun(10) will call the first function. The fun(1.2) calls the second function according to our prediction. But, this does not refer to any function as in C++, all the floating point constants are treated as double not as a float. If we replace float to double, the program works. Therefore, this is a type conversion from float to double.</p> <ul> <li>Function with Default Arguments</li> </ul> <p> <strong>Let&apos;s see a simple example.</strong> </p> <pre> #include using namespace std; void fun(int); void fun(int,int); void fun(int i) { std::cout &lt;&lt; &apos;Value of i is : &apos; &lt; <i<< std::endl; } void fun(int a,int b="9)" { std::cout << 'value of a is : ' < <a<< <b<< int main() fun(12); return 0; pre> <p>The above example shows an error &apos;call of overloaded &apos;fun(int)&apos; is ambiguous&apos;. The fun(int a, int b=9) can be called in two ways: first is by calling the function with one argument, i.e., fun(12) and another way is calling the function with two arguments, i.e., fun(4,5). The fun(int i) function is invoked with one argument. Therefore, the compiler could not be able to select among fun(int i) and fun(int a,int b=9).</p> <ul> <li>Function with pass by reference</li> </ul> <p>Let&apos;s see a simple example.</p> <pre> #include using namespace std; void fun(int); void fun(int &amp;); int main() { int a=10; fun(a); // error, which f()? return 0; } void fun(int x) { std::cout &lt;&lt; &apos;Value of x is : &apos; &lt;<x<< std::endl; } void fun(int &b) { std::cout << 'value of b is : ' < <b<< pre> <p>The above example shows an error &apos; <strong>call of overloaded &apos;fun(int&amp;)&apos; is ambiguous</strong> &apos;. The first function takes one integer argument and the second function takes a reference parameter as an argument. In this case, the compiler does not know which function is needed by the user as there is no syntactical difference between the fun(int) and fun(int &amp;).</p> <h2>C++ Operators Overloading</h2> <p>Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. Operator overloading is used to overload or redefines most of the operators available in C++. It is used to perform the operation on the user-defined data type. For example, C++ provides the ability to add the variables of the user-defined data type that is applied to the built-in data types.</p> <p>The advantage of Operators overloading is to perform different operations on the same operand.</p> <p> <strong>Operator that cannot be overloaded are as follows:</strong> </p> <ul> <li>Scope operator (::)</li> <li>Sizeof</li> <li>member selector(.)</li> <li>member pointer selector(*)</li> <li>ternary operator(?:) </li> </ul> <h2>Syntax of Operator Overloading</h2> <pre> return_type class_name : : operator op(argument_list) { // body of the function. } </pre> <p>Where the <strong>return type</strong> is the type of value returned by the function. </p><p> <strong>class_name</strong> is the name of the class.</p> <p> <strong>operator op</strong> is an operator function where op is the operator being overloaded, and the operator is the keyword.</p> <h2>Rules for Operator Overloading</h2> <ul> <li>Existing operators can only be overloaded, but the new operators cannot be overloaded.</li> <li>The overloaded operator contains atleast one operand of the user-defined data type.</li> <li>We cannot use friend function to overload certain operators. However, the member function can be used to overload those operators.</li> <li>When unary operators are overloaded through a member function take no explicit arguments, but, if they are overloaded by a friend function, takes one argument.</li> <li>When binary operators are overloaded through a member function takes one explicit argument, and if they are overloaded through a friend function takes two explicit arguments. </li> </ul> <h2>C++ Operators Overloading Example</h2> <p>Let&apos;s see the simple example of operator overloading in C++. In this example, void operator ++ () operator function is defined (inside Test class).</p> <p>// program to overload the unary operator ++.</p> <pre> #include using namespace std; class Test { private: int num; public: Test(): num(8){} void operator ++() { num = num+2; } void Print() { cout&lt;<'the count is: '<<num; } }; int main() { test tt; ++tt; calling of a function 'void operator ++()' tt.print(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> The Count is: 10 </pre> <p>Let&apos;s see a simple example of overloading the binary operators.</p> <p>// program to overload the binary operators.</p> <pre> #include using namespace std; class A { int x; public: A(){} A(int i) { x=i; } void operator+(A); void display(); }; void A :: operator+(A a) { int m = x+a.x; cout&lt;<'the result of the addition two objects is : '<<m; } int main() { a a1(5); a2(4); a1+a2; return 0; < pre> <p> <strong>Output:</strong> </p> <pre> The result of the addition of two objects is : 9 </pre></'the></pre></'the></pre></x<<></pre></i<<></pre></i<<></pre></r1<<></pre></c.add(10,>

Laten we het eenvoudige voorbeeld bekijken waarin het type argumenten varieert.

// Programma voor overbelasting van functies met verschillende soorten argumenten.

 #include using namespace std; int mul(int,int); float mul(float,int); int mul(int a,int b) { return a*b; } float mul(double x, int y) { return x*y; } int main() { int r1 = mul(6,7); float r2 = mul(0.2,3); std::cout &lt;&lt; &apos;r1 is : &apos; &lt;<r1<< std::endl; std::cout <<\'r2 is : \' <<r2<< return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> r1 is : 42 r2 is : 0.6 </pre> <h2>Function Overloading and Ambiguity</h2> <p>When the compiler is unable to decide which function is to be invoked among the overloaded function, this situation is known as <strong>function overloading</strong> .</p> <p>When the compiler shows the ambiguity error, the compiler does not run the program.</p> <p> <strong>Causes of Function Overloading:</strong> </p> <ul> <li>Type Conversion.</li> <li>Function with default arguments.</li> <li>Function with pass by reference.</li> </ul> <img src="//techcodeview.com/img/c-tutorial/89/c-overloading-function-2.webp" alt="C++ Overloading"> <ul> <li>Type Conversion:</li> </ul> <p> <strong>Let&apos;s see a simple example.</strong> </p> <pre> #include using namespace std; void fun(int); void fun(float); void fun(int i) { std::cout &lt;&lt; &apos;Value of i is : &apos; &lt; <i<< std::endl; } void fun(float j) { std::cout << \'value of j is : \' <<j<< int main() fun(12); fun(1.2); return 0; < pre> <p>The above example shows an error &apos; <strong>call of overloaded &apos;fun(double)&apos; is ambiguous</strong> &apos;. The fun(10) will call the first function. The fun(1.2) calls the second function according to our prediction. But, this does not refer to any function as in C++, all the floating point constants are treated as double not as a float. If we replace float to double, the program works. Therefore, this is a type conversion from float to double.</p> <ul> <li>Function with Default Arguments</li> </ul> <p> <strong>Let&apos;s see a simple example.</strong> </p> <pre> #include using namespace std; void fun(int); void fun(int,int); void fun(int i) { std::cout &lt;&lt; &apos;Value of i is : &apos; &lt; <i<< std::endl; } void fun(int a,int b="9)" { std::cout << \'value of a is : \' < <a<< <b<< int main() fun(12); return 0; pre> <p>The above example shows an error &apos;call of overloaded &apos;fun(int)&apos; is ambiguous&apos;. The fun(int a, int b=9) can be called in two ways: first is by calling the function with one argument, i.e., fun(12) and another way is calling the function with two arguments, i.e., fun(4,5). The fun(int i) function is invoked with one argument. Therefore, the compiler could not be able to select among fun(int i) and fun(int a,int b=9).</p> <ul> <li>Function with pass by reference</li> </ul> <p>Let&apos;s see a simple example.</p> <pre> #include using namespace std; void fun(int); void fun(int &amp;); int main() { int a=10; fun(a); // error, which f()? return 0; } void fun(int x) { std::cout &lt;&lt; &apos;Value of x is : &apos; &lt;<x<< std::endl; } void fun(int &b) { std::cout << \'value of b is : \' < <b<< pre> <p>The above example shows an error &apos; <strong>call of overloaded &apos;fun(int&amp;)&apos; is ambiguous</strong> &apos;. The first function takes one integer argument and the second function takes a reference parameter as an argument. In this case, the compiler does not know which function is needed by the user as there is no syntactical difference between the fun(int) and fun(int &amp;).</p> <h2>C++ Operators Overloading</h2> <p>Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. Operator overloading is used to overload or redefines most of the operators available in C++. It is used to perform the operation on the user-defined data type. For example, C++ provides the ability to add the variables of the user-defined data type that is applied to the built-in data types.</p> <p>The advantage of Operators overloading is to perform different operations on the same operand.</p> <p> <strong>Operator that cannot be overloaded are as follows:</strong> </p> <ul> <li>Scope operator (::)</li> <li>Sizeof</li> <li>member selector(.)</li> <li>member pointer selector(*)</li> <li>ternary operator(?:) </li> </ul> <h2>Syntax of Operator Overloading</h2> <pre> return_type class_name : : operator op(argument_list) { // body of the function. } </pre> <p>Where the <strong>return type</strong> is the type of value returned by the function. </p><p> <strong>class_name</strong> is the name of the class.</p> <p> <strong>operator op</strong> is an operator function where op is the operator being overloaded, and the operator is the keyword.</p> <h2>Rules for Operator Overloading</h2> <ul> <li>Existing operators can only be overloaded, but the new operators cannot be overloaded.</li> <li>The overloaded operator contains atleast one operand of the user-defined data type.</li> <li>We cannot use friend function to overload certain operators. However, the member function can be used to overload those operators.</li> <li>When unary operators are overloaded through a member function take no explicit arguments, but, if they are overloaded by a friend function, takes one argument.</li> <li>When binary operators are overloaded through a member function takes one explicit argument, and if they are overloaded through a friend function takes two explicit arguments. </li> </ul> <h2>C++ Operators Overloading Example</h2> <p>Let&apos;s see the simple example of operator overloading in C++. In this example, void operator ++ () operator function is defined (inside Test class).</p> <p>// program to overload the unary operator ++.</p> <pre> #include using namespace std; class Test { private: int num; public: Test(): num(8){} void operator ++() { num = num+2; } void Print() { cout&lt;<\'the count is: \'<<num; } }; int main() { test tt; ++tt; calling of a function \'void operator ++()\' tt.print(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> The Count is: 10 </pre> <p>Let&apos;s see a simple example of overloading the binary operators.</p> <p>// program to overload the binary operators.</p> <pre> #include using namespace std; class A { int x; public: A(){} A(int i) { x=i; } void operator+(A); void display(); }; void A :: operator+(A a) { int m = x+a.x; cout&lt;<\'the result of the addition two objects is : \'<<m; } int main() { a a1(5); a2(4); a1+a2; return 0; < pre> <p> <strong>Output:</strong> </p> <pre> The result of the addition of two objects is : 9 </pre></\'the></pre></\'the></pre></x<<></pre></i<<></pre></i<<></pre></r1<<>

Functie-overbelasting en dubbelzinnigheid

Wanneer de compiler niet kan beslissen welke functie moet worden aangeroepen uit de overbelaste functie, staat deze situatie bekend als overbelasting van functies .

Wanneer de compiler de dubbelzinnigheidsfout vertoont, voert de compiler het programma niet uit.

Oorzaken van functieoverbelasting:

  • Type conversie.
  • Functie met standaardargumenten.
  • Functie met pass-by-referentie.
C++ Overbelasting
  • Typeconversie:

Laten we een eenvoudig voorbeeld bekijken.

 #include using namespace std; void fun(int); void fun(float); void fun(int i) { std::cout &lt;&lt; &apos;Value of i is : &apos; &lt; <i<< std::endl; } void fun(float j) { std::cout << \'value of j is : \' <<j<< int main() fun(12); fun(1.2); return 0; < pre> <p>The above example shows an error &apos; <strong>call of overloaded &apos;fun(double)&apos; is ambiguous</strong> &apos;. The fun(10) will call the first function. The fun(1.2) calls the second function according to our prediction. But, this does not refer to any function as in C++, all the floating point constants are treated as double not as a float. If we replace float to double, the program works. Therefore, this is a type conversion from float to double.</p> <ul> <li>Function with Default Arguments</li> </ul> <p> <strong>Let&apos;s see a simple example.</strong> </p> <pre> #include using namespace std; void fun(int); void fun(int,int); void fun(int i) { std::cout &lt;&lt; &apos;Value of i is : &apos; &lt; <i<< std::endl; } void fun(int a,int b="9)" { std::cout << \'value of a is : \' < <a<< <b<< int main() fun(12); return 0; pre> <p>The above example shows an error &apos;call of overloaded &apos;fun(int)&apos; is ambiguous&apos;. The fun(int a, int b=9) can be called in two ways: first is by calling the function with one argument, i.e., fun(12) and another way is calling the function with two arguments, i.e., fun(4,5). The fun(int i) function is invoked with one argument. Therefore, the compiler could not be able to select among fun(int i) and fun(int a,int b=9).</p> <ul> <li>Function with pass by reference</li> </ul> <p>Let&apos;s see a simple example.</p> <pre> #include using namespace std; void fun(int); void fun(int &amp;); int main() { int a=10; fun(a); // error, which f()? return 0; } void fun(int x) { std::cout &lt;&lt; &apos;Value of x is : &apos; &lt;<x<< std::endl; } void fun(int &b) { std::cout << \'value of b is : \' < <b<< pre> <p>The above example shows an error &apos; <strong>call of overloaded &apos;fun(int&amp;)&apos; is ambiguous</strong> &apos;. The first function takes one integer argument and the second function takes a reference parameter as an argument. In this case, the compiler does not know which function is needed by the user as there is no syntactical difference between the fun(int) and fun(int &amp;).</p> <h2>C++ Operators Overloading</h2> <p>Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. Operator overloading is used to overload or redefines most of the operators available in C++. It is used to perform the operation on the user-defined data type. For example, C++ provides the ability to add the variables of the user-defined data type that is applied to the built-in data types.</p> <p>The advantage of Operators overloading is to perform different operations on the same operand.</p> <p> <strong>Operator that cannot be overloaded are as follows:</strong> </p> <ul> <li>Scope operator (::)</li> <li>Sizeof</li> <li>member selector(.)</li> <li>member pointer selector(*)</li> <li>ternary operator(?:) </li> </ul> <h2>Syntax of Operator Overloading</h2> <pre> return_type class_name : : operator op(argument_list) { // body of the function. } </pre> <p>Where the <strong>return type</strong> is the type of value returned by the function. </p><p> <strong>class_name</strong> is the name of the class.</p> <p> <strong>operator op</strong> is an operator function where op is the operator being overloaded, and the operator is the keyword.</p> <h2>Rules for Operator Overloading</h2> <ul> <li>Existing operators can only be overloaded, but the new operators cannot be overloaded.</li> <li>The overloaded operator contains atleast one operand of the user-defined data type.</li> <li>We cannot use friend function to overload certain operators. However, the member function can be used to overload those operators.</li> <li>When unary operators are overloaded through a member function take no explicit arguments, but, if they are overloaded by a friend function, takes one argument.</li> <li>When binary operators are overloaded through a member function takes one explicit argument, and if they are overloaded through a friend function takes two explicit arguments. </li> </ul> <h2>C++ Operators Overloading Example</h2> <p>Let&apos;s see the simple example of operator overloading in C++. In this example, void operator ++ () operator function is defined (inside Test class).</p> <p>// program to overload the unary operator ++.</p> <pre> #include using namespace std; class Test { private: int num; public: Test(): num(8){} void operator ++() { num = num+2; } void Print() { cout&lt;<\'the count is: \'<<num; } }; int main() { test tt; ++tt; calling of a function \'void operator ++()\' tt.print(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> The Count is: 10 </pre> <p>Let&apos;s see a simple example of overloading the binary operators.</p> <p>// program to overload the binary operators.</p> <pre> #include using namespace std; class A { int x; public: A(){} A(int i) { x=i; } void operator+(A); void display(); }; void A :: operator+(A a) { int m = x+a.x; cout&lt;<\'the result of the addition two objects is : \'<<m; } int main() { a a1(5); a2(4); a1+a2; return 0; < pre> <p> <strong>Output:</strong> </p> <pre> The result of the addition of two objects is : 9 </pre></\'the></pre></\'the></pre></x<<></pre></i<<></pre></i<<>

Waar de soort retour is het type waarde dat door de functie wordt geretourneerd.

naam van de klasse is de naam van de klasse.

operator op is een operatorfunctie waarbij op de operator is die overbelast wordt, en de operator het sleutelwoord is.

Regels voor overbelasting door operators

  • Bestaande operators kunnen alleen overbelast worden, maar de nieuwe operators kunnen niet overbelast worden.
  • De overbelaste operator bevat ten minste één operand van het door de gebruiker gedefinieerde gegevenstype.
  • We kunnen de vriendenfunctie niet gebruiken om bepaalde operators te overbelasten. De ledenfunctie kan echter worden gebruikt om deze operators te overbelasten.
  • Wanneer unaire operatoren overbelast worden via een lidfunctie, nemen ze geen expliciete argumenten, maar als ze overbelast worden door een vriendfunctie, nemen ze één argument.
  • Wanneer binaire operatoren overbelast worden via een lidfunctie, is er één expliciet argument nodig, en als ze overbelast worden via een vriendfunctie, zijn er twee expliciete argumenten nodig.

Voorbeeld van overbelasting van C++-operators

Laten we eens kijken naar het eenvoudige voorbeeld van overbelasting van operators in C++. In dit voorbeeld is de operatorfunctie void operator ++ () gedefinieerd (binnen de testklasse).

// programma om de unaire operator ++ te overbelasten.

 #include using namespace std; class Test { private: int num; public: Test(): num(8){} void operator ++() { num = num+2; } void Print() { cout&lt;<\\'the count is: \\'<<num; } }; int main() { test tt; ++tt; calling of a function \\'void operator ++()\\' tt.print(); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> The Count is: 10 </pre> <p>Let&apos;s see a simple example of overloading the binary operators.</p> <p>// program to overload the binary operators.</p> <pre> #include using namespace std; class A { int x; public: A(){} A(int i) { x=i; } void operator+(A); void display(); }; void A :: operator+(A a) { int m = x+a.x; cout&lt;<\\'the result of the addition two objects is : \\'<<m; } int main() { a a1(5); a2(4); a1+a2; return 0; < pre> <p> <strong>Output:</strong> </p> <pre> The result of the addition of two objects is : 9 </pre></\\'the></pre></\\'the>

Laten we een eenvoudig voorbeeld bekijken van het overbelasten van de binaire operatoren.

// programma om de binaire operatoren te overbelasten.

 #include using namespace std; class A { int x; public: A(){} A(int i) { x=i; } void operator+(A); void display(); }; void A :: operator+(A a) { int m = x+a.x; cout&lt;<\\'the result of the addition two objects is : \\'<<m; } int main() { a a1(5); a2(4); a1+a2; return 0; < pre> <p> <strong>Output:</strong> </p> <pre> The result of the addition of two objects is : 9 </pre></\\'the>