SlideShare a Scribd company logo
1 of 18
Download to read offline
3
int main()
{
Complex c1(10.0, 5.0), c2(4.0, 3.0);
Complex c3 = c1 + c2;
c1.print(); std::cout << " + ";
c2.print(); std::cout << " = ";
c3.print();
std::cout << std::endl;
}
4
#include <iostream>
class Complex
{
private:
double real;
double imaginary;
public:
Complex()
: real(0.0), imaginary(0.0) { }
Complex(double _real, double _imaginary)
: real(_real), imaginary(_imaginary) { }
void print() const;
};
void Complex::print() const
{
std::cout << real;
std::cout << "+";
std::cout << imaginary;
std::cout << "i";
}
5
error C2676: binary '+': 'Complex' does not
define this operator or a conversion to
a type acceptable to the predefined operator
Complex c3 = c1 + c2;
const Complex Complex::add(const Complex& rhs) const
{
return Complex(real + rhs.real, imaginary + rhs.imaginary);
}
const Complex add(const Complex& rhs) const;
6
Complex c1(10.0, 5.0), c2(4.0, 3.0);
Complex c3 = c1.add(c2);
Complex c1(10.0, 5.0), c2(4.0, 3.0);
Complex c3 = c1 + c2;
7
const Complex operator+(const Complex& rhs) const;
const Complex Complex::operator+(const Complex& rhs) const
{
return Complex(real + rhs.real, imaginary + rhs.imaginary);
}
Complex c1(10.0, 5.0), c2(4.0, 3.0);
Complex c3 = c1 + c2; // 14.0, 8.0
8
using tempC = std::pair<double, double>;
Complex(tempC c)
: real(c.first), imaginary(c.second) { }
Complex c4 = c3 + tempC(2.0, 4.0); // 16.0, 12.0
tempC(2.0, 4.0) Complex(2.0, 4.0) operator+(const Complex& rhs)
9
Complex c4 = c3 + tempC(2.0, 4.0); // 16.0, 12.0
Complex c5 = tempC(2.0, 4.0) + c3; // Error
10
const Complex operator+(const Complex& lhs, const Complex& rhs) {
Complex addedComplex;
addedComplex.real = lhs.real + rhs.real;
addedComplex.imaginary = lhs.imaginary + rhs.imaginary;
return addedComplex;
}
Complex c4 = c3 + tempC(2.0, 4.0); // 16.0, 12.0
Complex c5 = tempC(2.0, 4.0) + c3; // 16.0, 12.0
friend const Complex operator+(const Complex& lhs, const Complex& rhs);
11
friend const Complex operator-(const Complex& lhs, const Complex& rhs);
friend const Complex operator*(const Complex& lhs, const Complex& rhs);
const Complex operator-(const Complex& lhs, const Complex& rhs) {
Complex subtractedComplex;
subtractedComplex.real = lhs.real - rhs.real;
subtractedComplex.imaginary = lhs.imaginary - rhs.imaginary;
return subtractedComplex;
}
// (a, b)(c, d) = (ac-bd, ad+bc)
const Complex operator*(const Complex& lhs, const Complex& rhs) {
Complex multipliedComplex;
multipliedComplex.real = lhs.real * rhs.real - lhs.imaginary * rhs.imaginary;
multipliedComplex.imaginary = lhs.real * rhs.imaginary + lhs.imaginary * rhs.real;
return multipliedComplex;
}
12
friend const Complex operator/(const Complex& lhs, const Complex& rhs);
// If (c, d) is not equal (0, 0),
// (a, b)/(c, d) = ((ac+bd)/(c^2+d^2), (bc-ad)/(c^2+d^2))
const Complex operator/(const Complex& lhs, const Complex& rhs)
{
if (rhs.real == 0 && rhs.imaginary == 0)
throw std::invalid_argument("Divide by zero.");
Complex multipliedComplex;
multipliedComplex.real = lhs.real * rhs.real - lhs.imaginary * rhs.imaginary;
multipliedComplex.imaginary = lhs.real * rhs.imaginary + lhs.imaginary * rhs.real;
return multipliedComplex;
}
13
Complex& operator+=(const Complex& rhs);
Complex& operator-=(const Complex& rhs);
Complex& operator*=(const Complex& rhs);
Complex& operator/=(const Complex& rhs);
14
Complex& Complex::operator+=(const Complex& rhs)
{
real += rhs.real;
imaginary += rhs.imaginary;
return *this;
}
Complex& Complex::operator-=(const Complex& rhs)
{
real -= rhs.real;
imaginary -= rhs.imaginary;
return *this;
}
15
Complex& Complex::operator*=(const Complex& rhs)
{
Complex temp;
temp.real = real * rhs.real - imaginary * rhs.imaginary;
temp.imaginary = real * rhs.imaginary + imaginary * rhs.real;
real = temp.real;
imaginary = temp.imaginary;
return *this;
}
Complex& Complex::operator/=(const Complex& rhs)
{
if (rhs.real == 0 && rhs.imaginary == 0)
throw std::invalid_argument("Divide by zero.");
Complex temp;
temp.real = real * rhs.real - imaginary * rhs.imaginary;
temp.imaginary = real * rhs.imaginary + imaginary * rhs.real;
real = temp.real;
imaginary = temp.imaginary;
return *this;
}
16
17
Complex c6(3.0, 2.0), c7(8.0, 3.0);
c7 -= c6; // 5.0, 1.0
c7 += tempC(5.0, 4.0); // 10.0, 5.0
friend bool operator==(const Complex& lhs, const Complex& rhs);
bool operator==(const Complex& lhs, const Complex& rhs) {
return ((lhs.real == rhs.real) && (lhs.imaginary == rhs.imaginary));
}
http://en.cppreference.com/w/cpp/language/operators
http://en.cppreference.com/w/cpp/language/friend
http://www.github.com/utilForever/ModernCpp
18

