-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path15_person-with_copy_constructor.cpp
113 lines (89 loc) · 2.09 KB
/
15_person-with_copy_constructor.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
106
107
108
109
110
111
112
113
// // Create a class Person with instance member variables for name and age. Implement a constructor that initializes these variables.
// // Header files
#include <iostream>
#include <string.h>
// // use namespace
using namespace std;
// // define class Person
class Person
{
public:
// // static member variable
static const int MAX_CHARS_NAME = 31;
private:
// // instance member variables
char name[MAX_CHARS_NAME];
int age;
public:
// // constructors
Person() {}
Person(int a)
{
age = a;
}
Person(char *nm)
{
strcpy(name, nm);
}
Person(char *nm, int a)
{
strcpy(name, nm);
age = a;
}
Person(int a, char *nm)
{
strcpy(name, nm);
age = a;
}
Person(Person &p)
{
strcpy(name, p.name);
age = p.age;
}
// // instance member function to set age
void setAge(int a)
{
age = a;
}
// // instance member function to set name
void setName(char *nm)
{
strcpy(name, nm);
}
// // instance member function to get age
int getAge()
{
return age;
}
// // instance member function to get name
char *getName(char *nm)
{
strcpy(nm, name);
return nm;
}
};
// // Main Function Start
int main()
{
char name[Person::MAX_CHARS_NAME];
int age;
// // Get person's Data
cout << "\nEnter Person's Age => ";
cin >> age;
cout << "\nEnter Person's Name (MAX_CHARACTERS " << Person::MAX_CHARS_NAME - 1 << ") => ";
cin.ignore();
cin.getline(name, Person::MAX_CHARS_NAME);
Person p1(name, age); // create object of Person
// // get and display persons's data
cout << "\nPerson's Age => " << p1.getAge();
cout << "\nPerson's Name => " << p1.getName(name);
// // create a copy of p1
Person p2 = p1;
// // get and display persons's data
cout << "\n\nPerson's Age => " << p2.getAge();
cout << "\nPerson's Name => " << p2.getName(name);
cout << endl; // Add new line
cin.ignore();
return 0;
}
// // Main Function End