Skip to content

Commit 44f480b

Browse files
committed
pointers together with a class simple example | involve tuples too ..
1 parent b77a6e0 commit 44f480b

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Diff for: pointers/class_pointer_deux.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <tuple>
4+
5+
using namespace std;
6+
7+
8+
class Person
9+
{
10+
public:
11+
static Person newPerson(string name, int age) {
12+
return {name, age};
13+
}
14+
void sayHello() {
15+
cout << "Hello, my name is " << this->name << " and I am " << this->age << " y.o." << endl;
16+
}
17+
tuple<string, int> getData() {
18+
return make_tuple(name, age);
19+
}
20+
private:
21+
string name;
22+
int age;
23+
Person(string name, int age)
24+
: name(name), age(age)
25+
{}
26+
};
27+
28+
29+
int main()
30+
{
31+
Person diman = Person::newPerson("Zizou", 47);
32+
Person *str_ptr = &diman;
33+
34+
cout << "Hello, my name is " << get<0>(str_ptr->getData()) << " and I am " << get<1>(str_ptr->getData()) << " y.o." << endl;
35+
36+
// deallocate memory used by str_ptr in Heap memomry
37+
//
38+
delete str_ptr;
39+
40+
return 0;
41+
}
42+
43+

0 commit comments

Comments
 (0)