cs 111 - introduction to data structures

19
CS 111 - Introduction to Data Structures Spring Term 2004 Franz Hiergeist

Upload: suchi

Post on 05-Jan-2016

36 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 111  -  Introduction to Data Structures

CS 111 - Introduction to Data Structures

Spring Term 2004

Franz Hiergeist

Page 2: CS 111  -  Introduction to Data Structures

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.

Page 3: CS 111  -  Introduction to Data Structures

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.

Page 4: CS 111  -  Introduction to Data Structures

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.

Page 5: CS 111  -  Introduction to Data Structures

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.

Page 6: CS 111  -  Introduction to Data Structures

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.

Page 7: CS 111  -  Introduction to Data Structures

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

Page 8: CS 111  -  Introduction to Data Structures

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

};

Page 9: CS 111  -  Introduction to Data Structures

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.

Page 10: CS 111  -  Introduction to Data Structures

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.

Page 11: CS 111  -  Introduction to Data Structures

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

Page 12: CS 111  -  Introduction to Data Structures

The specification file

#ifndef CLOCK

#define CLOCK

Class Clock

{

public:

// public attributes

private:

// private attributes

};

#endif // CLOCK

Page 13: CS 111  -  Introduction to Data Structures

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);

Page 14: CS 111  -  Introduction to Data Structures

Specification file (continued)

private:

int h, m, s;

Page 15: CS 111  -  Introduction to Data Structures

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.

Page 16: CS 111  -  Introduction to Data Structures

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.

Page 17: CS 111  -  Introduction to Data Structures

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.

Page 18: CS 111  -  Introduction to Data Structures

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 ();

Page 19: CS 111  -  Introduction to Data Structures

Assume an application program named clockTest.cpp that uses the class Clock has been written. Unix steps:

1. Compile the implementation file for the class Clock:

g++ -c clock.cpp –o clock.o

2. Compile and link the application in clockTest.cpp:

g++ clockTest.cpp clock.o –o clockTest

3. Execute the clockTest program:

clockTest or ./clockTest