Skip to content

Commit 904d7f5

Browse files
committed
Tambah: namespace/class/src/class.cpp
1 parent 3638029 commit 904d7f5

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

src/namespace/class/src/class.cpp

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include <iostream>
2+
3+
enum Status {
4+
HIDUP,
5+
MENINGGAL
6+
};
7+
8+
namespace Bentuk {
9+
class Lingkaran {
10+
private:
11+
int diameter;
12+
float phi;
13+
14+
public:
15+
Lingkaran(int _diameter) {
16+
std::cout << "(obj) Lingkaran dibuat dengan diameter: " << _diameter << std::endl;
17+
diameter = _diameter;
18+
phi = 3.14;
19+
}
20+
21+
int luas() {
22+
return phi * diameter * diameter;
23+
}
24+
25+
~Lingkaran() {
26+
std::cout << "(obj) Lingkaran dihapus" << std::endl;
27+
}
28+
};
29+
};
30+
31+
namespace {
32+
33+
class Manusia {
34+
private:
35+
std::string nama;
36+
int age;
37+
const char *jeniskelamin;
38+
Status status;
39+
40+
std::string meninggal = "alm. ";
41+
42+
public:
43+
Manusia(Status _status){
44+
std::cout << "(obj) manusia dibuat" << std::endl;
45+
status = _status;
46+
}
47+
48+
void aturnama(std::string _nama){
49+
if (status == MENINGGAL)
50+
nama = meninggal + _nama;
51+
else
52+
nama = _nama;
53+
}
54+
55+
void aturumur(int _age) {
56+
age = _age;
57+
}
58+
59+
void aturjeniskelamin(char _jeniskelamin) {
60+
if(_jeniskelamin == 'P')
61+
jeniskelamin = "Pria";
62+
else if(_jeniskelamin == 'W')
63+
jeniskelamin = "Wanita";
64+
else
65+
jeniskelamin = "tidak diatur, silahkan gunakan aturjeniskelamin(string)";
66+
}
67+
68+
std::string ambilnama() {
69+
return nama;
70+
}
71+
72+
int ambilumur() {
73+
return age;
74+
}
75+
76+
const char* ambiljeniskelamin() {
77+
return jeniskelamin;
78+
}
79+
80+
~Manusia() {
81+
std::cout << "(obj) manusia dihapus" << std::endl;
82+
}
83+
};
84+
};
85+
86+
int main() {
87+
using namespace std;
88+
89+
cout << "CPP Teaching Copyright (C) 2022 EmptyWork" << endl;
90+
91+
::Manusia jason(MENINGGAL);
92+
93+
jason.aturnama("Jason Aldo Amluina");
94+
jason.aturumur(23);
95+
jason.aturjeniskelamin('P');
96+
97+
cout << "Nama\t\t\t: " << jason.ambilnama() << endl;
98+
cout << "Umur\t\t\t: " << jason.ambilumur() << endl;
99+
cout << "Jenis Kelamin (P/W)\t: " << jason.ambiljeniskelamin() << endl;
100+
101+
Bentuk::Lingkaran bangunpertama(2);
102+
cout << "Luas lingkaran yaitu: " << bangunpertama.luas() << endl;
103+
}

0 commit comments

Comments
 (0)