-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_definition.cpp
More file actions
executable file
·88 lines (68 loc) · 1.62 KB
/
class_definition.cpp
File metadata and controls
executable file
·88 lines (68 loc) · 1.62 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include "Person.h"
#include <typeinfo>
using namespace std;
void Person::WhoAmI()
{
string txtCivility;
switch (civility)
{
case Mr:
txtCivility = "mister";
break;
case Ms:
txtCivility = "Miss";
break;
default:
txtCivility = " nothing ";
}
cout << typeid(this).name() << ": my name is " << txtCivility << " " << name_ << " and I am " << findAge() << " y.o.\n";
}
int Person::findAge() const
{
return age; // method is declared const, impossible to modify anything
}
int Person::getLifeExpectancy()
{
return lifeExpectancy;
}
int Person::getPersonLE()
{
if (civility == Person::Mr)
return Person::getLifeExpectancy() - 5;
return Person::getLifeExpectancy() + 5;
}
int Person::lifeExpectancy = 80;
int Person::getCivility()
{
return civility;
}
int main()
{
//on the pile
Person p;
if (typeid(p) == typeid(Person))
{
cout << "same type \n";
}
p.name_ = "Jack Sparrow";
p.age = 20;
// use a member method
p.WhoAmI();
//on the heap
Person *p2 = new Person(); // or auto *p2 to modify name in one place only just in case
if (typeid(*p2) == typeid(Person))
{
cout << "same type \n";
}
p2->name_ = "Oliver twist";
p2->age = 22;
p2->civility = Person::Ms;
p2->WhoAmI();
cout << " human " << Person::getLifeExpectancy() << endl;
cout << " O T " << p2->getPersonLE() << endl;
cout << " human from O T " << p2->getLifeExpectancy() << endl;
delete (p2);
cout << "\n\n\n end of the program" << endl;
return 0;
}