CIS 540/543

C++ Lab 3: Overloading, Headers, & Makefiles

Overloading

Overloading allows us to ease the usage of certain functions. It is more intuitive to say:

A+B

then

A.add(B); or A->add(B);

Overloading also allows acceptance of multiple parameters to the same function:

A(int a){…}

A(int a, int b){…}

If we want to overload functions, we simply define a second one with different parameter types/quantities. YOU CANNOT HAVE:

Void squiggle(int length, int width){…}

Void squiggle(int length, int numberOfLines){…}

If this happened, there is no way of knowing which function to use. YOU CAN HAVE:

Void squiggle(int length){…}

Void squiggle(int length, int numberOfLines){…}

Void squiggle(int length, char* textToDrawOver){…}

If we want to overload operators, we do the following:

Class A{…

A operator+(A a){…}

…}

This would overload the + operator so we could do :

A a = *(new A());

A b = *(new A());

A c = a + b;

This clears up code greatly, and allows us to also overwrite the == operator. Perhaps in a Person class we want the == operator to compare name, birthday, parents, and place of birth. Or we could do SSN, etc. It is easier and more intuitive to use the == operator than call a function such as ->equals(…).

Headers

Header files are “outlines” of classes to be defined in the corresponding source file. Headers should be named the same as the class they define (classname.h) and the related source should as well (classname.cpp). Header files are useful for large/complex classes. Not only can we separate the class out into its own file, we can separate the outline into one for quick access. This is useful for finding the parameters and return values of functions when we don’t care about how the result is obtained.

#ifndef CLASSNAME_H

#define CLASSNAME_H

#endif

These preprocessor commands (identifiable as such by the # preceeding them) tells the compiler the following:

If CLASSNAME_H is not defined, do what’s between #ifndef and #endif.

Define CLASSNAME_H so we know we have defined this class in the overall program.

We do this as a precaution: Trying to include the code more than once can cause compiler errors, but the #ifndef prevents this by only defining it once and otherwise ignoring it.

Makefiles

Makefiles are text files that specify what files to compile, which compiler to use, and what target to compile to.

Look at the Makefile provided for this lab. It consists of:

CC = g++

OBJECTS = lab3.o Complex.o

lab3: $(OBJECTS)

$(CC) $(OBJECTS) -o lab3

clean:

rm *.o lab3

CC specifies the compiler (in this case, g++)

OBJECTS specifies the list of objects we want to include in this compile (these are the objects generated from each of the source files in the project)

Lab3: and clean: are targets. They let us execute different commands. Clean would remove all objects and the executable from the last compile. Lab3, the default target (it’s the top most) does the compilation.

To make the program, we simply run the “make” command. It creates an executable called lab3. If we run make clean (we select the target as clean) then we remove all objects and the executable.

Clean is a useful target to include: on larger projects, the space consumed by all the objects might be large. We can clean, zip, and ship it off. Then a simple make would reconstruct the program.

Directions

In the lab3.zip, there are four files:

Complex.h // Contains some information about requirements in here

Complex.cpp //Empty starter file for the Complex source code

Lab3.cpp // Complete file with main function and i/o for testing

Makefile // Complete Makefile