contoh program class :
#include <iostream.h>
class bilangan{
private:
int a,b;
public:
void hasil(){
cout<<"masukan nilai a : ";
cin>>a;
cout<<"masukan nilai b : ";
cin>>b;
if(a>b){
cout<<"bilangan terbesar : "<<a<<endl;
}
else {
cout<<"Bilangan terbesar : "<<b<<endl;
}
}};
int main(){
int x;
bilangan a;
a.hasil();
cin>>x;
return 0;
}
Nilai Huruf
#include <iostream.h>
class bilangan{
private:
int nilai;
char nilai_huruf;
public:
void nilai_hasil(){
cout<<"Masukan nilai angka : ";
cin>>nilai;
if((nilai > 0)&&(nilai <= 20)){
nilai_huruf='E';
}
else if((nilai > 20)&&(nilai <= 40)){
nilai_huruf='D';
}
else if((nilai > 40)&&(nilai <= 60)){
nilai_huruf='C';
}
else if((nilai > 60)&&(nilai <= 80)){
nilai_huruf='B';
}
else {
nilai_huruf='A';
}
cout<<"Nilai huruf = "<<nilai_huruf<<endl;
}};
int main(){
int x;
bilangan a;
a.nilai_hasil();
cin>>x;
return 0;
}
Nama Hari
#include <iostream.h>
class HARI {
private:
int hari_ke;
char hari[100];
public:
void Set_Hari(){
cout<<"Masukkan hari ke-";
cin>>hari_ke;
switch (hari_ke){
case 1 : strcpy(hari, "Senin");break;
case 2 : strcpy(hari, "Selasa");break;
case 3 : strcpy(hari, "Rabu");break;
case 4 : strcpy(hari, "Kamis");break;
case 5 : strcpy(hari, "Jumat");break;
case 6 : strcpy(hari, "Sabtu");break;
default : strcpy(hari, "Minggu");break;
}
cout<<"Hari ke-"<<hari_ke<<" "<<"adalah"<<hari<<endl;
}
};
int main(){
int x;
HARI H;
H.Set_Hari();
cin>>x;
return 0;
}
0 komentar:
Posting Komentar