Created
May 15, 2024 06:23
-
-
Save sdkfz181tiger/dcd9ba0519de14e1b33dbe338f3668d4 to your computer and use it in GitHub Desktop.
C/C++課題ネタ05_演算子オーバーロード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//========== | |
// 演算子オーバーロード | |
// 1, Compile | |
// g++ -std=c++17 -Wall -Wextra main.cpp | |
// 2, Run | |
// ./a.out | |
#include <math.h> | |
#include <stdio.h> | |
class Integer{ | |
public: | |
const int num; | |
Integer(int n): num(n){ | |
printf("Integer"); | |
} | |
Integer operator+(const Integer &rhs) const{ | |
return Integer(num + rhs.num); | |
} | |
Integer operator-(const Integer &rhs) const{ | |
return Integer(num - rhs.num); | |
} | |
Integer operator*(const Integer &rhs) const{ | |
return Integer(num * rhs.num); | |
} | |
Integer operator/(const Integer &rhs) const{ | |
return Integer(num / rhs.num); | |
} | |
Integer operator+() const{// 符号はそのまま | |
return *this; | |
} | |
Integer operator-() const{// 符号を反転させる | |
return Integer(-num); | |
} | |
}; | |
int main(){ | |
// Test | |
const Integer a = Integer(3); | |
const Integer b = Integer(2); | |
const Integer result = (a+b) * (a-b); | |
printf("result: %d\n", result.num); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//========== | |
// 演算子オーバーロード(インクリメント、デクリメント) | |
// 1, Compile | |
// g++ -std=c++17 -Wall -Wextra main.cpp | |
// 2, Run | |
// ./a.out | |
#include <math.h> | |
#include <stdio.h> | |
class Integer{ | |
public: | |
int num; | |
Integer(int n): num(n){ | |
printf("Integer"); | |
} | |
Integer operator+(const Integer &rhs) const{ | |
return Integer(num + rhs.num); | |
} | |
Integer operator-(const Integer &rhs) const{ | |
return Integer(num - rhs.num); | |
} | |
Integer operator*(const Integer &rhs) const{ | |
return Integer(num * rhs.num); | |
} | |
Integer operator/(const Integer &rhs) const{ | |
return Integer(num / rhs.num); | |
} | |
Integer operator+() const{// 符号はそのまま | |
return *this; | |
} | |
Integer operator-() const{// 符号を反転させる | |
return Integer(-num); | |
} | |
Integer& operator++(){// 前置インクリメント | |
++num;// 先にインクリメント | |
return *this; | |
} | |
Integer& operator--(){// 前置デクリメント | |
--num;// 先にデクリメント | |
return *this; | |
} | |
Integer operator++(int){ | |
auto tmp = *this;// 自身のコピー | |
++*this;// インクリメント | |
return tmp;// コピーしておいた変更前の値 | |
} | |
Integer operator--(int){ | |
auto tmp = *this;// 自身のコピー | |
--*this;// デクリメント | |
return tmp;// コピーしておいた変更前の値 | |
} | |
}; | |
int main(){ | |
// Test | |
Integer a = Integer(3); | |
Integer b = Integer(2); | |
const Integer result = (++a+b--) * (a-b); | |
printf("result: %d\n", result.num); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment