How to Overload the Multiplication Operator in C++? Last Updated : 25 Jan, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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++ // C++ Program to implement overload * operator without // using friend function #include <iostream> using namespace std; class Vector { int x, y, z; public: Vector(int x, int y, int z) { this->x = x; this->y = y; this->z = z; } void getVector() { cout << x << "i" << showpos << y << "j" << z << "k\n" << noshowpos; } // Overloading * operator to perform cross product of // two vectors, the left operand is *this object Vector operator*(Vector const& v) { int x = (this->y * v.z) - (this->z * v.y); int y = (this->z * v.x) - (this->x * v.z); int z = (this->x * v.y) - (this->y * v.x); return Vector(x, y, z); } }; // driver code int main() { Vector v1(2, -3, 7), v2(4, 2, -2); cout << "Vector 1: "; v1.getVector(); cout << "Vector 2: "; v2.getVector(); // multiplying two vector Vector v3 = v1 * v2; // Calling Operator function cout << "Vector 3: "; v3.getVector(); } OutputVector 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. Comment More infoAdvertise with us Next Article How to Overload the Less-Than (<) Operator in C++? S samuelnadar1211 Follow Improve Article Tags : C++ Programs C++ cpp-operator-overloading CPP-OOPs CPP Examples +1 More Practice Tags : CPP Similar Reads How to Overload the Less-Than (<) Operator in C++? In C++ we have an operator called less than operator (<) which checks if the left side operand is smaller than the right side operand or not. In this article, we will learn how to overload the less-than operator in C++. Overloading Less-Than Operator in C++In C++, we can overload the less-than op 2 min read How to Overload the (+) Plus Operator in C++? In C++, operator overloading is a feature of the OOPs concept that allows you to redefine the behavior for different operators when they are used with objects of user-defined classes. The plus operator (+) is a binary operator generally used for addition. In this article, we will learn how to overlo 2 min read How to Overload the Function Call Operator () in C++? In C++, operator overloading allows the user to redefine the behavior of an operator for a class. Overloading the function call operator () allows you to treat objects like functions enabling them to be called as if they were functions. Such classes are called functors in C++. In this article, we wi 2 min read Overloading the Comma Operator In C++, we can overload the comma operator using Operator Overloading. For Example: For "Send the query X to the server Y and put the result in variable Z", the "and" plays the role of the comma. The comma operator (, ) is used to isolate two or more expressions that are included where only one expr 5 min read How to Take Operator as Input in C++? Operators are symbols that specify some kind of operation. In C++, we sometimes need to take operators as user input mainly to perform mathematical operations. In this article, we will learn how to take operators as user input in C++. Operators as Input in C++To take operators (like +,-,*,/ etc) as 2 min read Overloading Relational Operators in C++ In C++, operator overloading is used to redefine the behavior of already existing operators. Similarly, overloading the relational operators is commonly used to compare the instances of user-defined classes. By overloading these operators we can easily define the behavior of comparisons for the obje 4 min read Like