1 Classes and Data Abstraction Chapter 15. 2 What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.

Slides:



Advertisements
Similar presentations
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Advertisements

1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
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.
Chapter 14: Overloading and Templates
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Chapter 11: Classes and Data Abstraction
1 Abstract Data Type (ADT) a data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how)
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
1 Structured Types, Data Abstraction and Classes.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Chapter 11: Classes and Data Abstraction. C++ Programming: Program Design Including Data Structures, Fourth Edition2 Objectives In this chapter, you will:
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Data Structures and Algorithms Week 3 Data Abstraction: Part II Structured Types, and Classes.
Design.ppt1 Top-down designs: 1. Define the Problem IPO 2. Identify tasks, Modularize 3. Use structure chart 4. Pseudocode for Mainline 5. Construct pseudocode.
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.
Classes Structured Programming 256 Chapter 8 Classes - Part I OOP & Class Object Terminology File Management.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
1 Classes and Data Abstraction 2 Objectives Understand encapsulation and data hiding Understand data abstraction and Abstract Data Types (ADTs) Create.
1 Structural Types, Data Abstractions and Classes.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
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.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
1 Final Exam Tues 3/16/10 (2-3:50) (Same classroom) Old Textbook - Chapters 11-16, 18 Focus is on 15, 16 and 18 Final Exam Tues 3/16/10 (2-3:50) (Same.
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?
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, Fourth Edition Chapter 12: Classes and Data Abstraction.
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.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
1 Final Exam 20 Questions Final Exam 20 Questions Multiple choice and some minimal programming will be required.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
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 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.
1 Structured Types, Data Abstraction and Classes.
Chapter 12 Classes and Abstraction
Classes C++ representation of an object
Dale/Weems/Headington
Java Programming: Guided Learning with Early Objects
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Introduction to Classes
Chapter Structured Types, Data Abstraction and Classes
Introduction to Structured Data Types and Classes
CS148 Introduction to Programming II
Introduction to Classes
Classes and Data Abstraction
Defining Classes and Methods
Defining Classes and Methods
Classes C++ representation of an object
Chapter 9 Introduction To Classes
CS148 Introduction to Programming II
(4 – 2) Introduction to Classes in C++
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Introduction to Classes and Objects
Presentation transcript:

1 Classes and Data Abstraction Chapter 15

2 What a Class ! ! Specification and implementation Private and public elements Declaring classes data and functions What is a constructor? Classes we have already seen

3 Abstract Data Types Defn => A type whose properties (domain and operations) are specified independent of any particular implementation Domain => what are possible values for the type Operations => what things can be done to/with the values in the domain

4 Data Type set of values (domain) allowable operations on those values FOR EXAMPLE, data type int has domain operations +, -, *, /, %, >>, <<

5 Example of Abstract Data Type Type: complex numbers Domain: numbers of the form a + bi Operations –add, subtract –multiply –divide (4 + 2i) + (6 - 3i) (7 - 2i) * (5 + 4)

6 Categories of Operations Constructor => create new instance (variable) of Abstract Data Type (ADT) Transformer => builds a new value of the ADT, given one or more previous values of the type complex c1, c2; // constructor initializes a + bi complex c1, c2; c1.assign (3, -4); // stores new values in c1

7 Categories of Operations Observer => Allows a look at the state of an instance of an ADT without changing it. Iterator => Enables processing (one at a time) of all the components of an instance of an ADT complex c1, c2; cout << c1.real_part(); complex c1, c2; c1.double();

8 C++ Classes Data and Operations bound into a single unit –Like a struct which includes functions to manipulate the data class Complex { public : void assign (float a, float b); void print_complex (); float real_part ();... private: float a, b; } ;

9 Classes, Objects, Members Class => a structured type in a programming language –used to represent an abstract data type Class Member => Component of a class –functions –data class Complex { public : void assign (float a, float b); void print_complex (); float real_part ();... private: float a, b; } ;

10 Classes, Objects, Members Class Object class instance –variable of a class type Client => Software that declares and manipulates objects of a particular class: complex c1, c2; cout << c1.real_part();

11 Built In Class Operations Programmer defined classes can be like built in types Declare as many objects of a class as you like Pass as parameters to functions Return as function values void do_whatever (Complex z); Complex new_value ( … );

12 Built In Class Operations Arrays of class objects Can be automatic or static Assign operator and dot. operator both work complex c1, c2; Complex c_list [20]; complex c1, c2;... c1 = c2; cout << c1.real_part();

13 Class Operations Recall that built in types (int, float, etc.) have some operators + - * / == <= << Classes do NOT come with these available by default –if you need them, you must program them

14 Class Scope Name of a class member is local to the class. Same identifier declared outside the class will be unrelated; complex c1, c2; int a, b, c;... c1 = c2; cout << c1.real_part(); // but NOT // cout << real_part

15 Information Hiding Class object has an "invisible wall" –called the abstraction barrier Protects private data and functions –client code cannot access private elements –client can access only public members Think of a class as a "black box" –it will act on the data –but you need not know how it works

16 Information Hiding Class implementation details are hidden from the client’s view. This is called information hiding. Public functions of a class provide the interface between the client code and the class objects. client code specification abstraction barrier implementation

17 Information Hiding Encapsulation Hiding of implementation details Keeps client/user of the ADT from… –depending on details –incorrectly manipulating the details

18 Specification File File which describes the behavior of the data type –does not reference the implementation details This is the.h file with the prototypes, the declarations Both the client and implementation file will have #include

19 Implementation File Contains all the function definitions –includes function heading –includes body of function Similar to function definitions below main ( ) –except is in different file Function headings in this file must match prototypes in.h file

20 class TimeType Specification // SPECIFICATION FILE( timetype.h ) class TimeType// declares a class data type {// does not allocate memory public : // 5 public function members void Set ( int hours, int mins, int secs ) ; void Increment ( ) ; void Write ( ) const ; Boolean Equal ( TimeType otherTime ) const ; Boolean LessThan ( TimeType otherTime ) const ; private :// 3 private data members int hrs ; int mins ; int secs ; } ; // SPECIFICATION FILE( timetype.h ) class TimeType// declares a class data type {// does not allocate memory public : // 5 public function members void Set ( int hours, int mins, int secs ) ; void Increment ( ) ; void Write ( ) const ; Boolean Equal ( TimeType otherTime ) const ; Boolean LessThan ( TimeType otherTime ) const ; private :// 3 private data members int hrs ; int mins ; int secs ; } ;

21 2 Separate Files Generally Used for class Type // SPECIFICATION FILE ( timetype.h ) // Specifies the data and function members. class TimeType { public:... private:... } ; // IMPLEMENTATION FILE ( timetype.cpp ) // Implements the TimeType member functions....

22 Implementation file for TimeType // IMPLEMENTATION FILE ( timetype.cpp ) // Implements the TimeType member functions. #include “ timetype.h”// also must appear in client code #include... Boolean TimeType :: Equal ( /* in */ TimeType otherTime ) const // Postcondition: // Function value == true, if this time equals otherTime // == false, otherwise { return ( (hrs == otherTime.hrs) && (mins == otherTime.mins) && (secs == otherTime.secs) ) ; }...

23 Familiar Class Instances and Function Members The member selection operator (. ) selects either data members or function members. Header files iostream. h and fstream.h declare the istream, ostream,and ifstream, ofstream I/O classes. Both cin and cout are class objects Both get and ignore are function members. cin.get (someChar) ; cin.ignore (100, ‘\n’) ; cin.get (someChar) ; cin.ignore (100, ‘\n’) ;

24 Familiar Class Instances and Function Members Declare myInfile as an instance of class ifstream. Invoke function member open. ifstream myInfile ; myInfile.open ( “A:\\mydata.dat” ) ; ifstream myInfile ; myInfile.open ( “A:\\mydata.dat” ) ;

25 Class constructors Guarantee initialization of class object Constructor function has same name as the class Can have different versions of constructor Complex::Complex ( ) { a = 0; b = 0; Complex::Complex (int real_part, int Imag_part) { a = real_part; b = imag_part; }

26 Invoking a Constructor Invoked automatically whenever a class object is created (instantiated) Example: Which version gets used, depends on the number of parameters Complex c1, c2; Complex c3 (5.3, -6);

27 Guidelines for Constructors A constructor canNOT return a value –no return value type is declared Multiple constructors are allowed –compiler chooses appropriate one –according to number & data types of parameters Parameters passed to a constructor –place actual parameter list after name of class object being declared

28 Guidelines for Constructors If class object declared WITHOUT a parameter list –results depend on what constructors are provided –even if NONE provided, there is a default constructor which allocates memory for private data elements When array of class objects declared, default constructor is invoked

29 Testing and Debugging Hints Don't forget semicolon ; at end of class type declaration Function declaration in.h file (specification) end with semicolons Function definitions in.cpp file (implementation) do NOT end with ; In implementation, don't forget to prefix function name with –name of class –double colon ::

30 Testing and Debugging Hints Only built in operations (we know about) are –the dot. for member selection –the assignment = Functions which inspect (but do not modify) should be const member functions