UMBC CMSC202, Computer Science II, Spring 1998,
Sections 0101, 0102, 0103, 0104 and Honors
Tuesday May 12, 1998
Assigned Reading:
- A Book on C:
- Programming Abstractions in C:
Handouts (available on-line):
Topics Covered:
- Summary of topics covered in this course:
- OOP Topics:
- C++ Classes and Data Hiding.
- Class Derivation and Inheritance.
- Function Overloading
- Memory Allocation
- Reference Parameters
- Data Structures:
- Dynamic Arrays
- Linked Lists
- Stacks and Queues
- Trees and Binary Search Trees
- Hash Tables
- Big-Oh Notation (without formal definition)
- Recursion
- Sorting
- Some topics not covered in this course:
- C++ features:
- C++ iostream.h library
- Inline functions
- Initializers in constructors
- Multiple inheritance
- Operator overloading
- Polymorphism: virtual functions and abstract classes
- Exception Handling
- Templates
- Data Structures:
- Heaps
- Balanced Binary Search Trees
- Graphs
- Algorithm Design
- Formal definitions of Big-Oh, Big-Omega, etc.
- Where to go from here?
- Read a C++ book. Some suggestions:
- "C++ with Object Oriented Programming" by Paul S. Wang,
PWS Publishing, 1994 (ISBN 0-534-19644-6).
- "Success with C++" by Kris Jamsa, Jamsa Press, 1995
(ISBN 0-7895-0088-4).
- Read a book on Java programming.
- Take CMSC 109 (C++ version) to brush up on C++.
- Take CMSC 341 (Data Structures). In the Fall '98 semester, CMSC
341 will cover some C++ programming topics at the beginning of the
course. Starting Spring '99, CMSC 341 will assume that entering
students already know C++.
- Take CMSC 331 (Programming Languages): compare, contrast and critique programming
languages.
- Take CMSC 211 (Assembly Language): even lower level programming.
- Eventually take CMSC 431 (Compiler Construction): implement a
small compiler and figure out why compilers generate those cryptic
syntax error messages.
- Finally, we finish up the qsort() example from the
previous lecture. Last time, we saw how a
function pointer for the compare function is used by the qsort()
library function to sort an array of any type. We wrote a program to sort an array of int
and another program to sort an array
of double. The two programs were extremely similar (see output from diff).
- In the next example, we have quicksort functions to sort both
int and double arrays in one program. Our goal is to
write the function only once. One not very elegant way to do this is to
have a C++ file, qhack.C,
that has the macro DATA appear where the typedef'ed
data appear. In the main
program, we include the file qhack.C twice: once with
DATA defined as int and once with DATA
defined as double. In this way, we get two copies of the
functions compare(), PrintArray(),
CheckArray() and qsort(). However, one set of
functions works with int arrays and the other set works with
double arrays. So, the function overloading feature of C++
guarantees that there is no naming problem. By the way, this is not a
strange use of function overloading. This is the reason that C++
allows function overloading. (See sample
run.)
- The qsort() function needs a bit of explanation:
void qsort(DATA A[], int n) {
int (*compare_func) (const DATA *, const DATA *) ;
compare_func = &compare ;
qsort(A, n, sizeof(DATA), compare_func) ;
}
The statement:
int (*compare_func) (const DATA *, const DATA *) ;
is a variable declaration. The variable name is compare_func.
The variable is a pointer to a function that takes two const DATA
pointers and returns an int. We assign the address of the
compare() function to compare_func then use
compare_func in the call to the qsort() library
function. Why don't we simply use the following?
qsort(A, n, sizeof(DATA), &compare) ;
Well, there are two functions called compare(). Since
the use of compare() in the call to qsort() does not
involve any parameters, the compiler cannot distinguish the two
compare() functions. However, the declaration of the variable
compare_func does provide the type of the parameters and of the
return value of the functions that compare_func can point to.
Thus, in the assignment
compare_func = &compare ;
the compiler can distinguish the two compare() functions.
- The C++ template facility can be used here instead of including the
file qhack.C. To make a
template, we have a file qtemplate.h which looks very
much like qhack.C. The main
difference is that the line
template
appears before every function prototype and every function definition.
Also, the file qtemplate.h
is included only once in the main
program. The intent here is exactly the same as the previous example.
(See sample run.) We write the code for
compare(), PrintArray(), CheckArray() and
qsort() just once. If the compiler encounters a need for these
functions for a particular type of array (int, double,
etc.), it uses the template to generate overloaded functions for that
particular type.
It's Summer !!!
Last Modified:
22 Jul 2024 11:27:45 EDT
by
Richard Chang
Back up
to Spring 1998 CMSC 202 Section Homepage