Jumat, 23 Maret 2012

Latihan Penggunaan Overloading C++

#include <cstdlib>
#include <iostream>

using namespace std;

class contoh {
      friend istream& operator>>(istream&, contoh&);
      friend ostream& operator<<(ostream&, contoh&);
      public :
             contoh();
             void tambah();
      private :
              int a,b,c;
};

contoh::contoh() {
     cout<<"Contoh Overloading"<<endl;              
}

void contoh::tambah() {
     c=a+b;  
}

istream& operator>>(istream& in, contoh& input) {
     cout<<"Masukan bilangan:";
     in>>input.a;      
     cout<<"Masukan bilangan:";
     in>>input.b;
}

ostream& operator<<(ostream& out, contoh& output) {
         cout<<"Hasil bilangan:";
         out<<output.c;
}


int main(int argc, char *argv[])
{
    contoh coba;
    cin>>coba;
    coba.tambah();
    cout<<coba;
    cout<<endl;
  
    system("PAUSE");
    return EXIT_SUCCESS;
}




0 komentar:

Posting Komentar