More Related Content

What's hot

Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphismmohamed sikander
 
Travel management
Travel managementTravel management
Travel management1Parimal2
 
C++ Question on References and Function Overloading
C++ Question on References and Function OverloadingC++ Question on References and Function Overloading
C++ Question on References and Function Overloadingmohamed sikander
 
Understanding storage class using nm
Understanding storage class using nmUnderstanding storage class using nm
Understanding storage class using nmmohamed sikander
 
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13Chris Ohk
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++Bharat Kalia
 
Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))Alex Penso Romero
 
Writing good std::future&lt;c++>
Writing good std::future&lt;c++>Writing good std::future&lt;c++>
Writing good std::future&lt;c++>Anton Bikineev
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...ssuserd6b1fd
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...ssuserd6b1fd
 

What's hot (20)

Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphism
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
 
Implementing stack
Implementing stackImplementing stack
Implementing stack
 
Travel management
Travel managementTravel management
Travel management
 
C++ programs
C++ programsC++ programs
C++ programs
 
Cquestions
Cquestions Cquestions
Cquestions
 
C++ Question on References and Function Overloading
C++ Question on References and Function OverloadingC++ Question on References and Function Overloading
C++ Question on References and Function Overloading
 
Function basics
Function basicsFunction basics
Function basics
 
C++ file
C++ fileC++ file
C++ file
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Understanding storage class using nm
Understanding storage class using nmUnderstanding storage class using nm
Understanding storage class using nm
 
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
 
c programming
c programmingc programming
c programming
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 
Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))Programa en C++ ( escriba 3 números y diga cual es el mayor))
Programa en C++ ( escriba 3 números y diga cual es el mayor))
 
Writing good std::future&lt;c++>
Writing good std::future&lt;c++>Writing good std::future&lt;c++>
Writing good std::future&lt;c++>
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
 
C questions
C questionsC questions
C questions
 
c programming
c programmingc programming
c programming
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
 

Viewers also liked

