Object Oriented Programming - PowerPoint PPT Presentation

object oriented programming n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Object Oriented Programming PowerPoint Presentation
Download Presentation
Object Oriented Programming

play fullscreen
1 / 100
Object Oriented Programming
152 Views
Download Presentation
zudora
Download Presentation

Object Oriented Programming

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Object Oriented Programming • Programmer thinks about and defines the attributes and behavior of objects. • Often the objects are modeled after real-world entities. • Very different approach than function-based programming (like C).

  2. Object Oriented Programming • Object-oriented programming (OOP) • Encapsulates data (attributes) and functions (behavior) into packages called classes. • So, Classes are user-defined (programmer-defined) types. • Data (data members) • Functions (member functions or methods) • In other words, they are structures + functions

  3. Classes in C++ • Member access specifiers • public: • can be accessed outside the class directly. • The public stuff is the interface. • private: • Accessible only to member functions of class • Private members and methods are for internaluse only.

  4. class Circle { private: double radius; public: Circle() { radius = 0.0;} Circle(int r); void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); }; Circle::Circle(int r) { radius = r; } double Circle::getArea() { return radius * radius * (22.0/7); } double Circle:: getCircumference() { return 2 * radius * (22.0/7); } void main() { Circle c(7); Circle *cp1 = &c; Circle *cp2 = new Circle(7); cout<<“The are of cp2:” <<cp2->getArea(); }

  5. Another class Example • This class shows how to handle time parts. class Time { private: int *hour,*minute,*second; public: Time(); Time(int h,int m,int s); void printTime(); void setTime(int h,int m,int s); int getHour(){return *hour;} int getMinute(){return *minute;} int getSecond(){return *second;} void setHour(int h){*hour = h;} void setMinute(int m){*minute = m;} void setSecond(int s){*second = s;} ~Time(); }; Destructor

  6. Time::Time() { hour = new int; minute = new int; second = new int; *hour = *minute = *second = 0; } Time::Time(int h,int m,int s) { hour = new int; minute = new int; second = new int; *hour = h; *minute = m; *second = s; } void Time::setTime(int h,int m,int s) { *hour = h; *minute = m; *second = s; } Dynamic locations should be allocated to pointers first

  7. void Time::printTime() { cout<<"The time is : ("<<*hour<<":"<<*minute<<":"<<*second<<")" <<endl; } Time::~Time() { delete hour; delete minute;delete second; } void main() { Time *t; t= new Time(3,55,54); t->printTime(); t->setHour(7); t->setMinute(17); t->setSecond(43); t->printTime(); delete t; } Destructor: used here to de-allocate memory locations Output: The time is : (3:55:54) The time is : (7:17:43) Press any key to continue When executed, the destructor is called

  8. Reasons for OOP • Simplify programming • Interfaces • Information hiding: • Implementation details hidden within classes themselves • Software reuse • Class objects included as members of other classes

  9. Modules • First introduced scope rules for data hiding. • Public part consists of variables and functions that are visible outside of the module. • Private part consists of variables and functions visible only within the module. • Modules may span multiple compilation units (files). • Modules generally lack any inheritance mechanism.

  10. Why OO-Programming? • Reduces conceptual load by reducing amount of detail • Provides fault containment • Can’t use components (e.g., a class) in inappropriate ways • Provides independence between components • Design/development can be done by more than one person

  11. The Evolution of OOPS • Global Variables -lifetime spans program execution. • Local Variables - lifetime limited to execution of a single routine. • Nested Scopes - allow functions to be local. • Static Variables - visible in single scope. • Modules - allow several subroutines to share a set of static variables. • Module Types - multiple instances of an abstraction. • Classes - families of related abstractions.

  12. Keys to OO Programming • An instance of a class is know as an Object. • Languages that are based on classes are know as Object-Oriented. • Eiffel • C++ • Modula-3 • Ada 95 • Java

  13. Keys to OO Programming • Encapsulation (data hiding) • Enable programmer to group data & subroutines (methods) together, hiding irrelevant details from users • Inheritance • Enable a new abstraction (i.e., derived class) to be defined as an extension of an existing abstraction, retaining key characteristics • Dynamic method binding • Enable use of new abstraction (i.e., derived class) to exhibit new behavior in context of old abstraction

  14. Classes in C++ • A class definition begins with the keyword class. • The body of the class is contained within a set of braces, { } ; (notice the semi-colon). class class_name { …. …. …. }; Any valid identifier Class body (data member + methods)

  15. Classes in C++ • Within the body, the keywords private: and public: specify the access level of the members of the class. • the default is private. • Usually, the data members of a class are declared in the private: section of the class and the member functions are in public: section.

  16. Classes in C++ class class_name { private: … … … public: … … … }; private members or methods Public members or methods

  17. Class Example • This class example shows how we can encapsulate (gather) a circle information into one package (unit or class) No need for others classes to access and retrieve its value directly. The class methods are responsible for that only. class Circle { private: double radius; public: void setRadius(double r); double getDiameter(); double getArea(); double getCircumference(); }; They are accessible from outside the class, and they can access the member (radius)

  18. Creating an object of a Class • Declaring a variable of a class type creates an object. You can have many variables of the same type (class). • Instantiation • Once an object of a certain class is instantiated, a new memory location is created for it to store its data members and code • You can instantiate many objects from a class type. • Ex) Circle c; Circle *c;

  19. Special Member Functions • Constructor: • Public function member • called when a new object is created (instantiated). • Initialize data members. • Same name as class • No return type • Several constructors • Function overloading

  20. Special Member Functions class Circle { private: double radius; public: Circle(); Circle(int r); void setRadius(double r); double getDiameter(); double getArea(); double getCircumference(); }; Constructor with no argument Constructor with one argument

  21. Implementing class methods • Class implementation: writing the code of class methods. • There are two ways: • Member functions defined outside class • Using Binary scope resolution operator (::) • “Ties” member name to class name • Uniquely identify functions of particular class • Different classes can have member functions with same name • Format for defining member functions ReturnTypeClassName::MemberFunctionName( ){ … }

  22. Implementing class methods • Member functions defined inside class • Do not need scope resolution operator, class name; class Circle { private: double radius; public: Circle() { radius = 0.0;} Circle(int r); void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); }; Defined inside class

  23. class Circle { private: double radius; public: Circle() { radius = 0.0;} Circle(int r); void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); }; Circle::Circle(int r) { radius = r; } double Circle::getArea() { return radius * radius * (22.0/7); } double Circle:: getCircumference() { return 2 * radius * (22.0/7); } Defined outside class

  24. Accessing Class Members • Operators to access class members • Identical to those for structs • Dot member selection operator (.) • Object • Reference to object • Arrow member selection operator (->) • Pointers

  25. class Circle { private: double radius; public: Circle() { radius = 0.0;} Circle(int r); void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); }; Circle::Circle(int r) { radius = r; } double Circle::getArea() { return radius * radius * (22.0/7); } double Circle:: getCircumference() { return 2 * radius * (22.0/7); } The first constructor is called The second constructor is called void main() { Circle c1,c2(7); cout<<“The area of c1:” <<c1.getArea()<<“\n”; //c1.raduis = 5;//syntax error c1.setRadius(5); cout<<“The circumference of c1:” << c1.getCircumference()<<“\n”; cout<<“The Diameter of c2:” <<c2.getDiameter()<<“\n”; } Since radius is a private class data member

  26. Classes • Extends the scope rules of modules to include inheritance. • Should private members of a base class be visible in derived classes? • Should public members of a base class always be public members of a derived class. • How much control should a base class have over its members in derived classes?

  27. C++ Classes • Any class can limit the visibility of its members: • Public members are visible anywhere the class is in scope. • Private members are visible only within the class’s methods. • Protected members are visible inside members of the class and derived classes. • Friend classes are granted exceptions to (some) of the rules.

  28. C++ Classes • Derived classes can further restrict visibility of base class members, but not increase it: • Private members of a base class are never visible in a derived class. • Protected and public members of a public base class are protected or public, respectively, in a derived class. • Protected and public members of a protected base class are protected members of a derived class. • Protected and public members of a private base class are private members of a derived class.

  29. C++ Classes • Derived classes that limit visibility of base class members can restore visibility by inserting a using declaration in its protected or public sections. • Rules in other languages can be significantly different.

  30. Dynamic Method Binding

  31. Member Lookup

  32. Inheritance

  33. public functions private functions private data Encapsulation • Encapsulation Requires that functions, modules and classes: • Have clearly defined external interfaces • Hide implementation details • Encapsulation is all about coupling – coupling happens through interfaces. • Exactly what is an interface?

  34. Abstract Data Types • Modularity • Keeps the complexity of a large program manageable by systematically controlling the interaction of its components • Isolates errors

  35. Abstract Data Types • Modularity (Continued) • Eliminates redundancies • A modular program is • Easier to write • Easier to read • Easier to modify

  36. Abstract Data Types • Procedural abstraction • Separates the purpose and use of a module from its implementation • A module’s specifications should • Detail how the module behaves • Identify details that can be hidden within the module

  37. Abstract Data Types • Information hiding • Hides certain implementation details within a module • Makes these details inaccessible from outside the module

  38. Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not affect task Q

  39. Abstract Data Types • The isolation of modules is not total • Functions’ specifications, or contracts, govern how they interact with each other Figure 3.2A slit in the wall

  40. Abstract Data Types • Typical operations on data • Add data to a data collection • Remove data from a data collection • Ask questions about the data in a data collection

  41. Abstract Data Types • Data abstraction • Asks you to think what you can do to a collection of data independently of how you do it • Allows you to develop each data structure in relative isolation from the rest of the solution • A natural extension of procedural abstraction

  42. Abstract Data Types • Abstract data type (ADT) • An ADT is composed of • A collection of data • A set of operations on that data • Specifications of an ADT indicate • What the ADT operations do, not how to implement them • Implementation of an ADT • Includes choosing a particular data structure

  43. Abstract Data Types Figure 3.4 A wall of ADT operations isolates a data structure from the program that uses it

  44. The ADT List • Except for the first and last items, each item has a unique predecessor and a unique successor • Head or front do not have a predecessor • Tail or end do not have a successor

  45. The ADT List • Items are referenced by their position within the list • Specifications of the ADT operations • Define the contract for the ADT list • Do not specify how to store the list or how to perform the operations • ADT operations can be used in an application without the knowledge of how the operations will be implemented

  46. The ADT List • ADT List operations • Create an empty list • Determine whether a list is empty • Determine the number of items in a list • Add an item at a given position in the list • Remove the item at a given position in the list • Remove all the items from the list • Retrieve (get) item at a given position in the list

  47. The ADT List • The ADT sorted list • Maintains items in sorted order • Inserts and deletes items by their values, not their positions

  48. The ADT List Figure 3.7 The wall between displayList and the implementation of the ADT list

  49. Designing an ADT • The design of an ADT should evolve naturally during the problem-solving process • Questions to ask when designing an ADT • What data does a problem require? • What operations does a problem require?

  50. Designing an ADT • For complex abstract data types, the behavior of the operations must be specified using axioms • Axiom: A mathematical rule • Ex. : (aList.createList()).size() = 0