forked from ShiqiYu/CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththis.cpp
72 lines (64 loc) · 1.39 KB
/
this.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
#include <iostream>
#include <cstring>
using namespace std;
class Student
{
private:
char * name;
int born;
bool male;
public:
Student()
{
name = new char[1024]{0};
born = 0;
male = false;
cout << "Constructor: Person()" << endl;
}
Student(const char * name, int born, bool male)
{
this->name = new char[1024];
setName(name);
this->born = born;
this->male = male;
cout << "Constructor: Person(const char, int , bool)" << endl;
}
~Student()
{
cout << "To destroy object: " << name << endl;
delete [] name;
}
void setName(const char * name)
{
strncpy(this->name, name, 1024);
}
void setBorn(int born)
{
this->born = born;
}
// the declarations, the definitions are out of the class
void setGender(bool isMale);
void printInfo();
};
void Student::setGender(bool isMale)
{
male = isMale;
}
void Student::printInfo()
{
std::cout << "Name: " << name << std::endl;
std::cout << "Born in " << born << std::endl;
std::cout << "Gender: " << (male ? "Male" : "Female") << std::endl;
}
int main()
{
Student * class1 = new Student[3]{
{"Tom", 2000, true},
{"Bob", 2001, true},
{"Amy", 2002, false},
};
class1[1].printInfo();
delete class1;
//delete []class1;
return 0;
}