Classes and Data Abstraction

Slides:



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

Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
C++ Classes & Data Abstraction
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 11: Classes and Data Abstraction
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
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.
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.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating 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.
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.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution 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 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
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
Structures and Classes
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
Dale/Weems/Headington
Abstract Data Types Programmer-created data types that specify
Chapter 7: Introduction to Classes and 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
Defining Classes and Methods
Defining Classes and Methods
Classes C++ representation of an object
Chapter 9 Introduction To Classes
CS148 Introduction to Programming II
Classes and Objects Systems Programming.
(4 – 2) Introduction to Classes in C++
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Introduction to Classes and Objects
Presentation transcript:

Classes and Data Abstraction Chapter 15

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

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

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

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)

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

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

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

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

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

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

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

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

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

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

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 abstraction barrier specification implementation

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

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 <xxxx.h> Example

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 Example

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.

Familiar Class Instances and Function Members The member selection operator ( . ) selects either data members or function members. Header files iostream and fstream 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’) ;

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

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

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

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

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

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

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