Fundamentals of Computer Programming 2 Notes and Practice Problems – August 13, 2003

Instructor: Ravi Kant Ryerson 177 e-mail: ravikant@cs.uchicago.edu Office Hours : Fri : 1:00PM - 3:00PM (or by appointment)

Classes

Class classname {
   private :

data and function declarations

   public :

data and function declarations }

• private members (functions and data) cannot be accessed from outside the class.

• public members have access to both the private members and other public members.

• Only the public members can be accessed from outside the class definition.

Practice problems :

1. For the class “complex” defined in the examples write the following member functions :

(a) double abs() which calculates the absolute value of the complex number ( abs(x + iy) = \sqrt{x^{2} + y^{2}}).

(b) double reciprocal() which calculates the reciprocal (1/(x+iy)) for the complex number x + iy.

2. Write the following functions for doing complex arithmetic

     (a) complex product(complex z1, complex z2) for calculating the

product of the 2 complex numbers z1 and z2.

     (b) complex ratio(complex z1, complex z2) for calculating z1/z2.
      (c) complex pow(complex z1, complex z2) for calculating z1^{z2}.