大家可以下载后用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. 制作一个vagrant的win7 box

    准备: 1.win7镜像文件 2.vagrant安装文件 3.virtual box安装文件 步骤: 1.先在本机上安装virtualbox和vagrant,本机为win7,安装虚机也为win7 2. ...

  2. 10min系列之二日志可视化进阶

    10min系列之二日志可视化进阶(作者原创,同步发布在github) 本文需要有一定的python和前端基础,如果没基础的,请关注我后续的基础教程系列博客 本文所有的demo,都是浏览器下展示的 原创 ...

  3. Protel中放置汉字工具的使用图示

    首先先到网上下载Protel中放置汉字工具ProtelHz.然后把ProtelHz中的文件全部解压到Protel99se安装目录X:\Program Files\Design Explorer 99 ...

  4. JAVA中List、Map、Set的区别与选用

    类层次关系如下: Collection ├List│├LinkedList│├ArrayList│└Vector│ └Stack└SetMap├Hashtable├HashMap └WeakHashM ...

  5. Android多线程断点续传下载

    这个月接到一个项目.要写一个像360助手一样的对于软件管理的APP:当中.遇到了一个问题:多线程断点下载 这个 ,因为之前没有写过这方面的应用功能.所以.不免要自学了. 然后就在各个昂站上收索并整理了 ...

  6. linux6.2安装mysql

    今天安装了一天的mysql,刚开始没想到会这么长时间,也和自己的基础不好有关,现在来说一下我的安装过程. 先说环境:rhel6.2 mysql5.1 在linux中,可能默认已经安装了一个mysql- ...

  7. WINCE平台下C#应用程序中使用看门狗

    看门狗定时器(WDT,Watch Dog Timer)是单片机的一个组成部分,它实际上是一个计数器,一般给看门狗一个大数,程序开始运行后看门狗开始倒计数.如果程序运行正常,过一段时间CPU应发出指令让 ...

  8. IIS MIME的 映射 网站有些类型的文件不能通过网页访问

    在iis中能够浏览所有扩展名的文件时,IIS MIME的 映射 您只能在故障排除过程中将通配符映射添加到 IIS MIME 映射中,以作为一种临时解决方案.确定缺少 MIME 类型是问题的原因后,请删 ...

  9. .html与.htm为网页后缀的区别

    有些人在做网页的时候会有疑问,到底是应该用.html还是.htm做网页后缀呢?.html和.htm有什么区别吗?在做网页时到底用哪一个好呢? 现在,我说一下我对这两个文件后缀的看法: 首先,要想使用后 ...

  10. Definitions

    Definitions and ODR Definitions are declarations that fully define the entity introduced by the decl ...