Cs 111 introduction to data structures
This presentation is the property of its rightful owner.
Sponsored Links
1 / 19

CS 111 - Introduction to Data Structures PowerPoint PPT Presentation


  • 64 Views
  • Uploaded on
  • Presentation posted in: General

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 Presentation

CS 111 - Introduction to Data Structures

An Image/Link below is provided (as is) to download presentation

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.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Cs 111 introduction to data structures

CS 111 - Introduction to Data Structures

Spring Term 2004

Franz Hiergeist


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.


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.


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.


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.


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.


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


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

};


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.


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.


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


Cs 111 introduction to data structures

The specification file

#ifndef CLOCK

#define CLOCK

Class Clock

{

public:

// public attributes

private:

// private attributes

};

#endif // CLOCK


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


Cs 111 introduction to data structures

Specification file (continued)

private:

int h, m, s;


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.


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.


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.


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


Cs 111 introduction to data structures

  • 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:

  • clockTestor ./clockTest


  • Login