Question

In: Computer Science

Using C++, identify suitable coding modules for the following (a) Overload the * operator so that...

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.

Solutions

Expert Solution

//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);
       }
};


Related Solutions

Using the program below, overload the Multiply operator to the Rectangle class… this will multiple the...
Using the program below, overload the Multiply operator to the Rectangle class… this will multiple the widths for the 2 Rectangles and multiple the lengths of the two rectangles (similar to how the “+” operator added the widths and lengths of the 2 rectangles). Overload this operator using the non-member method. Please copy-paste code into MS Word document and then show some screenshots of you running and testing this method using your IDE. Using the program below, overload the Divide...
In class we discussed how to overload the + operator to enable objects of type Fraction to be added together using the + operator.
2.(a) Fraction Operators Objective:In class we discussed how to overload the + operator to enable objects of type Fraction to be added together using the + operator. Extend the Fraction class definition so that the -, * and / operators are supported. Write a main function that demonstrates usage of all of these operators.(b)More Custom Types Objective:Define a set of classes that represent a Schedule and Course in the context of a university student who has a schedule with a...
Complete the following: Extend the newString class (attached) to include the following: Overload the operator +...
Complete the following: Extend the newString class (attached) to include the following: Overload the operator + to perform string concatenation. Overload the operator += to work as shown here: s1 = "Hello " s2 = "there" s1 += s2 // Should assign "Hello there" to s1 Add a function length to return the length of the string. Write a test program. //myString.h (header file) //Header file myString.h    #ifndef H_myString #define H_myString #include <iostream> using namespace std; class newString {...
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators...
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators in the code main.cpp //This program shows how to use the class rectangleType. #include <iostream> #include "rectangleType.h" using namespace std; int main() { rectangleType rectangle1(23, 45); //Line 1 rectangleType rectangle2(12, 10); //Line 2 rectangleType rectangle3; //Line 3 rectangleType rectangle4; //Line 4 cout << "Line 5: rectangle1: "; //Line 5 rectangle1.print(); //Line 6 cout << endl; //Line 7 cout << "Line 8: rectangle2: "; //Line...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
C++ Class involving Set intersection The goal is to overload the function: void Bag::operator/=(const Bag& a_bag)...
C++ Class involving Set intersection The goal is to overload the function: void Bag::operator/=(const Bag& a_bag) // Bag<int> bag1 = 1,2,3,4 //Bag<int> bag2 = 2,5,6,7 bag1+=bag2; bag1.display() should return 2 //Since type is void, you should not be returning any array but change the bag itself. #include <iostream> #include <string> #include <vector> using namespace std; template<class T> class Bag { public: Bag(); int getCurrentSize() const; bool isEmpty() const; bool add(const T& new_entry); bool remove(const T& an_entry); /** @post item_count_ ==...
C++ Class involving union. The goal is to overload the function: void Bag<T>::operator+=(const Bag<T>& a_bag) //...
C++ Class involving union. The goal is to overload the function: void Bag<T>::operator+=(const Bag<T>& a_bag) // The union of two sets A and B is the set of elements which are in A, in B, or in both A and B. For instance, Bag<int> bag1 = 1,2,3 and Bag<int> bag2 = 3,4,5 then bag1+=bag2 should return 1,2,3,4,5. //Since type is void, it should not return an array. #include <iostream> #include <string> #include <vector> using namespace std; template<class T> class Bag...
I need solution for following question. Self-Test Exercise 18 asked you to overload the operator >>...
I need solution for following question. Self-Test Exercise 18 asked you to overload the operator >> and the operator << for a class Percent. Complete and test this exercise. Implement the default constructor and the constructor with one int parameter. Overload the + and − operators to add and subtract percents. Also, overload the * operator to allow multiplication of a percent by an integer. Write a program to test all the member functions and overloaded operators in your class...
Organize the following information so that it is clear, concise, and suitable for entry into the...
Organize the following information so that it is clear, concise, and suitable for entry into the medical record. What statements will be S, O, A and P? You have been treating a patient with plantar fasciitis, providing ultrasound, soft tissue massage, and stretching. On the third visit, the patient indicates his pain in the morning has decreased from five out of then to three out of ten. His active and passive dorsiflexion range of motion have increased by five degrees....
Using Coding langues C ++ ---------------------------------------- There are three seating categories at a stadium. For a...
Using Coding langues C ++ ---------------------------------------- There are three seating categories at a stadium. For a softball game, Class A seats cost $15, Class B seats cost $12, and Class C seats cost $9. Write a program that asks how many tickets for each class of seats were sold, then displays the amount of income generated from ticket sales. Format your dollar amount in fixed-point notation, with two decimal places of precision, and be sure the decimal point is always...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT