-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritance.cpp
More file actions
47 lines (38 loc) · 904 Bytes
/
inheritance.cpp
File metadata and controls
47 lines (38 loc) · 904 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// 1. base class
// 2. derived class
// 3. akses modifikasi dalam pewarisan
// - public inheritance
// - protected inheritance
// - private inheritance
// 4. overrding dan polimorfisme
#include <iostream>
#include <string>
class Hewan {
protected:
std::string nama;
int umur;
public:
Hewan(std::string n, int u) : nama(n), umur(u) {}
void informasi() {
std::cout << "nama: " << nama << std::endl;
std::cout << "umur: " << umur << std::endl;
}
virtual void suara() {
std::cout << "hewan bersuara " << std::endl;
}
};
class Kucing : public Hewan {
public:
Kucing(std::string n, int u) : Hewan(n, u) {}
void suara() override {
std::cout << nama << " berkata: miaaaaw" << std::endl;
}
};
int main() {
Kucing kucing("james", 5);
Hewan* hewan_pointer;
hewan_pointer = &kucing;
hewan_pointer->informasi();
hewan_pointer->suara();
return 0;
}