190 likes | 356 Vues
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
E N D
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 We will discuss material from each of the first nine chapters and from Chapters 11 and 13 of the text.
Classes 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.
For an “ordinary” type there is a predefined set of operations on objects of the type that are available to the programmer. 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.
A class typically contains two kinds of attributes: Data members, or fields, which are variable declarations Member functions, or methods, which are operations (functions) that operate on the data members.
A class usually consists of two components: 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.
Names 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
Format of a class specification class <class name> { public: // declarations of visible attributes - // data members and member functions private: // declarations of hidden attributes - // data members and member functions };
The public section of the class consists the specifications of all data members, constants, types, and member functions that are accessible to the class and to any other component (your program that wants to declare and use objects of the class). 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.
Initialization of class objects 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.
Example – Define a class named Clock which keeps time in hours, minutes, and seconds (data members), and can 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
The specification file #ifndef CLOCK #define CLOCK Class Clock { public: // public attributes private: // private attributes }; #endif // CLOCK
Specification file (continued) public: 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);
Specification file (continued) private: int h, m, s;
In this example the class, the Clock class, has two constructors: Clock (); 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.
The function Clock is called with either NO arguments (usually called the default constructor), or with three integer arguments. In this first instance the constructor Clock () is called. In the second instance the constructor Clock (int hour, int min, int sec) is called.
Unlike an ordinary function, a constructor can not have a return type specified (not even a void type). 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.
Isolated statements from an Application Program using the Clock class #include “clock.h” Clock localClock, romeClock (6, 0, 0); cin >> hr >> min >> sec; localClock.set (hr, min,sec); romeClock.set (hr+6,min,sec); cout <<“The time in Rome is “; romeClock.displayTime ();
Assume an application program named clockTest.cpp that uses the class Clock has been written. Unix steps: • Compile the implementation file for the class Clock: • g++ -c clock.cpp –o clock.o • Compile and link the application in clockTest.cpp: • g++ clockTest.cpp clock.o –o clockTest • Execute the clockTest program: • clockTest or ./clockTest