operasi aritmatika pada class

#include <cstdlib> // bagian file headernya
#include <iostream>

using namespace std;

//nama kelas  dan badan kelasnya

class Dimensi{
friend istream& operator>>(istream&,Dimensi&);//overload operator
friend ostream& operator<<(ostream&,const Dimensi&);
public:
Dimensi(); // nama konstruktor
void aritmatika();                               //nama fungsi
void operasiT(int x,int y);
void operasiK(int x,int y);
void operasiX(int x,int y);
void operasiB(int x,int y);
private:

int n,i,a,b;

float hasil,A[100];
char pil,c;
};

//implementasi konstruktor

Dimensi::Dimensi(){
cout<<“masukkan panjang array : “;
cin>>n;
for(i=0;i<n;i++){
cout<<“a[“<<i<<“] : “;
cin>>A[i];
}
}

//implementasi fungsi pada class

void Dimensi::operasiX(int x,int y){
hasil=A[x]*A[y];
cout<<“hasil : “<<hasil;
}

void Dimensi::operasiB(int x,int y){
hasil=A[x]/A[y];
cout<<“hasil : “<<hasil;
}

void Dimensi::operasiT(int x,int y){
hasil=A[x]+A[y];
cout<<“hasil : “<<hasil;
}
void Dimensi::operasiK(int x,int y){
hasil=A[x]-A[y];
cout<<“hasil : “<<hasil;
}

void Dimensi::aritmatika(){
cout<<“Masukkan indek array dan operasi yang anda inginkan \n”;
cout<<“contoh 2+1, 3-2, 5*6, 7/3 \n”;
cin>>a >> c >> b;

if(c==’+’)
operasiT(a,b);
else if(c==’-‘)
operasiK(a,b);
else if(c==’/’)
operasiB(a,b);
else if(c==’*’)
operasiX(a,b);
else cout<<“\noperasi yang anda masukkan anda salah”;
}

//fungsi utama

int main(int argc, char *argv[])
{   Dimensi p;
p.aritmatika();
cout<<endl;
system(“PAUSE”);
return EXIT_SUCCESS;
}

About classcpp

menyukai hal yang menantang
This entry was posted in source code program and tagged , , . Bookmark the permalink.

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