// Program to overload arithmatic operators for Vectors. // Assignment-25 // vectoo.cpp, Author - Krishna Kumar Khatri. // Uncomment ONLY the following line if you are using Turbo/Borland compiler. //#define TC #ifdef TC #include #include #else #include #include using namespace std; #endif #include // For getch. const int MAXSIZE = 500; const int null = 0; class Vector { int vect[MAXSIZE]; int sz; public: Vector(const int&); Vector operator+(const Vector&); Vector operator+(const int&); Vector operator++(); // Pre. Vector operator++(int); // Post. Vector operator-(const Vector&); Vector operator-(const int&); Vector operator--(); // Pre. Vector operator--(int); // Post. friend ostream& operator<<(ostream&, const Vector&); }; Vector::Vector(const int& empty) { int i, inpt; // If the user is not supposed to fill up the vector (initialize) // empty is true (1), false (0) otherwise. if (empty == 0) { cout << endl << "Enter -1 to terminate the input." << endl; cout << "Max no. of elements allowed " << MAXSIZE << ": "; for (i = 0; i < MAXSIZE; i++) { cin >> inpt; if (inpt != -1) vect[i] = inpt; else break; } sz = i; } else sz = 0; } Vector Vector::operator+(const Vector& sparam) { Vector res(1); // This vector must initially be empty. int i; if (this->sz == sparam.sz) { res.sz = sparam.sz; // Reinitialize the resultant vector's sz. for (i = 0; i < sparam.sz; i++) res.vect[i] = this->vect[i] + sparam.vect[i]; } else cout << endl << endl << "Vector size mismatch!"; return res; } Vector Vector::operator+(const int& inc) { Vector res(1); int i; res.sz = this->sz; for (i = 0; i < this->sz; i++) res.vect[i] = this->vect[i] + inc; return res; } Vector Vector::operator++() { *this = *this + 1; return *this; } Vector Vector::operator++(int dummy) { Vector save(1); save = *this; *this = *this + 1; return save; } Vector Vector::operator-(const Vector& sparam) { Vector res(1); // This vector must initially be empty. int i; if (this->sz == sparam.sz) { res.sz = sparam.sz; // Reinitialize the resultant vector's sz. for (i = 0; i < sparam.sz; i++) res.vect[i] = this->vect[i] - sparam.vect[i]; } else cout << endl << endl << "Vector size mismatch!"; return res; } Vector Vector::operator-(const int& dec) { Vector res(1); int i; res.sz = this->sz; for (i = 0; i < this->sz; i++) res.vect[i] = this->vect[i] - dec; return res; } Vector Vector::operator--() { *this = *this - 1; return *this; } Vector Vector::operator--(int dummy) { Vector save(1); save = *this; *this = *this - 1; return save; } ostream& operator<<(ostream& os, const Vector& v) { int sz = v.sz; int i; if (v.sz != 0) { for (i = 0; i < sz; i++) (i + 1) < sz ? os << setw(3) << v.vect[i] << ", " : os << setw(3) << v.vect[i]; } else os << "NULL Vector."; return os; } int main() { #ifdef TC clrscr(); #endif int i, n; char choice; Vector res(1); // This vector must initially be empty. cout << "Enter data for first vector: "; Vector vect1(0); cout << endl << "Enter data for second vector: "; Vector vect2(0); res = vect1 + vect2; cout << endl << "Vector + > "; cout << res; res = vect1 - vect2; cout << endl << "Vector - > "; cout << res; ++vect1; cout << endl << endl << "Vector-1 ++ > "; cout << vect1; --vect2; cout << endl << "Vector-2 -- > "; cout << vect2; cout << endl << endl << "Program over. Press any key to exit..."; getch(); return 0; // Successful termination. }