0

In the book The C++ Programming Language, the author claims that, for the following class design,

class complex{
   double re, im; 
public: 
   double real( ) const { return re;}
   double imag( ) const { return im;}

};

Given real() and imag(), we can define all kinds of useful operators without granting them direct access to the representation of complex.

How to understand this statement? Where do we need direct access and where do we need indirect access?

The author also gives the following example.

inline bool operator==(complex a, complex b)
{ 
   return a.real( )==b.real() && a.imag () ==b.imag( );
}

How is this given example related to the author's statement given in the above.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
user630983
  • 947
  • 2
  • 9
  • 16
  • 4
    That's a bad example (which suggests that it may also be a bad book) - you should never test floating point values for equality using `==`. – Paul R Mar 08 '11 at 15:21
  • @Paul - huh ? How to test if two `double`-s are equal ? – Kiril Kirov Mar 08 '11 at 15:24
  • 1
    @Kiril: you need to test whether the absolute difference is smaller than epsilon, e.g. `fabs(a.real - b.real) < EPSILON`. See http://stackoverflow.com/questions/17333/most-effective-way-for-float-and-double-comparison – Paul R Mar 08 '11 at 15:29
  • @Paul - right, thanks, I usually do it like this(`fabs`), but not always and just wandered if there's a "smarter" way (: Thanks (y) – Kiril Kirov Mar 08 '11 at 15:34

6 Answers6

1

what the author is talking about here is known as "encapsulation". The new function that defines "==" defines the logic for comparing two objects of your new type (complex) without having to know what the internals of a complex number actually look like.

Your question is really vague, so i think what you want to do is learn a little more about "object oriented programming" and "encapsulation". A google search on these terms should help.

in fact, this might be a good place to start.

Ramy
  • 20,541
  • 41
  • 103
  • 153
0

It is related in that you don't access re and im directly, but, instead access them through access functions real() and imag().

If the function were to be written like this:

return a.re==b.re && a.im==b.im;

then it had to be a friend function (need access to private members).

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
0

The author is trying to illustrate the advantage of not exposing the actual implementation details of the class complex. At any time you can change the actual underlying implementation, without needing any other code to change, so long as those 2 methods always do what the user expects them to.

Mark Loeser
  • 17,657
  • 2
  • 26
  • 34
0

This means, that you don't provide access to the real members of Complex, as re and im are private and the getters (accessors) return a copy of the value. That's called encapsulation.

The example shows how to compare 2 Complex objects, without touching the private data members. (otherwise, if you make the re and im public, anybody can change them anytime, which destroys one of the main goals of OOP - as I said, encapsulation).

Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
0

Since operator== is a free function, which is not even friend of the class, so it cannot acess the private members of the class, but it has access to public member functions of complex, so it can implement operator== with the help of public member functions. That is what meant by the author. This is also a good demonstration of data encapsulation by the author!

By the way, the implementation can be further improved by defining as,

inline bool operator==(const complex & a,const complex & b) //note difference!
{ 
   return a.real( )==b.real() && a.imag () ==b.imag( );
}

Please notice the parameters: now, they're defined as reference so as to avoid unnecessary copy!

Nawaz
  • 353,942
  • 115
  • 666
  • 851
0

The operator== checks whether two complex numbers are equal through their real/imag interface, without touching the re and im members directly.

This means that

  1. a function receiving a complex & cannot change the members because they are effectively const (only reachable through const accessors), and
  2. you can later change the complex class to store numbers in polar form instead, without breaking anything but the real and imag members.

This is called "encapsulation" or "data hiding".

Fred Foo
  • 355,277
  • 75
  • 744
  • 836