C++ 什么是物件
- 什么是物件
- 面向程序 vs 面向物件
- 面向程序
- 面向物件
- 什么是類
- 類的格式
- 類的成員函式
- 函式訪問權限
- 方法一
- 方法二
- 方法三
- inline 成員函式
什么是物件
任何事物都是一個物件, 也就是傳說中的萬物皆為物件. 
物件的組成:
- 資料: 描述物件的屬性
- 函式: 描述物件的行為, 根據外界的資訊進行相應操作的代碼
- 具有相同的屬性和行為的物件抽象為類 (class)
- 類是物件的抽象
- 物件則是類的特例
面向程序 vs 面向物件
面向程序
面向程序的設計:
- 圍繞功能, 用一個函式實作一個功能
- 程式 = 演算法 +資料結構
- 演算法和資料結構兩者互相獨立, 分開設計
面向物件
面向物件的設計:
- 把演算法和資料封裝在一個物件中
- 設計所需要的歌者類和物件
- 向有關物件發送訊息
- 物件 = 演算法 + 資料結構
- 程式 = 物件*n + 訊息
什么是類
在 C++ 中, 用類來描述物件. 類屬于用戶自定的資料型別, 并且該型別的資料具有一定的行為能力, 也就是類中所描述的方法. 通常來說一個類的定義包含兩部分的內容, 一是該類的屬性, 二是該類的所擁有的方法.

類的格式
格式:
class 類名
{
public:
//公共的行為或屬性
private:
//私有的行為或屬性
};
例子:
main.cpp:
#include "Student.h"
using namespace std;
int main() {
Student student1(1, "Little white", 'f');
student1.display();
return 0;
}
Student.cpp:
#include "Student.h"
#include <iostream>
using namespace std;
Student::Student(int n, string p, char g) {
num = n;
name = p;
gender = g;
}
void Student::display() {
cout << "num: " << num << endl;
cout << "name: " << name << endl;
cout << "gender: " << gender << endl;
}
Student.h:
#ifndef PROJECT1_STUDENT_H
#define PROJECT1_STUDENT_H
#include <string>
using namespace std;
class Student {
private: // 私有成員
int num; // 學號
string name; // 名字
char gender; // 性別
public:
Student(int num, string name, char gender);
void display();
};
#endif //PROJECT1_STUDENT_H
輸出結果:
num: 1
name: Little white
gender: f
類的成員函式
類的成員函式是一個類的成員, 在類體重宣告.
注: 如果一個類中不包含成員函式, 就等同于 C 語言中的結構體了, 體現不出類在面向物件程式設計中的作用.
函式訪問權限
一般的做法: 講需要被外界呼叫的成員函式指定為 public, 它們是類的對外介面. (有的函式只被本類中的成員函式呼叫, 以支持其他的函式操作, 應該將它們制定為 private)
私有的成員函式只能被本類中的其他成員函式所呼叫, 而不能被類外呼叫. 成員函式可以訪問本類中任何成員 (包括私有和公用的), 可以參考在本作用域中有效的資料.
呼叫成員函式的權限:
- private: 私有的
- public: 公有的
- protected: 受保護的
訪問物件中成員的 3 種方法:
- 通過物件名和成員運算子訪問物件中的成員
- 通過指向物件的指標訪問物件中的成員
- 通過物件的參考變數訪問物件中的成員
方法一
通過物件名和成員運算子訪問物件中的成員.
Time 類:
#ifndef PROJECT1_TIME_H
#define PROJECT1_TIME_H
class Time {
private:
int hour;
int minute;
int second;
public:
void set_time(int h, int m, int s);
void show_time();
};
#endif //PROJECT1_TIME_H
main:
int main() {
Time time;
time.set_time(6, 6, 6);
time.show_time();
return 0;
}
輸出結果:
6:6:6
方法二
通過指向物件的指標訪問物件中的成員.
Time 類:
#ifndef PROJECT1_TIME_H
#define PROJECT1_TIME_H
class Time {
private:
int hour;
int minute;
int second;
public:
void set_time(int h, int m, int s);
void show_time();
};
#endif //PROJECT1_TIME_H
mian:
int main() {
Time time; // 實體化time
time.set_time(6, 6, 6); // 設定時間
Time *p = &time; // 定義指標, 指向time地址
p->show_time();
(*p).show_time();
return 0;
}
輸出結果:
6:6:6
6:6:6
方法三
通過物件的參考變數訪問物件中的成員.
參考變數共占同一段存盤單元. 實際上它們是同一個物件, 只是不同的面子表示而已.
Time 類:
#ifndef PROJECT1_TIME_H
#define PROJECT1_TIME_H
class Time {
private:
int hour;
int minute;
int second;
public:
void set_time(int h, int m, int s);
void show_time();
};
#endif //PROJECT1_TIME_H
mian:
int main() {
Time time1; // 實體化time
time1.set_time(6, 6, 6); // 設定時間
Time &time2 = time1;
time2.show_time();
return 0;
}
輸出結果:
6:6:6
inline 成員函式
使用內置函式只是影響編譯程序. 使用內置函式可以節省運行時間, 但卻增加了目標程式的長度:
內置函式:
- 一般只將規模很小而使用頻繁的函式宣告為內置函式
- 內置函式中不能包括復雜的控制陳述句, 如回圈陳述句和 switch 陳述句
- 對函式做 inline 宣告, 只是程式設計者對編譯系統提出的一個建議, 而不是指令性的
例子:
# include <iostream>
using namespace std;
inline int max(int, int, int);
int main() {
int i = 10, j = 20, k = 40, m;
m = max(i, j, k);
cout << "max= " << m << endl;
return 0;
}
inline int max(int a, int b, int c){
a = b > a ? b : a;
a = c > a ? c : a;
return a;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/282713.html
標籤:其他
上一篇:煙花
下一篇:java2實用教程小白入門(1)
