Lecture 4

面向对象就是把数据和结构绑定起来,通过函数调用实现用户与数据的通信,实现操作的同时保护数据。
Objects = Attributes + Service
  • Data: the properties or status
  • Operations: the functions
notion image
simple e.g.

Class


Object 与 Class 的关系:
notion image

Definition of Class

  • In C++, separated header and source files are used to define one class.
  • Class declaration and member function prototypes are in the .h header file.
  • All the function bodies (implementations) are in the .cpp source file.
  • PImpl technique: debatable, hides private members and removes compilation dependency

Standard Way


  • 函数声明放在 header .h 中,相当于接口
  • 主程序和函数实现放在 .cpp 文件中
  • 编译器只能看到 .cpp 文件(a compile unit),每个 .cpp编译生成一个 .obj 文件;链接器把所有 .obj 文件链接成一个可执行文件
notion image

Standard header file structure

防止重复定义

OOP


OOP Characteristic

notion image

Object-oriented programming

notion image

Encapsulation

  • Bundle data and methods together
  • Hide the details of dealing with the data
  • Restrict access only to the publicized methods

Abstraction

  • Abstraction is the ability to ignore details of parts to focus on high-level problems.
  • Modularization is the process of dividing a whole into parts that can be built separately and interact in well-defined ways.
 
Loading...