In: Computer Science
Using C++, identify suitable coding modules for the following
(a) Overload the * operator so that two instances of Quat can be
multiplied using the * operator.
Given that q1 and q2 are Quaternions. Let q1 = (a1, b1i, c1j, d1k)
and q2 = (a2, b2i, c2j, d2k)
The product (q1 * q2) is ( a1a2 – b1b2 – c1c2 – d1d2, (a1b2 + b1a2
+ c1d2 – d1c2)i, (a1c2 + c1a2 + d1b2 – b1d2)j, (a1d2 + d1a2 + b1c2
– c1b2)k )
For example
sq1 = (5, 2i, 6j, 8k)
sq2 = (3, 5i, 7j, 6k)
sq3 = sq1 * sq2 = (-85, 11i, 81j, 38k)
(b) Overload the == operator so that we can check whether two
instances of Quat are equal. Two instances are equal if each
element in one instance is equal to the corresponding element in
the other instance.
//C++ program
class Quat{
private:
int a,b,c,d;
public:
Quat(){
}
Quat operator *(Quat rhs){
Quat ob;
ob.a = a*rhs.a -
b*rhs.b - c*rhs.c - d*rhs.d;
ob.b = a*rhs.b +
b*rhs.a + c*rhs.d - d*rhs.c;
ob.c = a*rhs.c +
c*rhs.a + d*rhs.b - b*rhs.d;
ob.d = a*rhs.d +
d*rhs.a + b*rhs.c - c*rhs.b;
return ob;
}
bool operator==(Quat rhs){
return
(a==rhs.a)&&(b==rhs.b)&&(c==rhs.c)&&(d==rhs.d);
}
};