Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Classes and Data Abstraction Chapter 15. 2 What a Class ! ! Specification and implementation Private and public elements Declaring classes data and."— Presentation transcript:

1 1 Classes and Data Abstraction Chapter 15

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

5 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 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 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 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 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 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 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 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 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 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 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 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 17 Information Hiding Encapsulation Hiding of implementation details Keeps client/user of the ADT from… –depending on details –incorrectly manipulating the details

18 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 Example

19 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 Example

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

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

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

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

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

25 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

26 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

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

28 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


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

Similar presentations


Ads by Google