Advertisement
Guest User

Tugas 2

a guest
Feb 28th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include<iostream.h>
  2. #include<conio.h>
  3. class Persegi {
  4. public:
  5.     Persegi (float p = 0.0, float l = 0.0);
  6.     float hit_keliling();
  7.     float hit_luas();
  8.     void tampil();
  9.     void hasil();
  10. protected:
  11.     float panjang, lebar, keliling, luas;
  12. };
  13. Persegi::Persegi (float p, float l)
  14. {
  15. cout<< "Konstruktor Persegi panjang dijalankan"<<endl;
  16. panjang = p;
  17. lebar = l;
  18. }
  19. float Persegi::hit_keliling()
  20. {
  21.     keliling= panjang*lebar;
  22.     return keliling;
  23. }
  24. float Persegi::hit_luas()
  25. {
  26.     luas= 2*(panjang + lebar);
  27.     return luas;
  28. }
  29. void Persegi::tampil()
  30. {
  31.     cout<< "Panjang : "<<panjang<<endl;
  32.     cout<< "Lebar   : "<<lebar<<endl;
  33. }
  34. void Persegi::hasil()
  35. {
  36.     cout<< "Keliling Persegi : "<<keliling<<endl;
  37.     cout<< "Luas Persegi     : "<<luas<<endl;
  38. }
  39. class Kotak:public Persegi {
  40. public:
  41.     Kotak (float t = 0.0, float panjang = 0.0, float lebar = 0.0);
  42.     float hit_keliling();
  43.     float vol();
  44.     void tampil();
  45. protected:
  46.     float tinggi, keliling, volume;
  47. };
  48. Kotak::Kotak(float t, float p, float l) : Persegi (p, l)
  49. {
  50.     cout<< "Konstruktor Kotak dijalankan "<< endl;
  51.     tinggi = t;
  52. }
  53. float Kotak::hit_keliling()
  54.  
  55. {
  56.     keliling= ((2*panjang) + (2*lebar) + (2*tinggi));
  57.     return keliling;
  58. }
  59. float Kotak::vol()
  60. {
  61.     volume= panjang * lebar * tinggi;
  62.     return volume;
  63. }
  64. void Kotak::tampil()
  65. {
  66.     Persegi::tampil();
  67.     cout<< "Tinggi kotak     : "<<tinggi<<endl;
  68.     cout<< "Keliling kotak   : "<<keliling<<endl;
  69.     cout<< "Volume kotak     : "<<volume<<endl;
  70. }
  71. int main ()
  72. {
  73.     Persegi p(2, 4);
  74.     p.hit_keliling();
  75.     p.hit_luas();
  76.     p.tampil();
  77.     p.hasil();
  78.     cout<<endl;
  79.    
  80.     Kotak k( 3, 4, 5);
  81.     k.hit_keliling();
  82.     k.vol();
  83.     k.tampil();
  84.     getch();
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement