Skip to content

Commit 062dacc

Browse files
committed
use constructors instead of setter functions
1 parent f2524d3 commit 062dacc

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

BinaryFile/main.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ int main(int argc, char **argv) {
1717
ofile->open("student.dat", std::ios::binary | std::ios::trunc);
1818

1919
// 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])));
2322

2423
// write binary content into student
2524
ofile->write(reinterpret_cast<char *>(student), sizeof(*student));

BinaryFile/student.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
#include <sstream>
44
#include <string>
55

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+
}
910

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+
}
1215

1316
std::string Student::whoami() const {
1417
std::stringstream s;

BinaryFile/student.hpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@ class Student {
66
unsigned short age;
77

88
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);
1611
std::string whoami() const;
1712
};

0 commit comments

Comments
 (0)