大家可以下载后用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. 将dll放进exe[.Net]

    原文:将dll放进exe[.Net] 两种方案: 1.使用ILMerge工具. 缺点:需离开工程,使用第三方工具(ILMerge). 2.将dll作为Resource放进exe,exe执行时动态加载( ...

  2. angulajs 如何在controller 之间共享数据

    当在一个controller中 通过设置$scope的数据,来影响其它control的范围时,可以使用$rootScope 举个例子: 在一个control A范围内设置 mainApp.contro ...

  3. 贫血模型or领域模型

    参考: http://lifethinker.iteye.com/blog/283668 http://www.uml.org.cn/mxdx/200907132.asp http://www.itu ...

  4. HDU 1584 蜘蛛牌

    题解:纸牌只能移到比其大一的纸牌上,所以移动方向是定的,那么,就只有选择移动先后的问题了,对于决定要移的纸牌,比如1,如果2,3,4都是visited的状态,那么1一定是要移动到5的,因为2,3,4一 ...

  5. python利用utf-8编码判断中文英文字符(转)

    下面这个小工具包含了判断unicode是否是汉字.数字.英文或者其他字符,全角符号转半角符号,unicode字符串归一化等工作. #!/usr/bin/env python # -*- coding: ...

  6. 未能加载文件或程序集“DAL”或其他的某一个依赖项,系统找不到指定的文件

    针对这个问题我在敲VB.NET机房收费系统的时候总共出现了两次,第一次是在使用反射+抽象工厂的时候出现的,第二次是在使用VS2012自带的打包工具生成可执行文件执行exe文件的时候出现的.具体看下图: ...

  7. Objective-c 类实现 (@implementation)

    在用@interface声明类之后,可以使用@implementation进行实类的实现.类的实现的具体语法如下: @implementation 类名 方法实现代码; @end; 实例: @impl ...

  8. python基础补充

    关于模块导入方式: import  random print random.choice(range(10)) 和 from  random import choice print choice(ra ...

  9. plsql developer连接64位Oracle11g的解决方法

    1)安装Oracle 11g 64位 2)安装32位的Oracle客户端( instantclient-basic-win32-11.2.0.1.0)下载地址:http://www.oracle.co ...

  10. js得到分页栏

    自己写的,感觉返回html代码蠢蠢的,但是新手并不知道怎么写更好的,感觉这样子也蛮简单.记录下来,以后来越改越好. //获得分页栏.注意indexSize为奇数,这样也比较好看 //totalNum: ...