-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClass.cpp
More file actions
105 lines (87 loc) · 2.83 KB
/
Copy pathClass.cpp
File metadata and controls
105 lines (87 loc) · 2.83 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
98
99
100
101
102
103
104
105
/*
We can store details related to a student in a class consisting of his age (int), first_name (string), last_name (string) and standard (int).
You have to create a class, named Student, representing the student's details, as mentioned above, and store the data of a student. Create setter and getter functions for each element; that is, the class should at least have following functions:
get_age, set_age
get_first_name, set_first_name
get_last_name, set_last_name
get_standard, set_standard
Also, you have to create another method to_string() which returns the string consisting of the above elements, separated by a comma(,). You can refer to stringstream for this.
Input Format
Input will consist of four lines.
The first line will contain an integer, representing the age. The second line will contain a string, consisting of lower-case Latin characters ('a'-'z'), representing the first_name of a student.
The third line will contain another string, consisting of lower-case Latin characters ('a'-'z'), representing the last_name of a student.
The fourth line will contain an integer, representing the standard of student.
Note: The number of characters in first_name and last_name will not exceed 50.
Output Format
The code provided by HackerRank will use your class members to set and then get the elements of the Student class.
Sample Input
15
john
carmack
10
Sample Output
15
carmack, john
10
15,john,carmack,10
*/
#include <iostream>
#include <sstream>
using namespace std;
class Student{
int age;
int standard;
string first_name;
string last_name;
public:
Student()
{
age = 0;
standard = 0;
first_name.clear();
last_name.clear();
}
void set_age(int newAge)
{
age = newAge;
}
void set_standard(int newStandard)
{
standard = newStandard;
}
void set_first_name(string newFirst_name)
{
first_name = newFirst_name;
}
void set_last_name(string newLast_name)
{
last_name = newLast_name;
}
int get_age() {return age;}
int get_standard() {return standard;}
string get_first_name() {return first_name;}
string get_last_name() {return last_name;}
string to_string()
{
stringstream ss;
char c = ',';
ss << age << c << first_name << c << last_name << c << standard;
return ss.str();
}
};
int main() {
int age, standard;
string first_name, last_name;
cin >> age >> first_name >> last_name >> standard;
Student st;
st.set_age(age);
st.set_standard(standard);
st.set_first_name(first_name);
st.set_last_name(last_name);
cout << st.get_age() << "\n";
cout << st.get_last_name() << ", " << st.get_first_name() << "\n";
cout << st.get_standard() << "\n";
cout << "\n";
cout << st.to_string();
return 0;
}