![]() |
In C++, the multiplication operator is a binary operator that is used to find the product of two numeric values. In this article, we are going to learn how to overload the multiplication operator for a class in C++. Overloading Multiplication Operator in C++C++ provides the functionality of operator overloading which allows the user to modify the working or to define its working for a new class. To overload an operator for a class, we need to define a special function with the operator symbol as the public member function of the class. C++ Program to Overload the Multiplication OperatorConsider a class ‘vector’ that represents the coordinate of a point in the three-dimensional plane. Now let’s overload the multiplication operator so that it can perform the cross product of two vectors. C++
Output
Vector 1: 2i-3j+7k Vector 2: 4i+2j-2k Vector 3: -8i+32j+16k ExplanationHere Vector v1 is calling the operator * function with Vector v2 as argument. The result is stored in Vector v3. If we wouldn’t have defined the multiplication operator for our data type, the statement v1 * v2 might have lead to an error. |
Reffered: https://www.geeksforgeeks.org
C++ |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |