C++/Overload/Plus Plus

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Overload ++ and -- for three_d.

  
#include <iostream>
using namespace std;
class three_d {
  int x, y, z; // 3-D coordinates
public:
  three_d() { x = y = z = 0; }
  three_d(int i, int j, int k) { x = i; y = j; z = k; }
  three_d operator++();
  three_d operator++(int notused);
  three_d operator--();
  three_d operator--(int notused);
  friend ostream &operator<<(ostream &strm, three_d op);
};
// Overload prefix ++ for three_d.
three_d three_d::operator++() {
  x++;
  y++;
  z++;
  return *this;
}
// Overload postfix ++ for three_d.
three_d three_d::operator++(int notused) {
  three_d temp = *this;
  x++;
  y++;
  z++;
  return temp;
}
// Overload prefix -- for three_d.
three_d three_d::operator--() {
  x--;
  y--;
  z--;
  return *this;
}
// Overload postfix -- for three_d.
three_d three_d::operator--(int notused) {
  three_d temp = *this;
  x--;
  y--;
  z--;
  return temp;
}
// The three_d inserter is a non-member operator function.
ostream &operator<<(ostream &strm, three_d op) {
  strm << op.x << ", " << op.y << ", " << op.z << endl;
  return strm;
}
int main()
{
  three_d objA(1, 2, 3), objB(10, 10, 10), objC;
  cout << "Original value of objA: " << objA;
  cout << "Original value of objB: " << objB;
  // Demonstrate ++ and -- as stand-alone operations.
  ++objA;
  ++objB;
  cout << "++objA: " << objA;
  cout << "++objB: " << objB;
  --objA;
  --objB;
  cout << "--objA: " << objA;
  cout << "--objB: " << objB;
  objA++;
  objB++;
  cout << endl;
  cout << "objA++: " << objA;
  cout << "objB++: " << objB;
  objA--;
  objB--;
  cout << "objA--: " << objA;
  cout << "objB--: " << objB;
  cout << endl;
  objC = objA++;
  cout << "After objC = objA++\n objC: " << objC <<" objA: " << objA << endl;
  objC = objB--;
  cout << "After objC = objB--\n objC: " << objC <<" objB: " << objB << endl;
  objC = ++objA;
  cout << "After objC = ++objA\n objC: " << objC <<" objA: " << objA << endl;
  objC = --objB;
  cout << "After objC = --objB\n objC: " << objC <<" objB: " << objB << endl;
  return 0;
}


overloaded ++ operator in both prefix and postfix

   
#include <iostream>  
  using namespace std;  
  class Counter{  
     private:  
        unsigned int count; 
     public:  
        Counter() : count(0){  }  
        Counter(int c) : count(c) {  }  
        unsigned int get_count() const{ return count; }  
    
        Counter operator ++ () {
           return Counter(++count);
        }
    
        Counter operator ++ (int){
           return Counter(count++);
        }                       
  };  
  int main(){  
     Counter c1, c2; 
    
     cout << "\nc1=" << c1.get_count();    
     cout << "\nc2=" << c2.get_count();  
    
     ++c1;                                 
     c2 = ++c1;                            
    
     cout << "\nc1=" << c1.get_count();    
     cout << "\nc2=" << c2.get_count();  
    
     c2 = c1++;
     cout << "\nc1=" << c1.get_count();    
     cout << "\nc2=" << c2.get_count() << endl;  
     return 0;  
  }


Overload ++ relative to MyClass class.

 
#include <iostream>
using namespace std;
class MyClass {
  int x, y; 
public:
  MyClass() { 
     x=0; 
     y=0; 
  }
  MyClass(int i, int j) { 
     x=i; 
     y=j; 
  }
  void getXY(int &i, int &j) { 
     i=x; 
     j=y; 
  }
  MyClass operator++();
};
// Overload ++ for MyClass class.
MyClass MyClass::operator++()
{
  x++;
  y++;
  return *this;
}
int main()
{
  MyClass object1(10, 10);
  int x, y;
  ++object1; 
  object1.getXY(x, y);
  cout << "(++object1) X: " << x << ", Y: " << y << endl;
  return 0;
}