Programming and Graphics

(Kiana) #1

6.10 Pointers to class members 173


prints on the screen:


0.1 0.2 0.3
0.9 1.2 5.3

Problems


6.9.1.Overload the multiplication operator for the algebra class.


6.9.2.Demonstrate by example the action of the ++ operator and the twice
overloaded + operator for the algebra class discussed in the text.


6.9.3.(a) Overload the|operator for the circles discussed in Section 6.7, so
that the result is a Boolean variable that is true if the areas of two circles
are the same, and false otherwise. (b) Repeat for the squares.


6.10 Pointers to class members


In Chapter 5, we discussed pointers to scalar variables and various data types
including vectors and matrices. A class defines a new data type whose members
can also be identified with pointers encapsulating the addresses of memory cells
identifying the objects.


As an example, we introduce the algebra class and state in the main
program:


algebraD=algebra(-9.45);
algebra * pnt = &D;
algebraE=*pnt;
D.print();
E.print();

The first line defines object D; the second line defines a pointer to D; the third
line identifies point E with point D stated as the content of the memory space
identified by the pointer pnt; the last two lines print D and E.


The output of the code is:


-9.45 red
-9.45 red

Instead of operating on point D with a function, we can operate on its
pointer. Thus, in the above example, the statement:


D.print();
Free download pdf