第十一章運算子過載

2021-09-08 23:56:36 字數 1840 閱讀 1127

//1 運算子過載

#include using namespace std;

class num

~num(){}

int get()const

void set(int x)

private:

int n;

};int main()

~num(){}

int get()const

void set(int x)

void add()

private:

int n;

};int main()

~num(){}

int get()const

void set(int x)

void add()

void operator++()

private:

int n;

};int main()

~num(){}

int get()const

void set(int x)

void add()

void operator++()

private:

int n;

};int main()

~num(){}

int get()const

void set(int x)

void add()

num operator++()

private:

int n;

};int main()

private:

int n;

};int main()

private:

int n;

};int main()

num operator++()

//過載後執自加運算子

const num operator++(int o)

private:

int n;

};int main()

//過載+運算子,對兩個對像進行相加操作

const num operator+(const num &x)

const num operator-(const num &x)

num operator++()

//過載後執自加運算子

const num operator++(int o)

private:

int n;

};int main()

//過載+運算子,對兩個對像進行相加操作

const num operator+(const num &x)

const num operator-(const num &x)

const num equal(const num&x)

const num operator=(const num&x)

cout<<"operator=函式在呼叫"return *this;

} num operator++()

//過載後執自加運算子

const num operator++(int o)

//賦值=運算子過載

private:

int n;

};int main()

~a()

void get()

operator int()

private:

int i;

};struct b

//過載運算子int,可以將類進行型別換轉為int型

operator int() const

}; int main()

(一二二)運算子過載 第十一章

本章重點是類設計技術,而不是通用原理。c 的重點是 多使用。運算子過載是一種形式的c 多型 就像函式過載那樣,同名但呼叫不同的函式 要過載運算子,需要使用被稱為運算子函式的特殊函式形式。運算子函式格式如下 operator 運算子 argument list 例如 operator 表示過載加號運算...

C 程式語言 第十一章 運算子過載

1 運算子函式 運算子函式的名字是由關鍵字operator後跟對應的運算子構成。二元運算子可以定義為取乙個引數的非靜態成員函式,也可以定義為去兩個引數的非成員函式。乙個運算子函式必須或者是乙個成員函式,或者至少有乙個使用者定義型別的引數。不存在運算子遮蔽,這就保證了內部運算子可以用,並且為運算子定義...

第十一周 實現類中的運算子過載

檔名稱 text.cpp 完成日期 2015年5月16日 版本號 v1.0 問題描述 請用類的成員函式定義複數類過載運算子 使之能用於複數的加減乘除 輸入描述 無 程式輸出 複數類加減乘除之後的結果 include using namespace std class complex complex ...