OOP-1

02 What is Object-Oriented

Objects=Attributes+Services

image-20220320213015024

Data

​ properties or status

Operations

​ functions

Comparision: C && C++

1
2
3
4
5
6
7
8
9
10
11
12
13
//C style
typedef struct point3d{
float x;
float y;
float z;
}Point3d;

void Point3d_print(const Point3d* pd);
Point3d a;
a.x = 1;
a.y = 2;
a.z = 3;
Point3d_print(&a);
1
2
3
4
5
6
7
8
9
10
11
12
13
//C++ style
class Point3d{
public:
Point3d(float x, float y, float z);
print();
private:
float x;
float y;
float z;
};

Point3d a(1,2,3);
a.print();

What is object-oriented

A way to organize Designs and Implementations

To focus on things, not operations.

03 Fundamental of OOP

Objects send and receive messages

image-20220320215335376Messages are:
-Composed by the sender
-Interperted by the receiver(重点:让接收者自己决定执行什么动作,不接触类的data)
-Implemented by the methods

Object vs. Class

image-20220320220933094

OOP Characteristics

  1. Everything is an object.
  2. A program is a brunch of objects telling each other what to do by sending messages.(WHAT, not HOW!)
  3. Each object has its own memory made up of other objects.
  4. Every object has a type.
  5. All objects of a particular type can receive the same messages.(反过来也成立,即可以接受相同消息的对象属于相同类型)

Functions of the Interface

-communication

-protection

OOP程序设计中希望实现松耦合

Encapsulation(封装)

Bundle data and methods dealing with these data together in an object.

Hide the details of the data and the actions.

Restrict only access to the publicized methods.

04 Example: Ticket Machine

[规范]

project: 使用全小写字母,如 ticketmachine

class: 首字母大写,如 TicketMachine

一些说明

每个类都应当包含.h.cpp两个文件

.h中声明,.cpp中定义

:: 称为域解析符


OOP-1
http://example.com/2022/02/20/OOP-1/
作者
Thunderbolt
发布于
2022年2月20日
许可协议