user2370139
user2370139

Reputation: 1305

Operator overloading of "basic" classes

Hello I want to multiply a matrix by a number at left, something like : N=a*M where a is a number.

If i wanted to multiply at right i would have simply overloaded the operator * in my matrix class. But what shall I do in this case ? Can I overload the operator * of float even if it's a "default" class ? (I'm not even sure it's a class)

Upvotes: 3

Views: 111

Answers (2)

shivakumar
shivakumar

Reputation: 3397

Overload * operator to return matrix after multiplying with constant.

matrix  matrix :: operator* (int a)
{
   matrix temp(x, y);
    int num,num1, num2;
    A = new int *[temp.x];
    for (num=0; num<=temp.x; num++)
    {
        A[num] = new int [temp.y];
    }
        for (num1=0; num1<temp.x; num1++)
    {
        for (num2=0; num2<temp.y; num2++)
        {
            temp.A[num1][num2] = 0;
        }
    }
    int i, j;
    for ( i = 0; i < x; i++)
    {
        for ( j = 0; j < y; j++)

        {

            temp.A[i][j] = a * A[i][j];
        }
    }
    return (temp);
}

Refer this example matrix multiply by number

Upvotes: 0

David G
David G

Reputation: 96800

You would have to define the function in terms of the other. For example:

Matrix operator *(float x, Matrix const& m)
{
    return m * x;
}

Upvotes: 3

Related Questions