-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperson.cpp
More file actions
97 lines (95 loc) · 2.45 KB
/
Copy pathperson.cpp
File metadata and controls
97 lines (95 loc) · 2.45 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
89
90
91
92
93
94
95
96
97
/*
* Title: Person Class
* Purpose: Definition of the methods of the Person class
* Author: Carlos Arias
* Date: April 29, 2020
*/
#include "person.h"
#include <cstring>
#include <string>
#include <sstream>
using std::string;
using std::stringstream;
/**
* Constructor
* Creates a person using the values of the parameters.
* @param name the name of the person as a string
* @param age the age of the person
*/
Person::Person(const string& name, size_t age) : _age(age){
_name = new char[name.length() + 1];
strcpy(_name, name.c_str());
}
/**
* Copy constructor
* Creates a deep copy of the person. The person uses dynamic memory
* for the name, so it is required to have a copy constructor
* @param person the original person being copied
*/
Person::Person(const Person& person) : _age(person._age){
_name = new char[strlen(person._name) + 1];
strcpy(_name, person._name);
}
/**
* Copy Assignment Operator
* Allows the deep assignment of two persons. Required, see Copy Constructor
* @param rhs the person to be copied into this
* @return this to follow chain assignment standard
*/
const Person& Person::operator=(const Person& rhs){
if (strlen(_name) != strlen(rhs._name)){
delete[] _name;
_name = new char[strlen(rhs._name) + 1];
}
strcpy(_name, rhs._name);
return *this;
}
/**
* Destructor
* Releases the memory used by the name
*/
Person::~Person(){
delete[] _name;
}
/**
* Creates a string representation of a person.
* The string representation is in JSON format
* @return a string representing the person
*/
string Person::ToString()const{
stringstream retVal;
retVal << "Person: {name: " << _name << ", age: " << _age << "}";
return retVal.str();
}
/**
* Compares two persons.
* Follows these steps:
* 1. Checks if the two objects are the same
* 2. Checks if rhs is actually a person
* 3. Casts rhs and compares if the two have the same name and age
* @param rhs the person to compare to
* @return true if the two person have the same name and age, false otherwise
*/
bool Person::Equals(const Object& rhs)const{
if (this == &rhs){
return true;
}
const Person* rhsPtr = dynamic_cast<const Person*>(&rhs);
if (rhsPtr == nullptr){
return false;
}
return strcmp(rhsPtr->_name, _name) == 0 && rhsPtr->_age == _age;
}
/**
* Explicitly creates a deep copy of this
* @return a new copy of this
*/
Object* Person::Clone()const{
return new Person(*this);
}
/**
* Increases the age of the person by 1
*/
void Person::Birthday(){
_age++;
}