CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.

Slides:



Advertisements
Similar presentations
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Advertisements

Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Software Testing and Quality Assurance
Rossella Lau Lecture 5, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 5: Class construction  Encapsulation 
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 11: Classes and Data Abstraction
OOP Languages: Java vs C++
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Defining New Types Lecture 21 Hartmut Kaiser
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
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.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Recap Stale Pointers and Double Delete Reference Variables Reference Type vs Pointer Type Structures Pointers to Structures Exogenous vs Indigenous Data.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
1 Classes struct Public and Private Parts of a struct Class Scope of a Class Overloading Member Functions Class in a Class Static Members of Classes this.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
100 學年度碩士班新生暑期課程 程式設計 Object-Oriented Programming: Part I The Basics of Classes.
Comp1004: Building Better Objects II Encapsulation and Constructors.
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.
CMSC 202 Lesson 9 Classes III. Warmup Using the following part of a class, implement the Sharpen() method, it removes 1 from the length: class Pencil.
Classes (Part 1) Lecture 3
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
A First C++ Class – a Circle
Chapter 3: Using Methods, Classes, and Objects
Introduction to Classes
Introduction to Classes
Classes and Data Abstraction
Encapsulation and Constructors
Defining Classes and Methods
Defining Classes and Methods
Lists CMSC 202, Version 4/02.
Lists CMSC 202, Version 4/02.
Presentation transcript:

CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside the class a collection of public operations to manipulate the data additional private operations hidden inside the class The notion of information hiding is quite common the buttons on a cell phone provide various operations users have no idea about the inner workings of the phone it is not necessary to understand how the phone works in order to use it!

CPSC 252 ADTs and C++ Classes Page 2 C++ classes as abstract data types These correspond to ADTs in other languages public member declarations comprise the interface private members and their implementations comprise the definition or “inner workings” of the class client programmer need only know the public interface Each object is an instance of a class and has: state (its own values for the member variables) behavior (depends on current state) identity (no two objects are the same)

CPSC 252 ADTs and C++ Classes Page 3 The IntVector class as an example of an ADT We will review how to design and implement C++ classes by designing the IntVector class the class represents a vector of integers our implementation will store the data in an array we impose restrictions we will remove in future versions this implementation has only four public member functions A more robust implementation of IntVector is in the example section of the course website

CPSC 252 ADTs and C++ Classes Page 4 show IntVector example

CPSC 252 ADTs and C++ Classes Page 5 Class declaration and data members Our (simplified) implementation uses a fixed size array data members are private so they are hidden minimize the number of variables to represent state class IntVector // Invariant: there are MAX_SIZE entries // indexed from 0 to MAX_SIZE-1 // entries not defined unless initialized { private: static const int MAX_SIZE=100; int value[MAX_SIZE]; // array of integer values more on the next slide…

CPSC 252 ADTs and C++ Classes Page 6 Constructor Implicitly invoked for each instance of IntVector performs any necessary initialization of member variables always has the same name as the class constructor does not have a return type IntVector(); // Default constructor // POST: an IntVector with capacity MAX_SIZE // has been created note the semicolon “;” after the declaration no precondition (maybe if there are parameters) postcondition often just the invariant (unless parameters)

CPSC 252 ADTs and C++ Classes Page 7 An accessor Returns information about the state of an object does not alter the state of the object this accessor returns the integer stored at a specified index int at( int index ) const; // Accessor // PRE: 0 <= index < MAX_SIZE and [index] defined // POST: If 0 <= index < MAX_SIZE the element at // index is returned if [index] is defined // Otherwise, program is aborted. precondition says a valid index is required postcondition tells us how exceptions are handled sometimes we do not check the precondition in the code

CPSC 252 ADTs and C++ Classes Page 8 Another accessor this accessor returns the maximum capacity of the vector int size( void ) const; // Accessor // PRE: (none) // POST: maximum capacity of vector has been // returned. all accessors have the const keyword at the end no postcondtion required (but invariant is implied)

CPSC 252 ADTs and C++ Classes Page 9 A mutator Alters the state of the object in some way this mutator changes the value at a specified index int& at( int index ); // Mutator // PRE: 0 <= index < MAX_SIZE // POST: If 0 <= index < MAX_SIZE // the address of element at index is returned // Otherwise, the program is aborted. the const keyword is not used here this method changes the state of the object type of the return value is int& not int (reference)

CPSC 252 ADTs and C++ Classes Page 10 Overloading We now have two methods with the same name! int at( int index ) const; int& at( int index ); this is called function overloading it occurs when two functions have the same name but they have different signatures There are rules for how this can happen…

CPSC 252 ADTs and C++ Classes Page 11 Signatures of functions vs. methods In C++ a function declared outside a class has the following general syntax: [ ( ) ] signature A function declared within a class is a method: [ ( ) ] [ const ] signature

CPSC 252 ADTs and C++ Classes Page 12 Implicit parameters in methods The object on which a method is invoked is always a (hidden) zero-th parameter to the function Thus the method definition int IntVector::at( int index ) const signature can be considered to be “shorthand” for the function whose first parameter is a pointer to the IntVector int at( const IntVector* this, int index ) signature (Pointers and the fine details of “ const ” still lie ahead!)

CPSC 252 ADTs and C++ Classes Page 13 How a client might use the IntVector class IntVector myVector; int capacity = myVector.size(); for( int index = 0; index = 0; index-- ) cout << myVector.at( index ) << endl; We are able to write this code using only the public interface we haven’t used the private section of the class declaration nor have we implemented the member functions!

CPSC 252 ADTs and C++ Classes Page 14 Implementation of the IntVector methods I The constructor is easy, with nothing to do (usually there is) IntVector::IntVector() { } The first accessor just returns the appropriate entry int IntVector::at( int index ) const { return value[ index ]; } What happens if index is not legal or not defined?

CPSC 252 ADTs and C++ Classes Page 15 Implementation of the IntVector methods II The other accessor always returns the same value: int IntVector::size( void ) const { return MAX_SIZE; } The mutator is also (deceptively) simple: int& IntVector::at( int index ) { return value[ index ]; } The body of the method is exactly the same as for the const version of the overloaded at() method !

CPSC 252 ADTs and C++ Classes Page 16 Why do we need two versions of at() ? What is really going on? the 2nd version of at() returns a reference to the integer stored at the given index this reference could be used not only to change that integer but also to retrieve its value so why do we need the const version of the at() function? The answer to these questions reveals a number of features of C++ that will take a while to fully appreciate!

CPSC 252 ADTs and C++ Classes Page 17 An example to illustrate why we need both methods void printVector( const IntVector& myVector ) { int capacity = myVector.size(); for( int index = 0; index < capacity; index++ ) cout << myVector.at( index ) << endl; } The parameter myVector is declared to be const printVector cannot change the state of myVector only const methods in IntVector can be invoked on myVector we need a const version of the at() method, otherwise we would not be able to access the data stored in the vector