Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Vector {
- private:
- int mI;
- int mJ;
- int mK;
- public:
- Vector();
- Vector(int, int, int);
- ~Vector();
- Vector& operator= (const Vector&);
- friend Vector operator+ (const Vector& lhs, const Vector& rhs);
- friend bool operator== (const Vector&, const Vector&);
- friend ostream& operator<< (ostream&, const Vector&);
- };
- Vector::Vector() : mI(0), mJ(0), mK(0) {}
- Vector::Vector(int i, int j, int k) : mI(i), mJ(j), mK(k) {}
- Vector::~Vector() {}
- Vector& Vector::operator=(const Vector& Ref)
- {
- if (&Ref == this) return *this;
- this->mI = Ref.mI;
- this->mJ = Ref.mJ;
- this->mK = Ref.mK;
- return *this;
- }
- Vector operator+ (const Vector& lhs, const Vector& rhs)
- {
- return Vector(lhs.mI + rhs.mI, lhs.mJ + rhs.mJ, lhs.mK + rhs.mK);
- }
- bool operator== (const Vector& lhs, const Vector& rhs)
- {
- return lhs.mI == rhs.mI && lhs.mJ == rhs.mJ && lhs.mK == rhs.mK;
- }
- ostream& operator<< (ostream& OutputStream, const Vector& rhs)
- {
- OutputStream << rhs.mI << "i";
- if (rhs.mJ >= 0)
- {
- OutputStream << "+";
- }
- OutputStream << rhs.mJ << "j";
- if (rhs.mK >= 0)
- {
- OutputStream << "+";
- }
- OutputStream << rhs.mK << "k";
- return OutputStream;
- }
- int main() {
- Vector *A, *B;
- A = new Vector (4, 5, 3);
- B = new Vector (4, 5, 3);
- Vector C = *A;
- Vector D = *B;
- cout << (C + D) << endl;
- if (C == D) {
- cout << "Vectors are same!" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment