contoh program c++ untuk class jarak


Modul 7 latihan 1

#include <iostream.h>

enum boolean{salah, benar};

class Jarak

{

private:

int feet;

float inches;

public:

Jarak(){feet=0; inches=0.0;};

Jarak(int ft, float in){feet=ft; inches=in;}

void get_Jarak();

void show_Jarak(){cout << feet << “\’-” << inches << “\””;}

Jarak operator – (Jarak);

Jarak operator + (Jarak);

boolean operator < (Jarak);

boolean operator > (Jarak);

~Jarak(){}

};

void Jarak::get_Jarak()

{

cout << “Berikan feet: “;

cin >> feet;

cout << “Berikan inches: “;

cin >> inches;

}

Jarak Jarak::operator -(Jarak j)

{

int f = feet – j.feet;

float i = inches – j.inches;

if(i<=12.0)

{

i += 12.0;

f–;

}

return Jarak(f,i);

}

Jarak Jarak::operator +(Jarak k)

{

int f = feet + k.feet;

float i = inches + k.inches;

if(i>=12.0)

{

i -= 12.0;

f++;

}

return Jarak(f,i);

}

boolean Jarak::operator <(Jarak j)

{

float f1 = feet + inches/12.0F;

float f2 = j.feet +j.inches/12.0F;

return (f1 < f2) ? benar : salah;

}

boolean Jarak::operator >(Jarak j)

{

float f1 = feet + inches/12.0F;

float f2 = j.feet +j.inches/12.0F;

float f3 = j.feet +j.inches/12.0F;

float f5 = f2+f3;

return (f1 > f5) ? benar : salah;

}

int main()

{

Jarak j1, j3, j4, j5;

j1.get_Jarak();

Jarak j2(11, 6.25);

j3 = j1 – j2;

j5 = j2+j3;

j4 = j1 – j2 – j3;

if(j1 < j2)

cout << “Maaf, hasilnya negatif”;

else

{

cout << “\nJarak 1 = “; j1.show_Jarak();

cout << “\nJarak 2 = “; j2.show_Jarak();

cout << “\nJarak 3 = “; j3.show_Jarak();

cout << endl;

if(j1 > j5)

{

cout << “\nJarak 4 = “; j4.show_Jarak();

}

else

cout << “Maaf, Jarak 4 negatif”;

cout << endl;

}

return 0;

}

modul 7 latihan 2

#include <iostream.h>

class Hitung

{

private:

unsigned int cacah;

public:

Hitung(){cacah=0;};

Hitung(int c){cacah = s;}

int get_Hitungan(){return cacah;}

Hitung operator += ()

{

cacah+=;

return Hitung(cacah);

}

~Hitung(){}

};

int main()

{

Hitung s1, s2, s3;

cout << “\ns1 = ” << s1.get_Hitungan();

cout << “\ns2 = ” << s2.get_Hitungan();

s1 += s2;

s3 = s1 += s2;

cout << “\ns1 = ” << s1.get_Hitungan();

cout << “\ns2 = ” << s2++.get_Hitungan();

cout << endl;

return 0;

}

Tinggalkan Balasan

Please log in using one of these methods to post your comment:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s