面向对象程序设计-C++ Class & Object & Friend Function & Constructor & Destructor【第五次上课笔记】

时间:2023-11-17 11:27:44

大家可以下载后用Vim 或者 Sublime Text等文本编辑器查看

以下代码均已折叠,点击“+“即可打开

一开始老师用C语言大作业的例子,写了个 Student 的结构以及相关操作

 #include <iostream>
#include "Student.h" using namespace std; void display (Student* s) {
cout << "The current information of student:" << endl
<< "\tBirthday is " << s->birth.year << "-" << s->birth.month << "-" << s->birth.day << endl
<< "\tName is " << s->name << endl;
} void edit (Student* s, char* n){
/*
int i;
for (i = 0; n[i] != 0; ++i) {
s->name[i] = n[i];
}
s->name[i] = 0;
*/
s->name = n;
} int main(){ Date date;
date.year = ;
date.month = ;
date.day = ;
Student stu;
stu.birth = date;
stu.name = "Zhang San"; //Exist problems display (&stu); edit (&stu, "Li Si"); display (&stu); return ;
}

SourceCode.cpp

 struct Date {
int year, month, day;
}; struct Student {
Date birth;
char* name;
};

Student.h

不难发现,这份代码在

	stu.name = "Zhang San";	//Exist problems

 存在空间未分配的问题,还去赋值,暂时不管它

接着老师讲了正确姿势的写法,就是“初始化分配空间、结束的时候清空”的写法

 #include <iostream>
#include <cstring>
#include "Student.h" using namespace std; void display (Student* s) {
cout << "The current information of student:" << endl
<< "\tBirthday is " << s->birth.year << "-" << s->birth.month << "-" << s->birth.day << endl
<< "\tName is " << s->name << endl;
} void edit (Student* s, char* n){
/*
int i;
for (i = 0; n[i] != 0; ++i) {
s->name[i] = n[i];
}
s->name[i] = 0;
*/
//s->name = n; if (NULL != s->name) //if s->name is not NULL
delete[] s->name; int len = strlen (n);
s->name = new char[len + ];
strcpy (s->name, n); } void initialize (Student* s, Date* d, char* n) {
s->birth.year = d->year;
s->birth.month = d->month;
s->birth.day = d->day; int len = strlen (n);
s->name = new char[len + ];
strcpy (s->name, n);
//s->name = n;
} void clean (Student* s) {
delete[] s->name;
} int main(){ Date date;
date.day = ;
date.month = ;
date.year = ;
Student stu;
initialize (&stu, &date, "Zhang San"); display (&stu);
edit (&stu, "Li Si");
display (&stu); clean (&stu); return ;
}

SourceCode.cpp

 struct Date {
int year, month, day;
}; struct Student {
Date birth;
char* name; //void edit ();
//void display ();
};

Student.h

接下来,开始介绍了C++中面向对象的方法,使用 类来操作,同时把原来的函数都写进类使其成为成员函数

C++单文件版本:

 #include <iostream>
#include <cstring> using std::cout; //也可以这么写
using std::endl; //ADD struct Date {
int year, month, day;
}; class Student {
private: //成员的访问控制
Date birth;
char* name; public:
void edit (char* n); //成员函数
void display ();
void initialize (Date* d, char* n);
void clean ();
}; void Student::edit (char* n) { // :: means Scope operation
if (NULL != name) //if s->name is not NULL
delete[] name; int len = strlen (n);
name = new char[len + ];
strcpy (name, n);
} void Student::display () {
cout << "The current information of student:" << endl
<< "\tBirthday is " << birth.year << "-" << birth.month << "-" << birth.day << endl
<< "\tName is " << name << endl;
} void Student::initialize (Date* d, char* n) {
birth.year = d->year;
birth.month = d->month;
birth.day = d->day; int len = strlen (n);
name = new char[len + ];
strcpy (name, n);
} void Student::clean () {
if (NULL != name)
delete[] name;
} int main(){ Date date;
date.day = ;
date.month = ;
date.year = ;
Student stu; stu.initialize (&date, "Zhang San"); stu.display ();
stu.edit ("Li Si");
stu.display (); stu.clean (); return ;
}

SourceCode.cpp

老师也提到,在我们实际开发过程中是不会这么只一个cpp文件的,肯定是多文件

因为我注释写的比较详细,具体可以看以下代码:

Student.h

  

 #ifndef STUDENT_H    //编译预处理
#define STUDENT_H //防止多次包含头文件 struct Date {
int year, month, day;
}; class Student { private: //成员的访问控制
Date birth;
char* name; public:
void edit (char* n); //成员函数
void display ();
//void initialize (Date* d, char* n);
void clean (); //friend void frimen (); //友元函数 Student (Date* d, char* n);
Student ();
}; //void frimen () {
// Student stu;
// stu.birth.day = 16;
//} #endif
student.cpp

  

 #include <iostream>
#include <cstring>
#include "student.h" using std::cout; //也可以这么写
using std::endl; //ADD void Student::edit (char* n) { // :: means Scope operation
if (NULL != name) //if s->name is not NULL
delete[] name; int len = strlen (n);
name = new char[len + ];
strcpy (name, n);
} void Student::display () {
cout << "The current information of student:" << endl
<< "\tBirthday is " << birth.year << "-" << birth.month << "-" << birth.day << endl
<< "\tName is " << name << endl;
} Student::Student (Date* d, char* n) { //后面的Student表示,这个函数是构造函数
birth.year = d->year;
birth.month = d->month;
birth.day = d->day; int len = strlen (n);
name = new char[len + ];
strcpy (name, n);
} Student::Student () {
cout << "Student::Student () is Called" << endl;
} void Student::clean () {
if (NULL != name)
delete[] name;
}
SourceCode.cpp

  

 #include <iostream>
#include <cstring>
#include "student.h" int main(){ Date date;
date.day = ;
date.month = ;
date.year = ;
Student stu (&date, "Zhnag San"); //init Student stu2; //stu.initialize (&date, "Zhang San"); stu.display ();
stu.edit ("Li Si");
//stu.birth.year = 1989; //Cannot access private member declared in class 'Student'
stu.display (); stu.clean (); return ;
}