Jumat, 15 April 2011

Program C++ memindahkan isi dalam variable ke variable lain


Algoritma :
Deskripsi
a,b, t : integer
Deklarasi
t = a
a = b
b = t

Berikut programnya :

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

class memindahkan{
friend istream& operator >> (istream&, memindahkan&);
friend ostream& operator << (ostream&, memindahkan&);

public :
memindahkan();
void tukar(){
   t=a ;
   a=b ;
   b=t ;
};

private :
   int a,b;
   int t;
};

memindahkan::memindahkan(){
   cout<<"memindahkan bilangan antar variable"<<endl;
}

istream& operator >> (istream& cin, memindahkan& masukan){
   cout<<"isikan nilai a : ";
   cin>>masukan.a;
   cout<<"isikan nilai b : ";
   cin>>masukan.b;
   cout<<endl;

return cin;
}

ostream& operator << (ostream& out, memindahkan& keluar){
   out<<"hasil setelah di pindah\n"<<endl;
   out<<"nilai a : "<<keluar.a<<endl;
   out<<"nilai b : "<<keluar.b<<endl;

return out;
}

 void main(){
memindahkan nilai;
cin>>nilai;
nilai.tukar();
cout<<nilai;

getch();
}


Explanation:
The program above is a program used to move the contents of the variable to another variable by adding a new variable that is used to store data temporarily. Where variables used are a, b, and t. The process of working this program is to store the original value of a to the t, then the value of b will be loaded into a and b values ​​will be loaded with the value of t that is moving from a value at the beginning of the process. That explanation of this program. Thank you and good luck.

0 komentar:

Posting Komentar

Template by:

Free Blog Templates