大家可以下载后用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. BeanUtils复制属性

    package xiao; public class User2 { private String name; private String password; public String getNa ...

  2. document.createElement在IE和Firefox下的差异

    IE有3种方式都可以创建一个元素: 1 document.createElement("<input type=text>") 2 document.createEle ...

  3. 在GridView中实现全选反选的例子

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView控件.asp ...

  4. 在OSX狮子(Lion)上安装MYSQL(Install MySQL on Mac OSX)

    这篇文章简述了在Mac OSX狮子(Lion)上安装MySQL Community Server最新版本v10.6.7的过程. MySQL是最流行的开源数据库管理系统.首先,从MySQL的下载页面上下 ...

  5. python异常处理try,except,else,finally,raise

    先看下else的使用: try: ... exception: ... else: ... 只有在try中没有发生任何异常,所有代码完全成功的情况下才会转入else 再看下finally: final ...

  6. 2014年去哪儿网笔试题--有两个文件context.txt和words.conf,请尝试将他们合并成为一段文字,并打印出来。

    有两个文件context.txt和words.conf,请尝试将他们合并成为一段文字,并打印出来. 这两个文件内容如下: context.txt “并不是每个人都需要$(qunar)自己的粮食,$(f ...

  7. BFG

    "/"应用程序中的服务器错误. 配置错误 说明: 在处理向该请求提供服务所需的配置文件时出错.请检查下面的特定错误详细信息并适当地修改配置文件. 分析器错误消息: 提供程序集合中不 ...

  8. Extjs 3.0 htmleditor实现插入图片功能

    首先感谢前辈们的无私奉献.贴出参考地址 http://zhidao.baidu.com/link?url=Q0ZM405OFNy_xAHSut9TepRJxgXCxFayQttrQz1N82dlA1_ ...

  9. IOS开发:UIAlertView使用

    链接地址:http://www.2cto.com/kf/201307/231841.html UIAlertView是什么就不介绍了 1.基本用法 1 UIAlertView *view = [[UI ...

  10. django开发简易博客(一)

    这几篇博客是根据GoodSpeed的博客该写的,看了他的博客收获很大,但是他的博客从第三篇开始,条理很不清晰,加之又是几年之前写的,编写环境发生很大改变,所以对他的博客进行了一个整理,加入了一些自己的 ...