1 classes and data abstraction chapter 15. 2 what a class ! ! specification and implementation...

28
1 Classes and Data Abstraction Chapter 15

Upload: ashly-goward

Post on 22-Dec-2015

220 views

Category:

Documents


1 download

TRANSCRIPT

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

1

Classes and Data Abstraction

Chapter 15

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

2

What a Class ! !

Specificationand

implementation

Privateand

public elements

Declaring classesdata andfunctions

What is aconstructor?

Classes wehave already

seen

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

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

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

4

Data Type

set of values(domain)

allowable operationson those values

FOR EXAMPLE, data type int has

domain

-32768 . . . 32767

operations

+, -, *, /, %, >>, <<

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

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)(4 + 2i) + (6 - 3i)

(7 - 2i) * (5 + 4)(7 - 2i) * (5 + 4)

i

i

5

)23(

i

i

5

)23(

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

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;// constructor initializes a + bi

complex c1, c2;c1.assign (3, -4);// stores new values in c1

complex c1, c2;c1.assign (3, -4);// stores new values in c1

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

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;cout << c1.real_part();

complex c1, c2;c1.double();

complex c1, c2;c1.double();

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

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

class Complex{ public : void assign (float a, float b); void print_complex (); float real_part (); . . . private: float a, b; } ;

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

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

class Complex{ public : void assign (float a, float b); void print_complex (); float real_part (); . . . private: float a, b; } ;

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

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

complex c1, c2;cout << c1.real_part();

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

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);void do_whatever (Complex z);

Complex new_value ( … );Complex new_value ( … );

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

12

Built In Class Operations

Arrays of class objects

Can be automatic or staticAssign operator and dot . operator

both work

complex c1, c2;Complex c_list [20];

complex c1, c2;Complex c_list [20];

complex c1, c2;. . .c1 = c2;cout << c1.real_part();

complex c1, c2;. . .c1 = c2;cout << c1.real_part();

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

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

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

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

complex c1, c2;int a, b, c;. . .c1 = c2;cout << c1.real_part();// but NOT // cout << real_part

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

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

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

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.

clientcode

specification

abstraction barrierimplementation

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

17

Information Hiding

EncapsulationHiding of implementation detailsKeeps client/user of the ADT from…

– depending on details– incorrectly manipulating the details

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

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

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

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

ExampleExample

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

20

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

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

21Familiar 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’) ;cin.get (someChar) ;cin.ignore (100, ‘\n’) ;

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

22

Familiar Class Instances and Function MembersDeclare 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” ) ;

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

23

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

Complex::Complex ( ) { a = 0; b = 0;Complex::Complex (int real_part, int Imag_part) { a = real_part; b = imag_part; }

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

24

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);Complex c1, c2;Complex c3 (5.3, -6);

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

25

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

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

26

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

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

27

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

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

28

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