大家可以下载后用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 ;
}

面向对象程序设计-C++ Class & Object & Friend Function & Constructor & Destructor【第五次上课笔记】的更多相关文章

  1. 面向对象程序设计-C++ Finial exam review NOTES【第十六次上课笔记】

    写在前面: 我记得也不全,如果有记录的更全的同学可以留言,我会添加哒 :) 常量 内敛函数 为什么需要内敛函数 内敛函数适用于什么场合 内敛函数本身,最大优点是,避免了真正函数调用的开销 因为普通函数 ...

  2. 面向对象程序设计-C++ Operator Overloading & Type conversion (Static)【第十一次上课笔记】

    本次上课继续讲解了 [ ] .-> 等运算符重载的具体例子 也讲解了C++单个参数的类的类型转换的案例 最后稍微提到了 static 的第三种作用:静态数据成员 具体详解我都已注释出来了,大家可 ...

  3. 面向对象程序设计-C++ Stream & Template & Exception【第十五次上课笔记】

    这是本门<面向对象程序设计>课最后一次上课,刚好上完了这本<Thinking in C++> :) 这节课首先讲了流 Stream 的概念 平时我们主要用的是(1)在屏幕上输入 ...

  4. C++自学笔记 面向对象程序设计OOP(Object Oriented Programming)

    什么是对象? Objects = Attributes + Services Data : The properties or status Operations: the fuctions C语言中 ...

  5. 201871010133 赵永军《面向对象程序设计(java)》第六、七周学习总结

    201871010133 赵永军<面向对象程序设计(java)>第六.七周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...

  6. 面向对象程序设计(Java) 第6-7周学习指导及要求

    面向对象程序设计(Java)第6-7周学习指导及要求 (2019.9.29-2019.10.8)   学习目标 深入理解程序设计中算法与程序的关系: 深入理解java程序设计中类与对象的关系: 理解O ...

  7. Js中Prototype、__proto__、Constructor、Object、Function关系介绍

    一. Prototype.__proto__与Object.Function关系介绍 Function.Object:都是Js自带的函数对象.prototype,每一个函数对象都有一个显式的proto ...

  8. 【转】Js中Prototype、__proto__、Constructor、Object、Function关系介绍

    一    Prototype.__proto__与Object.Function关系介绍        Function.Object:Js自带的函数对象.         prototype,每一个 ...

  9. JavaScript 面向对象程序设计(下)——继承与多态 【转】

    JavaScript 面向对象程序设计(下)--继承与多态 前面我们讨论了如何在 JavaScript 语言中实现对私有实例成员.公有实例成员.私有静态成员.公有静态成员和静态类的封装.这次我们来讨论 ...

随机推荐

  1. win7 64位的PHP5.4安装redis扩展

    先看phpinfo.php信息 可以看是 PHP5.4 VC9 TS Architecture x86 说明是x86的PHP,虽然系统是64位的,所以还是要下载x86的redis 然后Github下载 ...

  2. Python 安装、循环语句、数据类型(一)

    一.关于版本的选择 Should i use Python 2 or Python 3 for my development activity?转载自Python官网 Short version: P ...

  3. php扩展类开发实例

    class Vector2D { private $_x; private $_y; /** * Constructor. */ public function __construct($x = 0, ...

  4. [HDU 4666]Hyperspace[最远曼哈顿距离][STL]

    题意: 许多 k 维点, 求这些点之间的最远曼哈顿距离. 并且有 q 次操作, 插入一个点或者删除一个点. 每次操作之后均输出结果. 思路: 用"疑似绝对值"的思想, 维护每种状态 ...

  5. [用UpdateLayeredWindow实现任意异形窗口]

    前面提到,我们可以用SetWindowRgn或SetLayeredWindowAttributes实现不规则以及半透明的效果 对于SetWindowRgn,它通过一个Rgn来设置区域,这个Rgn一般可 ...

  6. USACO 2001 OPEN

    第1题 绿组. 奶牛接力赛[relay] 题目描述 农夫约翰已经为一次赛跑选出了K(2≤K≤40)头牛组成了一支接力队.赛跑在农夫约翰所拥有的农场上进行,农场的编号为1到Ⅳf4≤Ⅳ< 800), ...

  7. dojo 学习笔记

    1  因为Dijit包括了一系列的UI组件,他绑定了4个支持的主题:nihilo, soria, tundra 和claro.每个主题包括了一系列的图片和CSS文件来控制组件的外观.CSS文件必须显示 ...

  8. 谁有SMI-S Provider的一些源码,能参考一下吗

    我要用OpenPegasus根据SMI-S规范来写provider,不知道如何下手啊,求高手指点

  9. Node.js入门-Node.js 介绍

    Node.js 是什么 Node.js 不是一种独立的语言,与 PHP,Python 等"既是语言优势平台"不同,它也不是一个 JavaScrip 框架,不同于 CakePHP,D ...

  10. JS 排列组合

    function $CL(arr, n, noLC, arrDan) { //从arr中取n个组合,然后 var r = [], sum = 0; n = n - arrDan.length; (fu ...