Laman

Jumat, 25 Februari 2011

Contoh Fungsi untuk Tugas OOP

Untuk teman-teman yang pingin lihat contoh untuk tugas OOP ini aku kasih beberapa contoh...

Operator*=::

Complex operator*=(Complex z){
         int x=this->real;
         int y=this->imaginary;
         this->real=(x*z.real)+((y*z.imaginary)*-1);
         this->imaginary=(x*z.imaginary)+(y*z.real);
         return *this;
}


Operator%=::

 Complex operator%=(Complex z){
         this->real%=z.real;
         this->imaginary%=z.imaginary;
         return *this;
}


Operator++::

 Complex operator++(){
         this->real++;
         this->imaginary++;
         return *this;
}

Operator==::

inline bool operator==(Complex z){
          int x=0;
          int y=0;
          if(this->real==z.real){
                x=1;
         }
         if(this->imaginary==z.imaginary){
                y=1;
         }
         return x==y&&x!=0;
}

Operator!=::

inline bool operator!=(Complex z){
          int x=0;
          int y=0;
          if(this->real!=z.real){
                x=1;
         }
         if(this->imaginary!=z.imaginary){
                y=1;
         }
         return x==y&&x!=0;
}

Operator>>::

istream &operator>>(istream &i,Complex &x){
    cout<<"Masukan nilai Complex"<<endl;
    cout<<"Real Part:";
    i>>x.real;
    cout<<"Imaginary Part:";
    i>>x.imaginary;
   return i;
}

Operator<<::

ostream &operator<<(ostream &i,Complex x){
    i<<x.real;
    if(x.imaginary!=0){
        if(x.imaginary>0){
            i<<"+";
        }
        i<<x.imaginary<<"i"<<endl;
   }
   i<<endl;
   return i;
}

class anonymous::

#include "conio.h"
#include "iostream.h"
class{
    private:
    int p,l,pl;
    public:
    void read(){
      cout<<"Enter The Length : ";
      cin>>p;
      cout<<"Enter The Width : ";
      cin>>l;
   }
   void calculate(){
       pl=p*l;
   }
   void display(){
       cout<<"Area = "<<pl<<endl;
   }
}LPP;
void main(){
    LPP.read();
    LPP.calculate();
    LPP.display();
    getch();
}

*untuk catatan operator '<<' dan '>>' jangan buat sebagai member class tapi buat sebagai friend class...

*untuk perhatian gunakan ini sebagai contoh pembelajaran buat kalian, jangan hanya di copy paste.. tapi buat lain jangan takutnya ada yang cuma copas dan jadinya dapat F... oke...

3 komentar: