How do I overload the input and + operator for quaternion class?

2
Contributors
4
Replies
5
Views
10 Years
Discussion Span
Last Post by toolbox03

type classname::operator+(argument-list)
{
operation relative to the class
}

type classname::operator+(argument-list)
{
operation relative to the class
}

do u have any sample code?

I have the following code sitting on my desktop from a book I've used previously.

``````// Overload operators using member functions.
#include <iostream>
using namespace std;

class three_d {
int x, y, z; // 3-D coordinates
public:
three_d() { x = y = z = 0; }
three_d(int i, int j, int k) {x = i; y = j; z = k; }

three_d operator+(three_d op2); // op1 is implied
three_d operator=(three_d op2); // op1 is implied

void show() ;
};

three_d three_d::operator+(three_d op2)
{
three_d temp;

temp.x = x + op2.x; // These are integer additions
temp.y = y + op2.y; // and the + retains is original
temp.z = z + op2.z; // meaning relative to them.
return temp;
}

three_d three_d::operator=(three_d op2)
{
x = op2.x; // These are integer assignments
y = op2.y; // and the = retains its original
z = op2.z; // meaning relative to them.
return *this;
}

// Show X, Y, Z coordinates.
void three_d::show()
{
cout << x << ", ";
cout << y << ", ";
cout << z << "\n";
}

int main()
{
three_d a(1, 2, 3), b(10, 10, 10), c;

a.show();
b.show();

c = a + b; // add a and b together
c.show();

c = a + b + c; // add a, b and c together
c.show();

c = b = a; // demonstrate multiple assignment
c.show();
b.show();

return 0;
}``````

great, do you know how to overload the output function?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.