Skip to content

Commit

Permalink
update examples, to make them robust
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiqiYu committed May 19, 2024
1 parent 73dc53c commit e60be46
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 19 deletions.
Binary file modified week09/Lecture09.pdf
Binary file not shown.
Binary file modified week09/Lecture09.pptx
Binary file not shown.
6 changes: 6 additions & 0 deletions week09/examples/access-attribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ class Student
public:
void setName(const char * s)
{
if (s == NULL)
{
std::cerr << "The input is NULL." << std::endl;
return;
}
size_t len = sizeof(name) - 1;
strncpy(name, s, len);
name[len] = '\0';
}
void setBorn(int b)
{
Expand Down
6 changes: 6 additions & 0 deletions week09/examples/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ class Student

void setName(const char * s)
{
if (s == NULL)
{
std::cerr << "The input is NULL." << std::endl;
return;
}
size_t len = 1024 - 1;
strncpy(name, s, len);
name[len] = '\0';
}
void setBorn(int b)
{
Expand Down
6 changes: 6 additions & 0 deletions week09/examples/const.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ class Student

void setName(const char * s)
{
if (s == NULL)
{
std::cerr << "The input is NULL." << std::endl;
return;
}
size_t len = 1024 - 1;
strncpy(name, s, len);
name[len] = '\0';
}
void setBorn(int b)
{
Expand Down
15 changes: 13 additions & 2 deletions week09/examples/constructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ class Student
public:
Student()
{
name[0] = 0;
name[0] = '\0';
born = 0;
male = false;
cout << "Constructor: Person()" << endl;
}
Student(const char * initName): born(0), male(true)
Student(const char * initName): born(0), male(false)
{
setName(initName);
cout << "Constructor: Person(const char*)" << endl;
Expand All @@ -32,8 +32,14 @@ class Student

void setName(const char * s)
{
if (s == NULL)
{
std::cerr << "The input is NULL." << std::endl;
return;
}
size_t len = sizeof(name) - 1;
strncpy(name, s, len);
name[len] = '\0';
}
void setBorn(int b)
{
Expand Down Expand Up @@ -79,5 +85,10 @@ int main()
zhou->printInfo();
delete zhou;

Student * s = new Student();
// Student * s = new Student; // the same
s->printInfo();
delete s;

return 0;
}
6 changes: 6 additions & 0 deletions week09/examples/destructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ class Student

void setName(const char * s)
{
if (s == NULL)
{
std::cerr << "The input is NULL." << std::endl;
return;
}
size_t len = 1024 - 1;
strncpy(name, s, len);
name[len] = '\0';
}
void setBorn(int b)
{
Expand Down
6 changes: 6 additions & 0 deletions week09/examples/firstclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ class Student
bool male;
void setName(const char * s)
{
if (s == NULL)
{
std::cerr << "The input is NULL." << std::endl;
return;
}
size_t len = sizeof(name) - 1;
strncpy(name, s, len);
name[len] = '\0';
}
void setBorn(int b)
{
Expand Down
6 changes: 6 additions & 0 deletions week09/examples/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ class Student
public:
void setName(const char * s)
{
if (s == NULL)
{
std::cerr << "The input is NULL." << std::endl;
return;
}
size_t len = sizeof(name) - 1;
strncpy(name, s, len);
name[len] = '\0';
}
void setBorn(int b)
{
Expand Down
6 changes: 6 additions & 0 deletions week09/examples/multi-files/student.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ class Student
public:
void setName(const char * s)
{
if (s == NULL)
{
std::cerr << "The input is NULL." << std::endl;
return;
}
size_t len = sizeof(name) - 1;
strncpy(name, s, len);
name[len] = '\0';
}
void setBorn(int b)
{
Expand Down
10 changes: 8 additions & 2 deletions week09/examples/static.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ class Student

void setName(const char * s)
{
if (s == NULL)
{
std::cerr << "The input is NULL." << std::endl;
return;
}
size_t len = 1024 - 1;
strncpy(name, s, len);
name[len] = '\0';
}
void setBorn(int b)
{
Expand All @@ -66,7 +72,7 @@ void Student::printInfo()
std::cout << "Gender: " << (male ? "Male" : "Female") << std::endl;
}

//size_t Student::student_total = 0; // definition it here
size_t Student::student_total = 0; // definition it here

int main()
{
Expand All @@ -82,7 +88,7 @@ int main()

Student yu("Yu", 2000, true);

cout << "---We have " << Student::getTotal() << " students---" << endl;
cout << "---We have " << yu.getTotal() << " students---" << endl;

class1[1].printInfo();
delete []class1;
Expand Down
29 changes: 14 additions & 15 deletions week09/examples/this.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ class Student
male = false;
cout << "Constructor: Person()" << endl;
}
Student(const char * name, int born, bool male)
Student(const char * initName, int initBorn, bool isMale)
{
this->name = new char[1024];
this->setName(name);
this->born = born;
this->male = male;
name = new char[1024];
setName(initName);
born = initBorn;
male = isMale;
cout << "Constructor: Person(const char, int , bool)" << endl;
cout << "this = " << static_cast<void *>(this) << endl;
}
~Student()
{
Expand All @@ -33,8 +34,14 @@ class Student

void setName(const char * s)
{
if (s == NULL)
{
std::cerr << "The input is NULL." << std::endl;
return;
}
size_t len = 1024 - 1;
strncpy(name, s, len);
name[len] = '\0';
}
void setBorn(int b)
{
Expand All @@ -61,15 +68,7 @@ void Student::printInfo()

int main()
{
Student * class1 = new Student[3]{
{"Tom", 2000, true},
{"Bob", 2001, true},
{"Amy", 2002, false},
};

class1[1].printInfo();
delete []class1;


Student * s = new Student("Tom", 2000, true);
cout << "s = " << static_cast<void *>(s) << endl;
return 0;
}

0 comments on commit e60be46

Please sign in to comment.