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

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
C++ Classes & Data Abstraction
Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Starting Out with C++: Early Objects 5th Edition
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 11: Classes and Data Abstraction
Guide To UNIX Using Linux Third Edition
1 Abstract Data Type (ADT) a data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how)
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
CHAPTER 13 CLASSES AND DATA ABSTRACTION. In this chapter, you will:  Learn about classes  Learn about private, protected, and public members of a class.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Data Structures Using C++1 Chapter 1 -Software Engineering Principles -ADT and Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Structured Data and Classes Chapter 7. Combining Data into Structures Structure: C++ construct that allows multiple variables to be grouped together Structure.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
W 4 L 1 sh 1 LessonSubjectBook Week 4 lesson 1Class, copy-constructorH ; p197 – 226 Week 4 lesson 2Operators 1H ; p237 – 254 Week 5 lesson.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 12: Classes and Data Abstraction.
Class 4 (L34) u Constructors u Default Constructor u Example of Default Constructors u Destructors u Constructors Are Called in Global Scope u Constructors.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
Procedural and Object-Oriented Programming
Classes C++ representation of an object
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 7: Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
More About Objects and Methods
Introduction to Classes
Chapter Structured Types, Data Abstraction and Classes
Separate Compilation and Namespaces
CS148 Introduction to Programming II
Introduction to Classes
Classes: A Deeper Look Outline
Classes C++ representation of an object
Separating Interface from Implementation
Presentation transcript:

CS Introduction to Data Structures Spring Term 2004 Franz Hiergeist

CS 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 { 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: 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: clockTestor./clockTest