-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperson.cpp
54 lines (43 loc) · 996 Bytes
/
person.cpp
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
#include <cstdlib>
// Person class
class Person{
public:
Person(int);
int get();
int fib(); // Public method
void set(int);
private:
int fib_aux(int); // Private method
int age;
};
Person::Person(int n){
age = n;
}
int Person::get(){
return age;
}
int Person::fib(){ // Publicfib method that calls auxilary recursive fib method
return fib_aux(age);
}
int Person::fib_aux(int age){ // Private fib method that calculates and returns fib value
if(age<=1) {
return(age);
}else {
return(fib_aux(age-1)+fib_aux(age-2));
}
}
void Person::set(int n){
age = n;
}
extern "C"{
Person* Person_new(int n) {return new Person(n);}
int Person_get(Person* person) {return person->get();}
int Person_fib(Person* person) {return person->fib();} // Added fib method to C bridging code
void Person_set(Person* person, int n) {person->set(n);}
void Person_delete(Person* person){
if (person){
delete person;
person = nullptr;
}
}
}