Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize operator overloading in C++

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to achieve operator overloading in C++. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article. Let's take a look at it with the editor.

The form of each operator overload:

Complex operator + (const Complex& c)

Complex operator-(const Complex& c)

Complex operator * (const Complex& c)

Complex operator / (const Complex& c)

Bool operator = (const Complex& c)

Bool operator! = (const Complex& c)

Complex& operator = (const Complex& c)

Code implementation:

Test.h header file:

# ifndef _ COMPLEX_H_

# define _ COMPLEX_H_

Class Complex

{

Double a

Double b

Public:

Complex (double a = 0, double b = 0)

Double getA ()

Double getB ()

Double getModulus ()

Complex operator + (const Complex& c)

Complex operator-(const Complex& c)

Complex operator * (const Complex& c)

Complex operator / (const Complex& c)

Bool operator = (const Complex& c)

Bool operator! = (const Complex& c)

Complex& operator = (const Complex& c)

}

# endif

Test.cpp file:

# include "Complex.h"

# include "math.h"

Complex::Complex (double a, double b)

{

This- > a = a

This- > b = b

}

Double Complex::getA ()

{

Return a

}

Double Complex::getB ()

{

Return b

}

Double Complex::getModulus ()

{

Return sqrt (a * a + b * b)

}

Complex Complex::operator + (const Complex& c)

{

Double na = a + c.a

Double nb = b + c.b

Complex ret (na, nb)

Return ret

}

Complex Complex::operator-(const Complex& c)

{

Double na = a-c.a

Double nb = b-c.b

Complex ret (na, nb)

Return ret

}

Complex Complex::operator * (const Complex& c)

{

Double na = a * c.a-b * c.b; / / the sum of real parts

Double nb = a * c.b + b * c.a; / / the addition of imaginary parts

Complex ret (na, nb)

Return ret

}

Complex Complex::operator / (const Complex& c)

{

Double cm = c.a * c.a + c.b * c.b

Double na = (a * c.a + b * c.b) / cm

Double nb = (b * c.a-a * c.b) / cm

Complex ret (na, nb)

Return ret

}

Bool Complex::operator = (const Complex& c)

{

Return (a = = c.a) & & (b = = c.b)

}

Bool Complex::operator! = (const Complex& c)

{

Return! (* this = = c)

}

Complex& Complex::operator = (const Complex& c)

{

If (this! = & c)

{

A = c.a

B = c.b

}

Return * this

}

Main.cpp file:

# include

# include "Complex.h"

Int main ()

{

Complex C1 (1,2)

Complex c2 (3,6)

Complex c3 = c2-C1

Complex c4 = C1 * c3

Complex c5 = c2 / C1

Printf ("c3.a =% f, c3.b =% f\ n", c3.getA (), c3.getB ())

Printf ("c4.a =% f, c4.b =% f\ n", c4.getA (), c4.getB ())

Printf ("c5.a =% f, c5.b =% f\ n", c5.getA (), c5.getB ())

Complex c6 (2,4)

Printf ("c3 = = c6:% d\ n", c3 = = c6)

Printf ("c3! = c4:% d\ n", c3! = c4)

(c3 = c2) = C1

Printf ("c1.a =% f, c1.b =% f\ n", c1.getA (), c1.getB ())

Printf ("c2.a =% f, c2.b =% f\ n", c2.getA (), c2.getB ())

Printf ("c3.a =% f, c3.b =% f\ n", c3.getA (), c3.getB ())

Return 0

}

Running result:

Root@txp-virtual-machine:/home/txp/tt#. / a.out

C3.a = 2.000000, c3.b = 4.000000

C4.a =-6.000000, c4.b = 8.000000

C5.a = 3.000000, c5.b = 0.000000

C3 = = c6: 1

C3! = c4: 1

C1.a = 1.000000, c1.b = 2.000000

C2.a = 3.000000, c2.b = 6.000000

C3.a = 1.000000, c3.b = 2.000000

Some mathematical knowledge is designed above, such as multiplication and division of complex numbers:

Multiplication: (a+bi) (c+di) = (ac-bd) + (bc+ad) I

Division: (a+bi) / (c+di) = (ac+bd) / c*c+d*d + (bc-ad) / c*c+d*d

3. Points for attention:

C++ stipulates that the assignment operator "=" can only be overloaded as member functions.

Operator overloading cannot change the priority of the original operator

Operator overloading cannot change the number of operands

Operator overloading should not change the original semantics of the operator

2. Summary:

The concept of plural can be implemented through custom classes.

Operations in the plural can be realized by operator overloading

Assignment operators can only be implemented through member functions

The essence of operator overloading is function definition.

The above is how to achieve operator overloading in C++. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report