Contoh penggunaan IF

Contoh penggunaan fungsi If, memasukkan nilai x ke dalam rumus x^2 + 1

 

#include <cstdlib>
#include <iostream>
#include <math.h>
#include <conio.h>

using namespace std;

class Hitung{
friend istream & operator >> ( istream &,Hitung & );
friend ostream & operator << ( ostream &,const Hitung & );
public:
void pilih();
private:
int x;
float hasil;
};

istream & operator >> ( istream &in,Hitung & masukkan ){
cout<<“Masukkan X :”;
in>>masukkan.x;
return in;
}

ostream & operator << ( ostream &out,const Hitung &keluar ){
out<<“Hasilnya : “<<keluar.hasil;
return out;
}

void Hitung::pilih(){

if ( x < 0 ) hasil = (float)2 /(x*x);
else if ( x > 0 )
hasil = ( float )x * x + 1;
else
hasil = 1;
}

int main(int argc, char *argv[])
{
Hitung A;
cin >> A;
A.pilih();
cout << A;

getch();
return 0;

system(“PAUSE”);
return EXIT_SUCCESS;
}


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s