Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream.h>
- #include<conio.h>
- class Persegi {
- public:
- Persegi (float p = 0.0, float l = 0.0);
- float hit_keliling();
- float hit_luas();
- void tampil();
- void hasil();
- protected:
- float panjang, lebar, keliling, luas;
- };
- Persegi::Persegi (float p, float l)
- {
- cout<< "Konstruktor Persegi panjang dijalankan"<<endl;
- panjang = p;
- lebar = l;
- }
- float Persegi::hit_keliling()
- {
- keliling= panjang*lebar;
- return keliling;
- }
- float Persegi::hit_luas()
- {
- luas= 2*(panjang + lebar);
- return luas;
- }
- void Persegi::tampil()
- {
- cout<< "Panjang : "<<panjang<<endl;
- cout<< "Lebar : "<<lebar<<endl;
- }
- void Persegi::hasil()
- {
- cout<< "Keliling Persegi : "<<keliling<<endl;
- cout<< "Luas Persegi : "<<luas<<endl;
- }
- class Kotak:public Persegi {
- public:
- Kotak (float t = 0.0, float panjang = 0.0, float lebar = 0.0);
- float hit_keliling();
- float vol();
- void tampil();
- protected:
- float tinggi, keliling, volume;
- };
- Kotak::Kotak(float t, float p, float l) : Persegi (p, l)
- {
- cout<< "Konstruktor Kotak dijalankan "<< endl;
- tinggi = t;
- }
- float Kotak::hit_keliling()
- {
- keliling= ((2*panjang) + (2*lebar) + (2*tinggi));
- return keliling;
- }
- float Kotak::vol()
- {
- volume= panjang * lebar * tinggi;
- return volume;
- }
- void Kotak::tampil()
- {
- Persegi::tampil();
- cout<< "Tinggi kotak : "<<tinggi<<endl;
- cout<< "Keliling kotak : "<<keliling<<endl;
- cout<< "Volume kotak : "<<volume<<endl;
- }
- int main ()
- {
- Persegi p(2, 4);
- p.hit_keliling();
- p.hit_luas();
- p.tampil();
- p.hasil();
- cout<<endl;
- Kotak k( 3, 4, 5);
- k.hit_keliling();
- k.vol();
- k.tampil();
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement