c++入門 (什麼是面向對象編程,理論)

oop的原則

1. 對象是data + operation.  外部對象不能直接訪問對象的data,可以調用對象的operation.

2. 對象是Entity, is an variable in programing language.

3. 面向對象 is a way to designs and implements.  Objects not control and not data flow, are the primary focus of designs and implements.關注點是things,不是操作

4. Objects send and receive messages. 對象之間互相發送消息,而不是去訪問對方的數據。

5. Messages are

Composed by the sender. 由發送者組裝

Interpreted by the receiver. 由接受者解釋

Implemented by the methods. 由方法來實現


May cause receiver to change state. 或許導致接受者狀態改變。

May return results. 或許有返回值。


OOP characteristics  OOP的特點


1.Everything is an object.          萬物是對象
2.A program is a bunch of objects telling each other what
to do by sending messages.               一個程序就是一堆對象之間互相發送消息。
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. 同一種類型的對象可以接受同樣的消息。


An object has an interface       所有的對象都有一個接口
1.The interface is the way it receives messages.     接口就是對象接受消息的方式
2.It is defined in the class the object belong to.        它在對象的類型裏面被定義。

3. 接口的作用:communication and protection   通訊和保護



c vs c++    procedural language  vs oop

c doesn't support relationship btw data and functions  c語言不支持數據和函數之間的關係

c version (c語言的版本):

typedef struct p3d{
float x;
float y;
float z;
}P3d;
void P3d_print(const P3d* p3d);
P3d a;
a.x=1;a.y=2;a.z=3;
P3d_print(&a);

c++ version (c++的版本):

class P3d{
public:
P3d(float x, float y, float z);
print();
private:
float x;
float y;
float z;
};
P3d a(1,2,3);
a.print();


C++最早的版本就是在struct的基礎上擴充出class

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章