CS 111 - Introduction to Data Structures. Spring Term 2004 Franz Hiergeist. CS 111 - A continuation of CS 110 C++ is the vehicle to implement our ideas Data Structures Algorithms Recursion Prerequisite: Successful completion of CS 110
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Spring Term 2004
C++ is the vehicle to implement our ideas
Prerequisite: Successful completion of CS 110
We will discuss material from each of the first nine chapters and from Chapters 11 and 13 of the text.
Recall that a data type is characterized by
1. The kind of value that can be stored in an object of the type
2. The operations that can be performed on objects of the type.
A class in C++ is the same thing as a type, though it is more advanced than an “ordinary” type, like int or float.
In a class the developer decides how the data will be stored in instances of the class (objects), and what operations will be available to the programmer who uses the class.
A type, or class, constructed entirely by a programmer who also specifies how the data will be stored, and what operations can be performed on the data is called an Abstract Data Type, or ADT.
Data members, or fields, which are variable declarations
Member functions, or methods, which are operations (functions) that operate on the data members.
A specification, or definition, file - also called a header file. It consists of declarations of data members, constants, and prototypes of member functions.
An implementation file - which contains the implementation of the member functions, and possibly declarations.
It’s a common convention that a class name begins with an upper case letter to distinguish it from an ordinary data type. For example: List
The name of the definition file of the class is the class name (usually all lower case) with the file extension .h
For example: list.h
The name of the implementation file of the class is the class name with the file extension .cpp
For example: list.cpp
// declarations of visible attributes - // data members and member functions
// declarations of hidden attributes - // data members and member functions
The private section of the class (the default) consists of all data members, constants, types, and member functions that are available ONLY to the class.
We will shortly introduce a third section called the protected section.
When an object of a class is declared in an application program, that object’s data members are uninitialized. One way to initialize the data members is through a constructor.
A constructor is a special kind of member function which has the same name as the class, and is automatically invoked every time an object of the class is declared.
A class may have more than one constructor.
Initialize the clock
Set the time on the clock
Advance the time on the clock
Display the time on the clock in several ways
These actions represent the member functions
// public attributes
// private attributes
#endif // CLOCK
Clock (int hour, int min, int sec);
bool set (int hour, int min, int sec);
void displayHour ();
void displayMin ();
void displaySec ();
void tick ();
void displayTime (bool writeSec = true);
int h, m, s;
Clock (int hour, int min, int sec);
This is an example of a function name which is overloaded. That is, there is more than one function with the same name.
This is sometimes called polymorphism.
When a function name has been overloaded, the compiler must be able to determine which function has been called by the argument list.
In this first instance the constructor
In the second instance the constructor
Clock (int hour, int min, int sec)
It is possible to implement the member functions in a class within the specification file, but in this course we will rarely do that. In almost all instances the member functions will be implemented in a separate implementation file.
romeClock (6, 0, 0);
cin >> hr >> min >> sec;
localClock.set (hr, min,sec);
cout <<“The time in Rome is “;