280 likes | 300 Views
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.
E N D
Classes and Data Abstraction Chapter 15
What a Class ! ! Privateandpublic elements Specificationandimplementation Classes wehave alreadyseen Declaring classesdata andfunctions What is aconstructor?
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 (domain) allowable operations on those values FOR EXAMPLE, data type int has operations +, -, *, /, %, >>, << domain -32768 . . . 32767
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 notknow 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 theinterface 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 andfstreamdeclare the istream, ostream,and ifstream, ofstream I/O classes. • Both cin and cout are class objects • Both get and ignoreare 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 versionsof 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