-
Notifications
You must be signed in to change notification settings - Fork 377
/
StaticDemo.cpp
44 lines (42 loc) · 885 Bytes
/
StaticDemo.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
#include <string>
#include <iostream>
using namespace std;
class Person
{
public:
string firstname() const
{
return _firstname;
}
string lastname() const
{
return this->_lastname;
}
static int howmany() {
return counter;
}
static Person *create() {
return new Person("aa", "bb");
}
private:
Person(string lastname, string firstname)
: _firstname(firstname), _lastname(lastname)
{
counter ++;
}
const string _firstname = nullptr;
const string _lastname = nullptr;
static int counter;
};
int Person::counter = 0;
int main(void)
{
Person *p1 = Person::create();
Person p2("ou", "you");
//how many person?
std::cout << Person::howmany() << std::endl;
cout << p1.howmany() << std::endl;
cout << p2.howmany() << std::endl;
Person p4("xi", "biaoge");
return 0;
}