C++中對象&類的深入理解
什么是對象
任何事物都是一個對象, 也就是傳說中的萬物皆為對象.
對象的組成:
- 數據: 描述對象的屬性
- 函數: 描述對象的行為, 根據外界的信息進行相應操作的代碼
- 具有相同的屬性和行為的對象抽象為類 (class)
- 類是對象的抽象
- 對象則是類的特例
面向過程 vs 面向對象
面向過程
面向過程的設計:
- 圍繞功能, 用一個函數實現一個功能
- 程序 = 算法 +數據結構
- 算法和數據結構兩者互相獨立, 分開設計
面向對象
面向對象的設計:
- 把算法和數據封裝在一個對象中
- 設計所需要的歌者類和對象
- 向有關對象發(fā)送消息
- 對象 = 算法 + 數據結構
- 程序 = 對象*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 成員函數
使用內置函數只是影響編譯過程. 使用內置函數可以節(jié)省運行時間, 但卻增加了目標程序的長度:
內置函數:
- 一般只將規(guī)模很小而使用頻繁的函數聲明為內置函數
- 內置函數中不能包括復雜的控制語句, 如循環(huán)語句和 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;
}
總結
到此這篇關于C++中對象&類的文章就介紹到這了,更多相關C++對象&類內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

