Operators

This is a discussion on Operators within the C++ Programming forums, part of the General Programming Boards category; Does anyone have any good coding examples of overloading the subtraction operator, or any operators. I am suppose to overload ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    5

    Operators

    Does anyone have any good coding examples of overloading the subtraction operator, or any operators. I am suppose to overload the prefix, and postfix, increment and decrement operators, as well as the stream insertion and stream extraction operators.
    Any suggestions would be greatly appreciated!!

  2. #2

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    #include<iosfwd>
    #include<algorithm>
    
    class boring
    {
    int x_;
    
    public:
    boring() : x_(0) {}
    boring(int x) : x_(x) {}
    boring(const boring& rhs) : x_(rhs.x_) {}
    boring& operator =(const boring& rhs)
    {
       boring temp(*this);
       std::swap (temp.x_ , rhs.x_);
       return *this;
    }
    boring& operator++() //prefix
    {
       ++x_;
       return *this;
    }
    boring& operator --()
    {
       --x_;
       return *this;
    }
    const boring operator ++(int) //postfix
    {
       boring temp(*this);
       ++(*this); // always implement post in terms of pre
       return temp;
    }
    const boring operator --(int)
    {
       boring temp(*this);
       --(*this);
       return temp;
    }
    const boring operator+( const boring& lhs,const boring& rhs)
    {
       return boring( lhs.x_ + rhs.x_ );
    }
    const boring operator -( const boring& lhs, const boring& rhs)
    {
       return boring( lhs.x_ - rhs.x_ );
    }
    std::ostream& print( std::ostream& os )const
    {
       os<<x_;
       return os;
    }
    std::istream& get( std::istream& is)
    {
       is>>x_;
       return is;
    }
    std::ostream& operator <<( std::ostream& os, const boring& b)
    {
       return b.print(os);
    }
    std::istream& operator >>( std::istream& is, boring& b)
    {
       return b.get(os);
    }
    };
    Last edited by Stoned_Coder; 04-02-2003 at 09:05 PM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    73
    Someone's a little bored. lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Operators in C++
    By Flecto in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2009, 07:17 AM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 06:42 PM
  3. operators???
    By arjunajay in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2005, 04:37 AM
  4. multiplying by 321 without using * or / operators
    By Silvercord in forum C++ Programming
    Replies: 8
    Last Post: 03-28-2003, 05:47 AM
  5. operators operands
    By verb in forum C Programming
    Replies: 6
    Last Post: 02-13-2002, 06:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21