File tree Expand file tree Collapse file tree 3 files changed +12
-15
lines changed Expand file tree Collapse file tree 3 files changed +12
-15
lines changed Original file line number Diff line number Diff line change @@ -17,9 +17,8 @@ int main(int argc, char **argv) {
17
17
ofile->open (" student.dat" , std::ios::binary | std::ios::trunc);
18
18
19
19
// setup student object
20
- Student *student = new Student ();
21
- student->setAge ((unsigned short )std::stoul (argv[2 ]));
22
- student->setName (argv[1 ]);
20
+ Student *student =
21
+ new Student (argv[1 ], static_cast <unsigned short >(std::stoul (argv[2 ])));
23
22
24
23
// write binary content into student
25
24
ofile->write (reinterpret_cast <char *>(student), sizeof (*student));
Original file line number Diff line number Diff line change 3
3
#include < sstream>
4
4
#include < string>
5
5
6
- void Student::setAge (int age) { this ->age = age; }
7
- void Student::setAge (short age) { this ->age = age; }
8
- void Student::setAge (unsigned short age) { this ->age = age; }
6
+ Student::Student () {
7
+ this ->name = " " ;
8
+ this ->age = 0 ;
9
+ }
9
10
10
- void Student::setName (std::string &name) { this ->name = name; }
11
- void Student::setName (const char *name) { this ->name = std::string (name); }
11
+ Student::Student (const char *name, unsigned short age) {
12
+ this ->name = std::string (name);
13
+ this ->age = age;
14
+ }
12
15
13
16
std::string Student::whoami () const {
14
17
std::stringstream s;
Original file line number Diff line number Diff line change @@ -6,12 +6,7 @@ class Student {
6
6
unsigned short age;
7
7
8
8
public:
9
- void setAge (int age);
10
- void setAge (short age);
11
- void setAge (unsigned short age);
12
-
13
- void setName (std::string &name);
14
- void setName (const char *name);
15
-
9
+ Student ();
10
+ Student (const char *name, unsigned short age);
16
11
std::string whoami () const ;
17
12
};
You can’t perform that action at this time.
0 commit comments