-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.cpp
105 lines (93 loc) · 1.77 KB
/
post.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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <cstdlib>
#include "post.h"
#include "rand.h"
using namespace std;
unsigned int Post::lastId = 0;
/*Constructor*/
Post::Post(const string title, const string creator, const string text)
: title(title), creator(creator), text(text)
{
id = ++lastId;
date = new Date(rand() % day_secs);
#if (OBJ_P_MESSAGES)
cout << "I create a post with Id: "
<< this->id
<< ", title: '"
<< this->title
<< "', Creator: '"
<< this->creator
<< "', Date: "
<< date->get_str()
<< endl;
#endif
}
/*Copy constructor*/
Post::Post(const Post& post)
: id(post.id), title(post.title), creator(post.creator), text(post.text)
{
this->date = new Date(*post.date);
#if (OBJ_P_MESSAGES)
cout << "Post with title: '"
<< post.title
<< "' has been copied!"
<< endl;
#endif
}
string Post::get_creator() const {
return this->creator;
}
/*Accesors*/
unsigned int Post::getId() const{
return this->id;
}
unsigned int Post::getLastId(){
return lastId;
}
/*Prints*/
void Post::print() const{
cout << "\t\t#"
<< this->id
<< " '"
<< this->title
<< "'\n\t\tMessage: '"
<< this->text
<< "'\n\t\t(created by '"
<< this->creator
<< "' at "
<< date->get_str()
<< ")\n"
<< endl;
}
/*Print for each field*/
void Post::print(const int field) const{
switch (field){
case P_TITLE:
cout << "\t'" << title << "'" << endl;
break;
case P_CREATOR:
cout << "\t'" << creator << "'" << endl;
break;
case P_DATE:
cout << "\t";
date->print();
break;
case P_TEXT:
cout << "\t'" << text << "'" << endl;
break;
default:
cerr << "Field not exists!" << endl;
cin.get();
exit(1);
break;
}
}
/*Destructor*/
Post::~Post(){
#if (OBJ_P_MESSAGES)
cout << "Post number: "
<< this->id
<< " is about to be destroyed"
<< endl;
#endif
delete date;
}