Class Features, Lab

Tom Kelliher, CS23

Mar. 7, 1997

Default Arguments

Use:

Complex x;
Complex y(5.0);
Complex z(-3.0, 2.0);

Constructor declaration:

Complex(float real = 0.0, float imag = 0.0);

Constructor definition:

Complex::Complex(float real, float imag)
{
   ...
}

  1. Default arguments can be used with any member function.

  2. Give appearance of different versions of same function --- overloading.

Function Overloading

Use:

Complex x(1.0);
Complex y(3.0, 4.0);

x.Add(y);
x.Add(3.0);

Member function declarations:

void Add(complex y);
void Add(float y);

  1. Overloaded functions must have unique signatures.

  2. Default arguments vs. function overloading.

Copy Constructor

Not important now, just included so you can see calls to copy constructor.

Accessing Data Members Within Methods

Add use:

Complex x(1.0, 2.0);
Complex y(3.0, 4.0);

x.Add(y);

Class declaration:

 private:
   float re;
   float im;

Add definition:

void Complex::Add(Complex y)
{
   re += y.re;
   im += y.im;
}

  1. Accessing members of object method called on.

  2. Accessing members of other objects.

  3. Invisible argument: this.



Thomas P. Kelliher
Thu Mar 6 17:10:52 EST 1997
Tom Kelliher