1 / 40

Object Oriented programming

Object Oriented programming. Paradigm. A Paradigm (para-dime) is a framework containing the basic assumptions, ways of thinking, and methodology that are commonly accepted by members of a scientific community . World View Standard Operating Procedure Process Model.

trista
Download Presentation

Object Oriented programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented programming

  2. Paradigm A Paradigm (para-dime) is a framework containing the basic assumptions, ways of thinking, and methodology that are commonly accepted by members of a scientific community. World View Standard Operating Procedure Process Model

  3. Procedure Oriented Programming So far this semester, we've been concentrating on the POP Paradigm POP focuses on ACTION What does the Program DO?

  4. Procedure Oriented Programming POP Design Involves: - Start with one procedure (function) that represents the entire program - Identify/specify a few sub-steps of the one procedure - Repeat this refinement process for each procedure until each procedure is simplistic (performs one specific task). Each procedure doessomething, and that action is usually reflected in its name as a verb.

  5. Procedure Oriented Programming

  6. Object Oriented Programming A software design paradigm that focuses on building models of Objects to solve a programming problem (instead of focusing on Action)

  7. Object Oriented Programming OOP Design Process: 1. Identify the "natural" Objects in the project description 2. For each Object, identify its: - Name - Data Elements - Operations to/for/with/on ONE object 3. Design the interaction/communication between objects.

  8. Object An Object is composed of: Data Elements and Operations

  9. Data Elements May be simple pieces of data (number, character, string, etc.) OR other objects. They are called: Data Members - in C++ Members - in Java

  10. Operations perform actions on the data elements and/or communicate with other objects They are called: Function Members - in C++ Methods - in Java

  11. Class is a description of an Object, including: - Name - List of Data Members - List of Methods - Access levels on each Member/Method: these define what "the outside world" may directly access.

  12. Class A description of an object A Class is to an Object as a Type is to a Variable as a Blueprint is to a House

  13. Class Purpose: Abstraction! Hide the details from "the outside world" to make the object: - easier to use - protected from misuse by "the outside"

  14. Class Declaration is divided into two parts: Interface - declarative information usually written in .h file (called a Header File) Implementation - Function Member declarations usually written in a .cpp file

  15. Interface Syntax class name{ public: list of public data members list of public function member prototypes private: list of private data members list of private function member prototypes };

  16. Interface Syntax Data Member declaration: is the same as a variable declaration class customer { public: string firstName, lastname; private: int age; float shoesize; };

  17. Interface Syntax Function Member Prototype: same as the header line of a function declaration, with a semicolon on the end. class name{ public: void print(int x); private: intgetStuff(); float calcStuff(float x, float y); };

  18. Interface Syntax Example:in a file called fraction.h class fraction { public: void setNumer(int n); void setDenom(int d); intgetNumer(); intgetDenom(); float getDecimal(); private: intnumer, denom; };

  19. Implementation Syntax Remember from functions that a Prototype is a promise to the compiler that we will write a function, but it is declared elsewhere. The Implementation "fulfills" the promises made in the Interface.

  20. Implementation Syntax For each function member prototyped in the interface: returnTypeclassName :: funMemName(args) { }

  21. Implementation Syntax Example:in a file called fraction.cpp void fraction::set(intn, int d) { } void fraction::getNumer(){ } void fraction::getDenom(){ } float fraction::getDecimal(){ } void fraction::reduce() { }

  22. Implementation Syntax Within all Function Members of a class, all Data Members have Global Scope (but also can be Masked!)

  23. Implementation Syntax void fraction::set(intn, int d) { numer = n; // data mem = arg denom = d; // data mem = arg } float fraction::getDecimal(){ // localVar = expression w/ data mems float dec = (1.0 * numer)/denom; return dec; }

  24. Free Function A function that is not a Method of a class. It does not have classname:: in front of its name. int largest(int a, int b) { if (a > b) return a; else return b; }

  25. Object Declaration After all that work...what have we accomplished?? Essentially, we've invented a new Data Type...except it is more complex than a Type, and so we call it a Class. Now the class name can be used the exact same way as a Type (ex: any way intis used).

  26. Object Declaration void funstuff() { // allocate 3 variables of typedouble, nothing new double a,b,c; // allocate 3 objects of class fraction fraction f1, f2, f3; }

  27. Object Declaration Semantics

  28. Accessing Members: Dot Operator Syntax: object.publicMember object.publicMethod()

  29. Accessing Members: Dot Operator void funstuff() { fraction f1; f1.numer=1; //error: numer is private f1.denom=0; //error: denom is private f1.set(2,3); float d = f1.getDecimal(); cout << d; // prints 0.66666 }

  30. Constructors Construtors are (special) function members where: - their name is exactly the same as the class name - they do not have a return type - they must always be public - they are automatically invoked whenever an object is allocated (declared). Purpose: to initialize data members at the time they are created.

  31. Constructors class fraction { public: fraction(); // constructor prototype private: intnumer; intdenom; }; fraction::fraction() { // constructor declaration numer = 0; denom = 1; }

  32. Constructors void main() { fraction f; // constructor executed NOW! // f.numer = 0, f.denom = 1 fraction fr[3]; // 5 constructors exec. NOW! // fr[0].numer=0, fr[0].denom=1 // fr[1].numer=0, fr[1].denom=1 // fr[2].numer=0, fr[2].denom=1 }

  33. OOP Design Principle All Data Members should be private (unless there is a very good reason to make them public) Code set() and get() Function Members for each Data Member to control access from the "outside world"

  34. get() Function Members get() function members are used when the outside world needs the value of a private data member Solution: Return a COPY of the data member

  35. get() Function Members class fraction { public: intgetNumer(); // return type is the same private: intnumer; // as the data member }; int fraction::getNumer() { return numer; }

  36. set() Function Members set() function members are used when the outside world needs to change the value of a private data member Solution: Pass the new value in as an argument Assign the argument to the data member if the argument's value is valid

  37. set() Function Members class fraction { public: void setDenom(intnewDen); // argument type private: intdenom; // same as member type }; void fraction::setDenom(intnewDen) { if (newDen > 0) { // denom should be > 0 denom = newDen; } // else print error, do something else, or do nothing }

  38. Complete example: class fraction interface //in a file called fraction.h class fraction { public: fraction(); void setNumer(int n); void setDenom(int d); intgetNumer(); intgetDenom(); private: intnumer, denom; };

  39. Complete example: class fraction implementation //in a file called fraction.cpp fraction::fraction() { numer=0; denom=1; } intgetNumer() { return numer; } intgetDenom() { return denom; } void setNumer(int n){ numer = n; } void setDenom(int d) { if (d > 0) { denom = d; } }

  40. Vocabulary

More Related