Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2 Objects and Classes

Similar presentations


Presentation on theme: "Chapter 2 Objects and Classes"— Presentation transcript:

1 Chapter 2 Objects and Classes
Saurav Karmakar Spring 2007

2 2.1 What is OO programming? Object: an atomic unit that has structure and state Information hiding: Black-box analogy Encapsulation: grouping of data and functions Inheritance: mechanism allows extending functionality of an object.

3 How does C++ support OO Template: the logic is independent of the type
Inheritance Polymorphism: can hold different types of objects. When operations are applied to the polymorphic type, the operation appropriate to the actual stored type is automatically selected.

4 2.2 Basic Class Syntax Class members: either data or functions and categorized into either public, protected,or private. Public: visible to all routines and may be accessed by any methods in any class. Private: not visible to nonclass routines and are accessed only by methods in its class. Protected: similar to private but visible to derived classes. Default: all members are private

5 Constructors Member functions/methods that describe how an object is declared and initialized. If no constructor defined, compilers will generate one called default constructor. Explicit constructors prevent automatic type conversion.

6 Constant Member Function
Mutators change the state of the object but accessors are those who don’t. Constant member function: functions that do not change any class data member. const is a part of the function signature. [const] return_type name([const] parameter_list) [const];

7 Interface & Implementation
Interface lists the class and its members and describes what can be done with the object. Implementation represents the internal process by which the interface specifications are met. Scope operator use : Systax : Classname ::member Signatures must match exactly.

8

9 TestIntCell.cpp #include <iostream.h> #include "IntCell.h"
int main( ) { IntCell m; // Or, IntCell m( 0 ); but not IntCell m( ); m.write( 5 ); cout << "Cell contents: " << m.read( ) << endl; return 0; }

10 Big three: Destructor, Copy Constructor, and Operator =
•Destructor tells how an object is destroyed and frees the allocated resources when it exists scope. ~IntCell(); •Copy Constructor allows a new object construction using the data in from existing one. IntCell a = b; // a new IntCellcall a IntCell a( b ); // a new IntCellcall a •Operator = copy assignment, copies data members using = by default. => may cause shallow copying.

11 This (predefine pointer)

12 2.3 Additional C++ Features
Operator overloading: extending the types to which an operator can be applied. example: string x=“Mary’s score is:”; int y=95; string z=x+y; “.”, “.*”, “?:”, “sizeof” can’t be overloaded

13 Additional C++ Features
Type conversion creates a temporary object of a new type– Example: int a = 5; double b = a; //implicit cast

14 2.5 Exceptions (report error)
An object that stores information transmitted outside the normal return sequence and is used to signal exceptional occurrences. Handle exceptions by throw and catch clauses.

15 Exceptions example try { for (int n=0; n<10; n++)
if (n>9) throw "Out of range"; } } catch (char * str) cout<< "Exception: " << str<< endl;

16 2.6 String Class C string: array of character terminated by ‘\0’
C++ standard string: a STL class with all overload operators and built-in functions

17 Array size extension int *original =arr; arr = new int [12];
for(i=0; i<10; i++) arr[i] =original[i]; delete [] original;

18 Summary Construction/ destruction of objects Copy semantics
Overloading Implicit/explicit type conversion Information hiding/atomicity


Download ppt "Chapter 2 Objects and Classes"

Similar presentations


Ads by Google