03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

68 CHAPTER 3. CLASSES<br />

int main()<br />

{<br />

complex c1, c2;<br />

// set c1<br />

c1.set r(3.0);<br />

c1.set i(2.0);<br />

// copy c1 to c2<br />

c2.set r(c1.get r());<br />

c2.set i(c1.get i());<br />

return 0;<br />

}<br />

In line 3 we created two objects of type complex. Then we set one of the objects and copied it<br />

to the other one. This works but it is a bit clumsy, isn’t it?<br />

C ++ provides another keyword <strong>for</strong> defining classes: struct. The only difference 2 is that members<br />

are by default public, there<strong>for</strong>e the example above is equivalent to:<br />

struct complex<br />

{<br />

double get r() { return r; }<br />

void set r(double newr) { r = newr; }<br />

double get i() { return i; }<br />

void set i(double newi) { i = newi; }<br />

private:<br />

double r, i;<br />

};<br />

Our member variables can only be accessed via functions. This gives the class designer the<br />

maximal control over the behavior. The setter could only accept values in a certain range. We<br />

could count how often the setter and getter is called <strong>for</strong> each complex number or <strong>for</strong> all complex<br />

numbers in the execution. The functions could have additional print-outs <strong>for</strong> debugging. 3 We<br />

could even allow the reading only at certain times of the day or writing only if the program runs<br />

on a computer with a certain IP. We will most likely not do the latter, at least not <strong>for</strong> complex<br />

numbers, but we could. If the variables are public and accessed directly, such modifications<br />

would not be possible. Nevertheless, handling the real and imaginary part of a complex number<br />

is cumbersome and we will discuss alternatives.<br />

Most C ++ programmer would not implement it this way. What would a C ++ programmer do<br />

first then? Writing constructors.<br />

3.3 Constructors<br />

What are constructors? Constructors initialize objects of classes and create a working environment<br />

<strong>for</strong> member functions. Sometimes such an environment includes resources like files,<br />

memory or locks that have to be freed after use. We come back to this later.<br />

To start with let us define a constructor <strong>for</strong> complex:<br />

2 There is really no other difference. One can define operators and virtual functions or derived classes in the<br />

same manner as with class. Per<strong>for</strong>mance of class and struct is also absolutely identical.<br />

3 A debugger is usually a better alternative to putting print-outs into programs.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!