| | |
overload functions
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
This might give you a start for how to define them:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <utility> class Integer { int _val; public: Integer(): _val(0) {} Integer(int val): _val(val) {} int get() const { return _val; } Integer operator+=(const Integer& rhs) { return _val += rhs._val; } Integer operator-=(const Integer& rhs) { return _val -= rhs._val; } Integer operator*=(const Integer& rhs) { return _val *= rhs._val; } Integer operator/=(const Integer& rhs) { return _val /= rhs._val; } Integer operator%=(const Integer& rhs) { return _val %= rhs._val; } Integer operator+(const Integer& rhs) { return _val + rhs._val; } Integer operator-(const Integer& rhs) { return _val - rhs._val; } Integer operator*(const Integer& rhs) { return _val * rhs._val; } Integer operator/(const Integer& rhs) { return _val / rhs._val; } Integer operator%(const Integer& rhs) { return _val % rhs._val; } Integer operator++() { return *this += 1; } Integer operator++(int) { Integer temp(*this); ++*this; return temp; } Integer operator--() { return *this -= 1; } Integer operator--(int) { Integer temp(*this); --*this; return temp; } friend bool operator==(const Integer& lhs, const Integer& rhs) { return lhs._val == rhs._val; } friend bool operator<(const Integer& lhs, const Integer& rhs) { return lhs._val < rhs._val; } }; int main() { using namespace std::rel_ops; // Sneaky sneaky ;-) Integer a(20); // Op-assign operators std::cout << a.get() <<'\n'; a += 2; std::cout << a.get() <<'\n'; a -= 2; std::cout << a.get() <<'\n'; a *= 2; std::cout << a.get() <<'\n'; a /= 2; std::cout << a.get() <<'\n'; a %= 2; std::cout << a.get() <<'\n'; a = 20; Integer c; // Arithmetic operators std::cout << c.get() <<'\n'; c = a + 2; std::cout << c.get() <<'\n'; c = a - 2; std::cout << c.get() <<'\n'; c = a * 2; std::cout << c.get() <<'\n'; c = a / 2; std::cout << c.get() <<'\n'; c = a % 2; std::cout << c.get() <<'\n'; a = 20; // Increment/decrement std::cout << (++a).get() <<'\n'; std::cout << (a++).get() <<'\n'; std::cout << (--a).get() <<'\n'; std::cout << (a--).get() <<'\n'; a = 20; c = 20; // Relational operators std::cout << (a == c) << '\n'; std::cout << (a != c) << '\n'; std::cout << (a < c) << '\n'; std::cout << (a <= c) << '\n'; std::cout << (a > c) << '\n'; std::cout << (a >= c) << '\n'; }
![]() |
Similar Threads
- Overloading PHP Functions (PHP)
- Tolerance (C)
- compile error (C++)
- need help overloading =, >>, << (C++)
- Need to know hwo to go about this (classes/accessor functions) (C++)
- How to write FNVAL functions (Java)
Other Threads in the C++ Forum
- Previous Thread: Printing
- Next Thread: Outputting data
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





