[ create a new paste ] login | about

Link: http://codepad.org/skVotiWO    [ raw code | output | fork ]

C++, pasted on Sep 17:
#include <iostream>
using namespace std;
class vec {
  double x, y, z;
public:
  vec() { x = y = z = 0.0; }
  vec(double x, double y, double z) {
    this->x = x; this->y = y; this->z = z;
  }
  vec operator+(const vec &a) {
    vec r;
    r.x = this->x + a.x;
    r.y = this->y + a.y;
    r.z = this->z + a.z;
    return r;
  }
  vec operator-(const vec &a) {
    vec r;
    r.x = this->x - a.x;
    r.y = this->y - a.y;
    r.z = this->z - a.z;
    return r;
  }
  double operator*(const vec &a) {
    return this->x * a.x + this->y * a.y + this->z * a.z;
  }
  friend ostream &operator<<(ostream &s, const vec &a) {
    s << a.x << ' ' << a.y << ' ' << a.z;
    return s;
  }
  friend istream &operator>>(istream &s, vec &a) {
    s >> a.x >> a.y >> a.z;
    return s;
  }
};
int main() {
  vec a, b;
  cout << "a:" << endl;
  cin >> a;
  cout << "b:" << endl;
  cin >> b;
  cout << "a + b = " << a + b << endl;
  cout << "a - b = " << a - b << endl;
  cout << "a * b = " << a * b << endl;
  return 0;
}
/* end */


Output:
1
2
3
4
5
a:
b:
a + b = 0 0 0
a - b = 0 0 0
a * b = 0


Create a new paste based on this one


Comments: