Bab 2 nomor 1 dan 4
BAB 2
nomor 1
a. #include<iostream>
#include<conio.h>
using namespace std;
class waktu{
friend ostream& operator<<(ostream&, waktu&);
friend istream& operator>>(istream&, waktu&);
private:
float waktu,detik,detikl,menitl,jaml;
public:
float coba_jam(){
detikl - detik; }
float coba_menit(){
menitl - detik/60; }
float coba_detik(){
jaml = detik/3600; }
};
istream& operator >> (istream& yes, waktu& no){
cout<<"masukan detik : ";
yes>> no.detik;
return yes;}
ostream& operator >> (ostream& ya, waktu& tidak){
ya<< "detik : "<<tidak.detikl<<endl;
ya<< "menit : "<<tidak.menitl<<endl;
ya<< "jam : "<<tidak.jaml<<endl;}
int main(){
waktu alpro;
cin>>alpro;
alpro.coba_jam();
alpro.coba_menit();
alpro.coba_detik();
cout<< alpro;
}
Hasil Output
b. #include "iostream"using namespace std;
class jum
{
public:
int x,y;
jum () {};
jum (int xx, int yy)
{
x=xx;
y=yy;
}
void tampil ()
{
cout<<"Input Nilai X = ";cin>>x;
cout<<"Input Nilai Y = ";cin>>y;
}
jum jum::operator + (jum ku);
};
jum jum::operator + (jum ku)
{
jum temp;
temp.x = x + ku.x;
temp.y = y + ku.y;
return (temp);
}
int main()
{
jum a(3,5);
jum b(1,2);
a.tampil ();
b.tampil ();
jum c;
c= a + b;
cout<<endl;
cout<<"Hasil Penjumlahan 2 Himpunan = "<<c.x<<","<<c.y<<endl;
system ("pause");
return 0;
}
Hasil Output
c. #include <iostream>using namespace std;
void sapaDuniailkom() {
cout << "Halo Duniailkom" << endl;
}
int main()
{
sapaDuniailkom();
sapaDuniailkom();
sapaDuniailkom();
return 0;
}
Hasil Output
Nomor 4#include<iostream>
using namespace std;
class bilangan{
friend ostream& operator << (ostream&, const bilangan&);
friend istream& operator << (istream&, bilangan&);
public:
friend istream& operator >> (istream &in, bilangan& bulat){
cout<<"masukan bilangan pertama: ";
in >> bulat.a;
cout<<"masukan bilangan kedua: ";
in >> bulat.b;
bulat.c = bulat.a + bulat.b;
bulat.d = bulat.a - bulat.b;
bulat.f = bulat.a * bulat.b;
bulat.g = bulat.a / bulat.b;
return in;
}
friend ostream& operator << (ostream& out, bilangan& bulat){
out<<bulat.a <<"+"<<bulat.b<<"="<<bulat.c<<endl;
out<<bulat.a <<"-"<<bulat.b<<"="<<bulat.d<<endl;
out<<bulat.a <<"*"<<bulat.b<<"="<<bulat.f<<endl;
out<<bulat.a <<"/"<<bulat.b<<"="<<bulat.g<<endl;
return out;
}
private:
int a,b,c,d,f,g;
}x;
int main(){
cin>>x;
cout<<x;
}
Hasil Output
Komentar
Posting Komentar