top of page
Writer's pictureD A Rosi Arsida Wardani

I/O Operator Overloading dan file | Workshop 2

Updated: May 28, 2021

Program memasukkan jam, menit, detik

#include<iostream>
using namespace std;

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

	public :
		int jam, menit;
		float detik;
			
};

istream& operator>>(istream& in, waktu& pukul){
	cout<<"Masukkan jam : ";
	in>>pukul.jam;
	cout<<"Masukkan menit : ";
	in>>pukul.menit;
	cout<<"Masukkan detik : ";
	in>>pukul.detik;
	return in;
}

ostream& operator<<(ostream& out, waktu& time){
	out<<"Waktu yaitu "<<time.jam<<":"<<time.menit<<":"<<time.detik;
	return out;
}


int main(){
	waktu x;
	
	cin>>x;
	cout<<x;

	return 0;
}

Output :


 

Program penyebut pembilang bilangan rasional


#include<iostream>
using namespace std;

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

	public :
		int penyebut, pembilang;			
};

istream& operator>>(istream& in, bilangan& rasional){
	cout<<"PROGRAM PENYEBUT PEMBILANG\n";
	cout<<"-----------------------------\n";
	cout<<"Masukkan pembilang : ";
	in>>rasional.pembilang;
	cout<<"Masukkan penyebut : ";
	in>>rasional.penyebut;
	
	return in;
}

ostream& operator<<(ostream& out, bilangan& bilangan_rasional){
	out<<"Bilangan rasional yaitu "<<bilangan_rasional.pembilang<<"/"<<bilangan_rasional.penyebut;
	
	return out;
}


int main(){
	bilangan x;
	
	cin>>x;
	cout<<x;

	return 0;
}

Output :


 

Program ribuan, ratusan, puluhan, satuan

#include<iostream>
using namespace std;

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

	public :
		int ribu, ratus, puluh, satuan;
		void rumus(){
			ribu=ribuan/1000;
			ratus=ratusan/100;
			puluh=puluhan/10;
		}			
	private :
		int  ribuan, ratusan, puluhan;
};

istream& operator>>(istream& in, bilangan& angka){
	cout<<"Masukkan bilangan ribuan : ";
	in>>angka.ribuan;
	cout<<"Masukkan bilangan ratusan : ";
	in>>angka.ratusan;
	cout<<"Masukkan bilangan puluhan : ";
	in>>angka.puluhan;
	cout<<"Masukkan bilangan satuan : ";
	in>>angka.satuan;
	
	return in;
}

ostream& operator<<(ostream& out, bilangan& bagi){
	out<<"Bilangan : "<<bagi.ribu<<bagi.ratus<<bagi.puluh<<bagi.satuan;
	
	return out;
}


int main(){
	bilangan x;
	
	cin>>x;
	
	x.rumus();
	
	cout<<x;

	return 0;
}

Output :


 

Program operasi aritmatika

#include<iostream>
using namespace std;

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

	public :
		int a, b, jumlah, kurang, kali;
		
		void rumus(){
			jumlah=a+b;
			kurang=a-b;
			kali=a*b;
		}			
};

istream& operator>>(istream& in, hitung& angka){
	cout<<"Masukkan bilangan pertama : ";
	in>>angka.a;
	cout<<"Masukkan bilangan kedua : ";
	in>>angka.b;
	
	return in;
}

ostream& operator<<(ostream& out, hitung& tanda){
	out<<tanda.a<<" + "<<tanda.b<<" = "<<tanda.jumlah<<endl;
	out<<tanda.a<<" - "<<tanda.b<<" = "<<tanda.kurang<<endl;
	out<<tanda.a<<" * "<<tanda.b<<" = "<<tanda.kali<<endl;
	out<<tanda.a<<" : "<<tanda.b<<" = "<<tanda.a<<"/"<<tanda.b<<endl;
	
	return out;
}


int main(){
	hitung x;
	
	cin>>x;
	
	x.rumus();
	
	cout<<x;

	return 0;
}

Output :


 

Program bilangan scientific


#include <iostream>
#include <conio.h>
using namespace std;

class sci{
	friend ostream& operator<<(ostream&, const sci&);
	friend istream& operator>>(istream&, sci&);
	
	public :
		float a, b, bilangan;
		
		void seleksi(){
			if (bilangan>=1000){
				a=3;
				b=bilangan/1000;	
			}
		}	
};

istream& operator>>(istream& in, sci& masuk){
	cout<<"Masukkan bilangan 4 digit : ";
	in>>masuk.bilangan;

	return in;
}

ostream& operator<<(ostream& out, sci& hasil){
	out<<"Bilangan scientific dari "<<hasil.bilangan<<" yaitu : "<<hasil.b<<"E"<<hasil.a;
	
	return out;
}


int main(){
	sci scientific;
	
	cin>>scientific;
	scientific.seleksi();
	cout<<scientific;

	return 0;
}

Output :


Recent Posts

See All

One-dimensional Arrays

One-dimensional arrays are data structures that contain data types of the same type. In the form of a group of related memories...

Loop

Introduction Examples of algorithms in everyday life : Example 1 : To finish eating a plate of rice (initial conditions) Mouthfuls of a...

Comments


Post: Blog2_Post
bottom of page