13 Nisan 2010 Salı

C++ ile Operatörlerinin Aşırı Yüklenmesi(Operator Overloading)

//www.ethemsulan.com
#include <cstdlib>
#include <iostream>
using namespace std;
class point{
      public:
             point(int i=0,int j=0,int k=0){
                       setpoint(i,j,k);
                       }
             point operator+(point p);
             point operator=(point p);
             void displaypoint() const;
             void setpoint(int,int,int);
      private:
              int x,y,z;
      };
void point::setpoint(int i,int j,int k){
     x=i;
     y=j;
     z=k;
     }
void point::displaypoint() const{
     cout<<"{"<<x<<","<<y<<","<<z<<"}"<<endl;
     }
point point::operator+(point p){
      point result;
      result.x=x+p.x;
      result.y=y+p.y;
      result.z=z+p.z;
      return result;
      }
point point::operator=(point p){
      x=p.x;
      y=p.y;
      z=p.z;
      return *this;
      }
int main(int argc, char *argv[])
{
    point a(3,2,1),b(13,14,15),c;
    a.displaypoint();
    b.displaypoint();
    c=a+b;
    c.displaypoint();
    c=a+b+c;
    c.displaypoint();
    c=b=a;
    c.displaypoint();
    b.displaypoint();
    system("PAUSE");
    return EXIT_SUCCESS;
}

Hiç yorum yok:

Yorum Gönder