面向对象程序设计-C++ Class & Object & Friend Function & Constructor & Destructor【第五次上课笔记】
大家可以下载后用Vim 或者 Sublime Text等文本编辑器查看
以下代码均已折叠,点击“+“即可打开
一开始老师用C语言大作业的例子,写了个 Student 的结构以及相关操作
1 #include <iostream> 2 #include "Student.h" 3 4 using namespace std; 5 6 void display (Student* s) { 7 cout << "The current information of student:" << endl 8 << "\tBirthday is " << s->birth.year << "-" << s->birth.month << "-" << s->birth.day << endl 9 << "\tName is " << s->name << endl; 10 } 11 12 void edit (Student* s, char* n){ 13 /* 14 int i; 15 for (i = 0; n[i] != 0; ++i) { 16 s->name[i] = n[i]; 17 } 18 s->name[i] = 0; 19 */ 20 s->name = n; 21 } 22 23 int main(){ 24 25 Date date; 26 date.year = 1992; 27 date.month = 10; 28 date.day = 23; 29 Student stu; 30 stu.birth = date; 31 stu.name = "Zhang San"; //Exist problems 32 33 display (&stu); 34 35 edit (&stu, "Li Si"); 36 37 display (&stu); 38 39 return 0; 40 }
1 struct Date { 2 int year, month, day; 3 }; 4 5 struct Student { 6 Date birth; 7 char* name; 8 };
不难发现,这份代码在
stu.name = "Zhang San"; //Exist problems
存在空间未分配的问题,还去赋值,暂时不管它
接着老师讲了正确姿势的写法,就是“初始化分配空间、结束的时候清空”的写法
1 #include <iostream> 2 #include <cstring> 3 #include "Student.h" 4 5 using namespace std; 6 7 void display (Student* s) { 8 cout << "The current information of student:" << endl 9 << "\tBirthday is " << s->birth.year << "-" << s->birth.month << "-" << s->birth.day << endl 10 << "\tName is " << s->name << endl; 11 } 12 13 void edit (Student* s, char* n){ 14 /* 15 int i; 16 for (i = 0; n[i] != 0; ++i) { 17 s->name[i] = n[i]; 18 } 19 s->name[i] = 0; 20 */ 21 //s->name = n; 22 23 if (NULL != s->name) //if s->name is not NULL 24 delete[] s->name; 25 26 int len = strlen (n); 27 s->name = new char[len + 1]; 28 strcpy (s->name, n); 29 30 } 31 32 void initialize (Student* s, Date* d, char* n) { 33 s->birth.year = d->year; 34 s->birth.month = d->month; 35 s->birth.day = d->day; 36 37 int len = strlen (n); 38 s->name = new char[len + 1]; 39 strcpy (s->name, n); 40 //s->name = n; 41 } 42 43 void clean (Student* s) { 44 delete[] s->name; 45 } 46 47 int main(){ 48 49 Date date; 50 date.day = 23; 51 date.month = 12; 52 date.year = 1993; 53 Student stu; 54 initialize (&stu, &date, "Zhang San"); 55 56 display (&stu); 57 edit (&stu, "Li Si"); 58 display (&stu); 59 60 clean (&stu); 61 62 return 0; 63 }
1 struct Date { 2 int year, month, day; 3 }; 4 5 struct Student { 6 Date birth; 7 char* name; 8 9 //void edit (); 10 //void display (); 11 };
接下来,开始介绍了C++中面向对象的方法,使用 类来操作,同时把原来的函数都写进类使其成为成员函数
C++单文件版本:
1 #include <iostream> 2 #include <cstring> 3 4 using std::cout; //也可以这么写 5 using std::endl; //ADD 6 7 struct Date { 8 int year, month, day; 9 }; 10 11 class Student { 12 private: //成员的访问控制 13 Date birth; 14 char* name; 15 16 public: 17 void edit (char* n); //成员函数 18 void display (); 19 void initialize (Date* d, char* n); 20 void clean (); 21 }; 22 23 void Student::edit (char* n) { // :: means Scope operation 24 if (NULL != name) //if s->name is not NULL 25 delete[] name; 26 27 int len = strlen (n); 28 name = new char[len + 1]; 29 strcpy (name, n); 30 } 31 32 void Student::display () { 33 cout << "The current information of student:" << endl 34 << "\tBirthday is " << birth.year << "-" << birth.month << "-" << birth.day << endl 35 << "\tName is " << name << endl; 36 } 37 38 39 void Student::initialize (Date* d, char* n) { 40 birth.year = d->year; 41 birth.month = d->month; 42 birth.day = d->day; 43 44 int len = strlen (n); 45 name = new char[len + 1]; 46 strcpy (name, n); 47 } 48 49 void Student::clean () { 50 if (NULL != name) 51 delete[] name; 52 } 53 54 int main(){ 55 56 Date date; 57 date.day = 23; 58 date.month = 12; 59 date.year = 1993; 60 Student stu; 61 62 stu.initialize (&date, "Zhang San"); 63 64 stu.display (); 65 stu.edit ("Li Si"); 66 stu.display (); 67 68 stu.clean (); 69 70 return 0; 71 }
老师也提到,在我们实际开发过程中是不会这么只一个cpp文件的,肯定是多文件
因为我注释写的比较详细,具体可以看以下代码:
Student.h
1 #ifndef STUDENT_H //编译预处理 2 #define STUDENT_H //防止多次包含头文件 3 4 struct Date { 5 int year, month, day; 6 }; 7 8 class Student { 9 10 private: //成员的访问控制 11 Date birth; 12 char* name; 13 14 public: 15 void edit (char* n); //成员函数 16 void display (); 17 //void initialize (Date* d, char* n); 18 void clean (); 19 20 //friend void frimen (); //友元函数 21 22 Student (Date* d, char* n); 23 Student (); 24 }; 25 26 //void frimen () { 27 // Student stu; 28 // stu.birth.day = 16; 29 //} 30 31 #endif
student.cpp
1 #include <iostream> 2 #include <cstring> 3 #include "student.h" 4 5 using std::cout; //也可以这么写 6 using std::endl; //ADD 7 8 void Student::edit (char* n) { // :: means Scope operation 9 if (NULL != name) //if s->name is not NULL 10 delete[] name; 11 12 int len = strlen (n); 13 name = new char[len + 1]; 14 strcpy (name, n); 15 } 16 17 void Student::display () { 18 cout << "The current information of student:" << endl 19 << "\tBirthday is " << birth.year << "-" << birth.month << "-" << birth.day << endl 20 << "\tName is " << name << endl; 21 } 22 23 24 Student::Student (Date* d, char* n) { //后面的Student表示,这个函数是构造函数 25 birth.year = d->year; 26 birth.month = d->month; 27 birth.day = d->day; 28 29 int len = strlen (n); 30 name = new char[len + 1]; 31 strcpy (name, n); 32 } 33 34 Student::Student () { 35 cout << "Student::Student () is Called" << endl; 36 } 37 38 void Student::clean () { 39 if (NULL != name) 40 delete[] name; 41 }
SourceCode.cpp
1 #include <iostream> 2 #include <cstring> 3 #include "student.h" 4 5 int main(){ 6 7 Date date; 8 date.day = 23; 9 date.month = 12; 10 date.year = 1993; 11 Student stu (&date, "Zhnag San"); //init 12 13 Student stu2; 14 15 //stu.initialize (&date, "Zhang San"); 16 17 stu.display (); 18 stu.edit ("Li Si"); 19 //stu.birth.year = 1989; //Cannot access private member declared in class 'Student' 20 stu.display (); 21 22 stu.clean (); 23 24 return 0; 25 }