[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기Chris Ohk
 
[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features SummaryChris Ohk
 
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기Chris Ohk
 
C++ Programming - 9th Study
C++ Programming - 9th StudyC++ Programming - 9th Study
C++ Programming - 9th StudyChris Ohk
 
C++ Programming - 8th Study
C++ Programming - 8th StudyC++ Programming - 8th Study
C++ Programming - 8th StudyChris Ohk
 
C++ Programming - 10th Study
C++ Programming - 10th StudyC++ Programming - 10th Study
C++ Programming - 10th StudyChris Ohk
 
C++ Programming - 13th Study
C++ Programming - 13th StudyC++ Programming - 13th Study
C++ Programming - 13th StudyChris Ohk
 
C++ Programming - 7th Study
C++ Programming - 7th StudyC++ Programming - 7th Study
C++ Programming - 7th StudyChris Ohk
 
C++ Programming - 12th Study
C++ Programming - 12th StudyC++ Programming - 12th Study
C++ Programming - 12th StudyChris Ohk
 
NDC 2015 박주은,최재혁 물리기반렌더링 지난1년간의 경험
NDC 2015 박주은,최재혁 물리기반렌더링 지난1년간의 경험NDC 2015 박주은,최재혁 물리기반렌더링 지난1년간의 경험
NDC 2015 박주은,최재혁 물리기반렌더링 지난1년간의 경험Jooeun Park
 
Data Structure - 1st Study
Data Structure - 1st StudyData Structure - 1st Study
Data Structure - 1st StudyChris Ohk
 
[Td 2015]디버깅, 어디까지 해봤니 당신이 아마도 몰랐을 디버깅 꿀팁 공개(김희준)
[Td 2015]디버깅, 어디까지 해봤니 당신이 아마도 몰랐을 디버깅 꿀팁 공개(김희준)[Td 2015]디버깅, 어디까지 해봤니 당신이 아마도 몰랐을 디버깅 꿀팁 공개(김희준)
[Td 2015]디버깅, 어디까지 해봤니 당신이 아마도 몰랐을 디버깅 꿀팁 공개(김희준)Sang Don Kim
 

Viewers also liked (12)

[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
 
[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary
 
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
[C++ Korea 3rd Seminar] 새 C++은 새 Visual Studio에, 좌충우돌 마이그레이션 이야기
 
C++ Programming - 9th Study
C++ Programming - 9th StudyC++ Programming - 9th Study
C++ Programming - 9th Study
 
C++ Programming - 8th Study
C++ Programming - 8th StudyC++ Programming - 8th Study
C++ Programming - 8th Study
 
C++ Programming - 10th Study
C++ Programming - 10th StudyC++ Programming - 10th Study
C++ Programming - 10th Study
 
C++ Programming - 13th Study
C++ Programming - 13th StudyC++ Programming - 13th Study
C++ Programming - 13th Study
 
C++ Programming - 7th Study
C++ Programming - 7th StudyC++ Programming - 7th Study
C++ Programming - 7th Study
 
C++ Programming - 12th Study
C++ Programming - 12th StudyC++ Programming - 12th Study
C++ Programming - 12th Study
 
NDC 2015 박주은,최재혁 물리기반렌더링 지난1년간의 경험
NDC 2015 박주은,최재혁 물리기반렌더링 지난1년간의 경험NDC 2015 박주은,최재혁 물리기반렌더링 지난1년간의 경험
NDC 2015 박주은,최재혁 물리기반렌더링 지난1년간의 경험
 
Data Structure - 1st Study
Data Structure - 1st StudyData Structure - 1st Study
Data Structure - 1st Study
 
[Td 2015]디버깅, 어디까지 해봤니 당신이 아마도 몰랐을 디버깅 꿀팁 공개(김희준)
[Td 2015]디버깅, 어디까지 해봤니 당신이 아마도 몰랐을 디버깅 꿀팁 공개(김희준)[Td 2015]디버깅, 어디까지 해봤니 당신이 아마도 몰랐을 디버깅 꿀팁 공개(김희준)
[Td 2015]디버깅, 어디까지 해봤니 당신이 아마도 몰랐을 디버깅 꿀팁 공개(김희준)
 

Similar to C++ Programming - 11th Study

Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2zindadili
 
Chainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたChainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたAkira Maruoka
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxssuser3cbb4c
 
C++20 the small things - Timur Doumler
C++20 the small things - Timur DoumlerC++20 the small things - Timur Doumler
C++20 the small things - Timur Doumlercorehard_by
 
C++ AssignmentPlease read all the requirements and the overloading.pdf
C++ AssignmentPlease read all the requirements and the overloading.pdfC++ AssignmentPlease read all the requirements and the overloading.pdf
C++ AssignmentPlease read all the requirements and the overloading.pdflakshmijewellery
 
M11 operator overloading and type conversion
M11 operator overloading and type conversionM11 operator overloading and type conversion
M11 operator overloading and type conversionNabeelaNousheen
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Geeks Anonymes
 
JavaScript - Agora nervoso
JavaScript - Agora nervosoJavaScript - Agora nervoso
JavaScript - Agora nervosoLuis Vendrame
 
Analysis of Microsoft Code Contracts
Analysis of Microsoft Code ContractsAnalysis of Microsoft Code Contracts
Analysis of Microsoft Code ContractsPVS-Studio
 
C multiple choice questions and answers pdf
C multiple choice questions and answers pdfC multiple choice questions and answers pdf
C multiple choice questions and answers pdfchoconyeuquy
 
programming in C++ report
programming in C++ reportprogramming in C++ report
programming in C++ reportvikram mahendra
 

Similar to C++ Programming - 11th Study (20)

Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
Cpp
Cpp Cpp
Cpp
 
Chainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみたChainer-Compiler 動かしてみた
Chainer-Compiler 動かしてみた
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
 
CppTutorial.ppt
CppTutorial.pptCppTutorial.ppt
CppTutorial.ppt
 
C++20 the small things - Timur Doumler
C++20 the small things - Timur DoumlerC++20 the small things - Timur Doumler
C++20 the small things - Timur Doumler
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
C++ AssignmentPlease read all the requirements and the overloading.pdf
C++ AssignmentPlease read all the requirements and the overloading.pdfC++ AssignmentPlease read all the requirements and the overloading.pdf
C++ AssignmentPlease read all the requirements and the overloading.pdf
 
M11 operator overloading and type conversion
M11 operator overloading and type conversionM11 operator overloading and type conversion
M11 operator overloading and type conversion
 
Cpp tutorial
Cpp tutorialCpp tutorial
Cpp tutorial
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
 
JavaScript - Agora nervoso
JavaScript - Agora nervosoJavaScript - Agora nervoso
JavaScript - Agora nervoso
 
Analysis of Microsoft Code Contracts
Analysis of Microsoft Code ContractsAnalysis of Microsoft Code Contracts
Analysis of Microsoft Code Contracts
 
C++ TUTORIAL 8
C++ TUTORIAL 8C++ TUTORIAL 8
C++ TUTORIAL 8
 
Beyond C++17
Beyond C++17Beyond C++17
Beyond C++17
 
C multiple choice questions and answers pdf
C multiple choice questions and answers pdfC multiple choice questions and answers pdf
C multiple choice questions and answers pdf
 
C++
C++C++
C++
 
programming in C++ report
programming in C++ reportprogramming in C++ report
programming in C++ report
 
Lecture5
Lecture5Lecture5
Lecture5
 

More from Chris Ohk

인프콘 2022 - Rust 크로스 플랫폼 프로그래밍
인프콘 2022 - Rust 크로스 플랫폼 프로그래밍인프콘 2022 - Rust 크로스 플랫폼 프로그래밍
인프콘 2022 - Rust 크로스 플랫폼 프로그래밍Chris Ohk
 
고려대학교 컴퓨터학과 특강 - 대학생 때 알았더라면 좋았을 것들
고려대학교 컴퓨터학과 특강 - 대학생 때 알았더라면 좋았을 것들고려대학교 컴퓨터학과 특강 - 대학생 때 알았더라면 좋았을 것들
고려대학교 컴퓨터학과 특강 - 대학생 때 알았더라면 좋았을 것들Chris Ohk
 
Momenti Seminar - 5 Years of RosettaStone
Momenti Seminar - 5 Years of RosettaStoneMomenti Seminar - 5 Years of RosettaStone
Momenti Seminar - 5 Years of RosettaStoneChris Ohk
 
선린인터넷고등학교 2021 알고리즘 컨퍼런스 - Rust로 알고리즘 문제 풀어보기
선린인터넷고등학교 2021 알고리즘 컨퍼런스 - Rust로 알고리즘 문제 풀어보기선린인터넷고등학교 2021 알고리즘 컨퍼런스 - Rust로 알고리즘 문제 풀어보기
선린인터넷고등학교 2021 알고리즘 컨퍼런스 - Rust로 알고리즘 문제 풀어보기Chris Ohk
 
Momenti Seminar - A Tour of Rust, Part 2
Momenti Seminar - A Tour of Rust, Part 2Momenti Seminar - A Tour of Rust, Part 2
Momenti Seminar - A Tour of Rust, Part 2Chris Ohk
 
Momenti Seminar - A Tour of Rust, Part 1
Momenti Seminar - A Tour of Rust, Part 1Momenti Seminar - A Tour of Rust, Part 1
Momenti Seminar - A Tour of Rust, Part 1Chris Ohk
 
Evolving Reinforcement Learning Algorithms, JD. Co-Reyes et al, 2021
Evolving Reinforcement Learning Algorithms, JD. Co-Reyes et al, 2021Evolving Reinforcement Learning Algorithms, JD. Co-Reyes et al, 2021
Evolving Reinforcement Learning Algorithms, JD. Co-Reyes et al, 2021Chris Ohk
 
Adversarially Guided Actor-Critic, Y. Flet-Berliac et al, 2021
Adversarially Guided Actor-Critic, Y. Flet-Berliac et al, 2021Adversarially Guided Actor-Critic, Y. Flet-Berliac et al, 2021
Adversarially Guided Actor-Critic, Y. Flet-Berliac et al, 2021Chris Ohk
 
Agent57: Outperforming the Atari Human Benchmark, Badia, A. P. et al, 2020
Agent57: Outperforming the Atari Human Benchmark, Badia, A. P. et al, 2020Agent57: Outperforming the Atari Human Benchmark, Badia, A. P. et al, 2020
Agent57: Outperforming the Atari Human Benchmark, Badia, A. P. et al, 2020Chris Ohk
 
Proximal Policy Optimization Algorithms, Schulman et al, 2017
Proximal Policy Optimization Algorithms, Schulman et al, 2017Proximal Policy Optimization Algorithms, Schulman et al, 2017
Proximal Policy Optimization Algorithms, Schulman et al, 2017Chris Ohk
 
Trust Region Policy Optimization, Schulman et al, 2015
Trust Region Policy Optimization, Schulman et al, 2015Trust Region Policy Optimization, Schulman et al, 2015
Trust Region Policy Optimization, Schulman et al, 2015Chris Ohk
 
Continuous Control with Deep Reinforcement Learning, lillicrap et al, 2015
Continuous Control with Deep Reinforcement Learning, lillicrap et al, 2015Continuous Control with Deep Reinforcement Learning, lillicrap et al, 2015
Continuous Control with Deep Reinforcement Learning, lillicrap et al, 2015Chris Ohk
 
GDG Gwangju DevFest 2019 - <하스스톤> 강화학습 환경 개발기
GDG Gwangju DevFest 2019 - <하스스톤> 강화학습 환경 개발기GDG Gwangju DevFest 2019 - <하스스톤> 강화학습 환경 개발기
GDG Gwangju DevFest 2019 - <하스스톤> 강화학습 환경 개발기Chris Ohk
 
[RLKorea] <하스스톤> 강화학습 환경 개발기
[RLKorea] <하스스톤> 강화학습 환경 개발기[RLKorea] <하스스톤> 강화학습 환경 개발기
[RLKorea] <하스스톤> 강화학습 환경 개발기Chris Ohk
 
[NDC 2019] 하스스톤 강화학습 환경 개발기
[NDC 2019] 하스스톤 강화학습 환경 개발기[NDC 2019] 하스스톤 강화학습 환경 개발기
[NDC 2019] 하스스톤 강화학습 환경 개발기Chris Ohk
 
C++20 Key Features Summary
C++20 Key Features SummaryC++20 Key Features Summary
C++20 Key Features SummaryChris Ohk
 
[델리만주] 대학원 캐슬 - 석사에서 게임 프로그래머까지
[델리만주] 대학원 캐슬 - 석사에서 게임 프로그래머까지[델리만주] 대학원 캐슬 - 석사에서 게임 프로그래머까지
[델리만주] 대학원 캐슬 - 석사에서 게임 프로그래머까지Chris Ohk
 
디미고 특강 - 개발을 시작하려는 여러분에게
디미고 특강 - 개발을 시작하려는 여러분에게디미고 특강 - 개발을 시작하려는 여러분에게
디미고 특강 - 개발을 시작하려는 여러분에게Chris Ohk
 
청강대 특강 - 프로젝트 제대로 해보기
청강대 특강 - 프로젝트 제대로 해보기청강대 특강 - 프로젝트 제대로 해보기
청강대 특강 - 프로젝트 제대로 해보기Chris Ohk
 
[NDC 2018] 유체역학 엔진 개발기
[NDC 2018] 유체역학 엔진 개발기[NDC 2018] 유체역학 엔진 개발기
[NDC 2018] 유체역학 엔진 개발기Chris Ohk
 

More from Chris Ohk (20)

인프콘 2022 - Rust 크로스 플랫폼 프로그래밍
인프콘 2022 - Rust 크로스 플랫폼 프로그래밍인프콘 2022 - Rust 크로스 플랫폼 프로그래밍
인프콘 2022 - Rust 크로스 플랫폼 프로그래밍
 
고려대학교 컴퓨터학과 특강 - 대학생 때 알았더라면 좋았을 것들
고려대학교 컴퓨터학과 특강 - 대학생 때 알았더라면 좋았을 것들고려대학교 컴퓨터학과 특강 - 대학생 때 알았더라면 좋았을 것들
고려대학교 컴퓨터학과 특강 - 대학생 때 알았더라면 좋았을 것들
 
Momenti Seminar - 5 Years of RosettaStone
Momenti Seminar - 5 Years of RosettaStoneMomenti Seminar - 5 Years of RosettaStone
Momenti Seminar - 5 Years of RosettaStone
 
선린인터넷고등학교 2021 알고리즘 컨퍼런스 - Rust로 알고리즘 문제 풀어보기
선린인터넷고등학교 2021 알고리즘 컨퍼런스 - Rust로 알고리즘 문제 풀어보기선린인터넷고등학교 2021 알고리즘 컨퍼런스 - Rust로 알고리즘 문제 풀어보기
선린인터넷고등학교 2021 알고리즘 컨퍼런스 - Rust로 알고리즘 문제 풀어보기
 
Momenti Seminar - A Tour of Rust, Part 2
Momenti Seminar - A Tour of Rust, Part 2Momenti Seminar - A Tour of Rust, Part 2
Momenti Seminar - A Tour of Rust, Part 2
 
Momenti Seminar - A Tour of Rust, Part 1
Momenti Seminar - A Tour of Rust, Part 1Momenti Seminar - A Tour of Rust, Part 1
Momenti Seminar - A Tour of Rust, Part 1
 
Evolving Reinforcement Learning Algorithms, JD. Co-Reyes et al, 2021
Evolving Reinforcement Learning Algorithms, JD. Co-Reyes et al, 2021Evolving Reinforcement Learning Algorithms, JD. Co-Reyes et al, 2021
Evolving Reinforcement Learning Algorithms, JD. Co-Reyes et al, 2021
 
Adversarially Guided Actor-Critic, Y. Flet-Berliac et al, 2021
Adversarially Guided Actor-Critic, Y. Flet-Berliac et al, 2021Adversarially Guided Actor-Critic, Y. Flet-Berliac et al, 2021
Adversarially Guided Actor-Critic, Y. Flet-Berliac et al, 2021
 
Agent57: Outperforming the Atari Human Benchmark, Badia, A. P. et al, 2020
Agent57: Outperforming the Atari Human Benchmark, Badia, A. P. et al, 2020Agent57: Outperforming the Atari Human Benchmark, Badia, A. P. et al, 2020
Agent57: Outperforming the Atari Human Benchmark, Badia, A. P. et al, 2020
 
Proximal Policy Optimization Algorithms, Schulman et al, 2017
Proximal Policy Optimization Algorithms, Schulman et al, 2017Proximal Policy Optimization Algorithms, Schulman et al, 2017
Proximal Policy Optimization Algorithms, Schulman et al, 2017
 
Trust Region Policy Optimization, Schulman et al, 2015
Trust Region Policy Optimization, Schulman et al, 2015Trust Region Policy Optimization, Schulman et al, 2015
Trust Region Policy Optimization, Schulman et al, 2015
 
Continuous Control with Deep Reinforcement Learning, lillicrap et al, 2015
Continuous Control with Deep Reinforcement Learning, lillicrap et al, 2015Continuous Control with Deep Reinforcement Learning, lillicrap et al, 2015
Continuous Control with Deep Reinforcement Learning, lillicrap et al, 2015
 
GDG Gwangju DevFest 2019 - <하스스톤> 강화학습 환경 개발기
GDG Gwangju DevFest 2019 - <하스스톤> 강화학습 환경 개발기GDG Gwangju DevFest 2019 - <하스스톤> 강화학습 환경 개발기
GDG Gwangju DevFest 2019 - <하스스톤> 강화학습 환경 개발기
 
[RLKorea] <하스스톤> 강화학습 환경 개발기
[RLKorea] <하스스톤> 강화학습 환경 개발기[RLKorea] <하스스톤> 강화학습 환경 개발기
[RLKorea] <하스스톤> 강화학습 환경 개발기
 
[NDC 2019] 하스스톤 강화학습 환경 개발기
[NDC 2019] 하스스톤 강화학습 환경 개발기[NDC 2019] 하스스톤 강화학습 환경 개발기
[NDC 2019] 하스스톤 강화학습 환경 개발기
 
C++20 Key Features Summary
C++20 Key Features SummaryC++20 Key Features Summary
C++20 Key Features Summary
 
[델리만주] 대학원 캐슬 - 석사에서 게임 프로그래머까지
[델리만주] 대학원 캐슬 - 석사에서 게임 프로그래머까지[델리만주] 대학원 캐슬 - 석사에서 게임 프로그래머까지
[델리만주] 대학원 캐슬 - 석사에서 게임 프로그래머까지
 
디미고 특강 - 개발을 시작하려는 여러분에게
디미고 특강 - 개발을 시작하려는 여러분에게디미고 특강 - 개발을 시작하려는 여러분에게
디미고 특강 - 개발을 시작하려는 여러분에게
 
청강대 특강 - 프로젝트 제대로 해보기
청강대 특강 - 프로젝트 제대로 해보기청강대 특강 - 프로젝트 제대로 해보기
청강대 특강 - 프로젝트 제대로 해보기
 
[NDC 2018] 유체역학 엔진 개발기
[NDC 2018] 유체역학 엔진 개발기[NDC 2018] 유체역학 엔진 개발기
[NDC 2018] 유체역학 엔진 개발기
 

C++ Programming - 11th Study

  • 1.
  • 2.
  • 3. 3
  • 4. int main() { Complex c1(10.0, 5.0), c2(4.0, 3.0); Complex c3 = c1 + c2; c1.print(); std::cout << " + "; c2.print(); std::cout << " = "; c3.print(); std::cout << std::endl; } 4 #include <iostream> class Complex { private: double real; double imaginary; public: Complex() : real(0.0), imaginary(0.0) { } Complex(double _real, double _imaginary) : real(_real), imaginary(_imaginary) { } void print() const; }; void Complex::print() const { std::cout << real; std::cout << "+"; std::cout << imaginary; std::cout << "i"; }
  • 5. 5 error C2676: binary '+': 'Complex' does not define this operator or a conversion to a type acceptable to the predefined operator Complex c3 = c1 + c2; const Complex Complex::add(const Complex& rhs) const { return Complex(real + rhs.real, imaginary + rhs.imaginary); } const Complex add(const Complex& rhs) const;
  • 6. 6 Complex c1(10.0, 5.0), c2(4.0, 3.0); Complex c3 = c1.add(c2); Complex c1(10.0, 5.0), c2(4.0, 3.0); Complex c3 = c1 + c2;
  • 7. 7 const Complex operator+(const Complex& rhs) const; const Complex Complex::operator+(const Complex& rhs) const { return Complex(real + rhs.real, imaginary + rhs.imaginary); } Complex c1(10.0, 5.0), c2(4.0, 3.0); Complex c3 = c1 + c2; // 14.0, 8.0
  • 8. 8 using tempC = std::pair<double, double>; Complex(tempC c) : real(c.first), imaginary(c.second) { } Complex c4 = c3 + tempC(2.0, 4.0); // 16.0, 12.0 tempC(2.0, 4.0) Complex(2.0, 4.0) operator+(const Complex& rhs)
  • 9. 9 Complex c4 = c3 + tempC(2.0, 4.0); // 16.0, 12.0 Complex c5 = tempC(2.0, 4.0) + c3; // Error
  • 10. 10 const Complex operator+(const Complex& lhs, const Complex& rhs) { Complex addedComplex; addedComplex.real = lhs.real + rhs.real; addedComplex.imaginary = lhs.imaginary + rhs.imaginary; return addedComplex; } Complex c4 = c3 + tempC(2.0, 4.0); // 16.0, 12.0 Complex c5 = tempC(2.0, 4.0) + c3; // 16.0, 12.0 friend const Complex operator+(const Complex& lhs, const Complex& rhs);
  • 11. 11 friend const Complex operator-(const Complex& lhs, const Complex& rhs); friend const Complex operator*(const Complex& lhs, const Complex& rhs); const Complex operator-(const Complex& lhs, const Complex& rhs) { Complex subtractedComplex; subtractedComplex.real = lhs.real - rhs.real; subtractedComplex.imaginary = lhs.imaginary - rhs.imaginary; return subtractedComplex; } // (a, b)(c, d) = (ac-bd, ad+bc) const Complex operator*(const Complex& lhs, const Complex& rhs) { Complex multipliedComplex; multipliedComplex.real = lhs.real * rhs.real - lhs.imaginary * rhs.imaginary; multipliedComplex.imaginary = lhs.real * rhs.imaginary + lhs.imaginary * rhs.real; return multipliedComplex; }
  • 12. 12 friend const Complex operator/(const Complex& lhs, const Complex& rhs); // If (c, d) is not equal (0, 0), // (a, b)/(c, d) = ((ac+bd)/(c^2+d^2), (bc-ad)/(c^2+d^2)) const Complex operator/(const Complex& lhs, const Complex& rhs) { if (rhs.real == 0 && rhs.imaginary == 0) throw std::invalid_argument("Divide by zero."); Complex multipliedComplex; multipliedComplex.real = lhs.real * rhs.real - lhs.imaginary * rhs.imaginary; multipliedComplex.imaginary = lhs.real * rhs.imaginary + lhs.imaginary * rhs.real; return multipliedComplex; }
  • 13. 13 Complex& operator+=(const Complex& rhs); Complex& operator-=(const Complex& rhs); Complex& operator*=(const Complex& rhs); Complex& operator/=(const Complex& rhs);
  • 14. 14 Complex& Complex::operator+=(const Complex& rhs) { real += rhs.real; imaginary += rhs.imaginary; return *this; } Complex& Complex::operator-=(const Complex& rhs) { real -= rhs.real; imaginary -= rhs.imaginary; return *this; }
  • 15. 15 Complex& Complex::operator*=(const Complex& rhs) { Complex temp; temp.real = real * rhs.real - imaginary * rhs.imaginary; temp.imaginary = real * rhs.imaginary + imaginary * rhs.real; real = temp.real; imaginary = temp.imaginary; return *this; }
  • 16. Complex& Complex::operator/=(const Complex& rhs) { if (rhs.real == 0 && rhs.imaginary == 0) throw std::invalid_argument("Divide by zero."); Complex temp; temp.real = real * rhs.real - imaginary * rhs.imaginary; temp.imaginary = real * rhs.imaginary + imaginary * rhs.real; real = temp.real; imaginary = temp.imaginary; return *this; } 16
  • 17. 17 Complex c6(3.0, 2.0), c7(8.0, 3.0); c7 -= c6; // 5.0, 1.0 c7 += tempC(5.0, 4.0); // 10.0, 5.0 friend bool operator==(const Complex& lhs, const Complex& rhs); bool operator==(const Complex& lhs, const Complex& rhs) { return ((lhs.real == rhs.real) && (lhs.imaginary == rhs.imaginary)); }