Skip to content

Instantly share code, notes, and snippets.

@arisupriatna14
Created April 16, 2023 18:05
Show Gist options
  • Save arisupriatna14/a5beecdbcf0517c7974a57648e59f54b to your computer and use it in GitHub Desktop.
Save arisupriatna14/a5beecdbcf0517c7974a57648e59f54b to your computer and use it in GitHub Desktop.
#include <iostream>
class Waktu {
private:
int jam;
int menit;
public:
Waktu(int j, int m) {
jam = j;
menit = m;
}
// overloading operator ++
void operator++() {
menit++;
if (menit >= 60) {
jam++;
menit = 0;
}
if (jam >= 24) {
jam = 0;
}
}
// overloading operator --
void operator--() {
menit--;
if (menit < 0) {
jam--;
menit = 59;
}
if (jam < 0) {
jam = 23;
}
}
void display() {
std::cout << "H: " << jam << " M: " << menit << std::endl;
}
};
int main() {
Waktu w1(12, 0);
Waktu w2(10, 41);
--w1;
w1.display();
// increment waktu 1 menit
++w1;
w1.display();
// decrement waktu 1 menit
++w1;
w1.display();
--w2;
w2.display();
// increment waktu 1 menit
++w2;
w2.display();
// decrement waktu 1 menit
++w2;
w2.display();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment