Cute Finding Nemo

Jumat, 08 April 2016

Diktat Alpo Bab 2 - No.4

#Diktat Alpo Bab 2 - No.4

Workshop Pemrograman : Operator Overloading

4. Buatlah program untuk merepresentasikan operasi-operasi

aritmatika : penjumlahan,
pengurangan, perkalian dan pembagian. User diminta memasukkan 2 buah

bilangan bulat,
kemudian menampilkan sajian lengkapnya dari semua operasi. Sebagai

contoh, user
memasukkan 1 dan 2 maka tampilan outputnya :
1 + 2 = 2
1 – 2 = -1
1 * 2 = 2
1 : 2 = ½
class Hitung {
}

Programnya:
#include <iostream>

using namespace std;

class hitung{
    
public:
   void masukan();
   void proses();
   void keluaran();
 
private:
    float x,y,hasil1,hasil2,hasil3,hasil4;
   };
 
void hitung::masukan(){
   cout<<"\t\t --->>>MEREPRESENTASIKAN OPERASI-OPERASI<<<--- \n";
   cout<<"\n\t\t\t\t -->>ARITMATIKA<<--\n";
   cout<<"\nMasukkan Nilai Ke- 1 : ";cin>>x;
   cout<<"Masukkan Nilai ke- 2 : ";cin>>y;
   cout<<endl;
   }
 
void hitung::proses(){
    hasil1=x+y;
    hasil2=x-y;
    hasil3=x*y;
    hasil4=x/y;
      }

void hitung::keluaran(){
   cout<<"Hasil : "<<x<<" + "<<y<<"= "<<hasil1<<endl;
   cout<<"Hasil : "<<x<<" - "<<y<<"= "<<hasil2<<endl;
   cout<<"Hasil : "<<x<<" * "<<y<<"= "<<hasil3<<endl;
   cout<<"Hasil : "<<x<<" : "<<y<<"= "<<hasil4<<endl;
   }

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(){
   hitung bab2;
   char opsi;
   do{
   bab2.masukan();
   bab2.proses();
   bab2.keluaran();
   cout<<"\n\t-->>ULANGI LAGI y/n<<--\n";cin>>opsi;
   }
   while(opsi=='y');
   cout<<"\n\t--->>>TERIMAKASIH<<<---";
return 0;
}

0 komentar:

Posting Komentar