Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented programming

Similar presentations


Presentation on theme: "Object Oriented programming"— 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 does something, 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 An Object is composed of:
Data Elements and Operations

9 Data Elements Data Members - in C++ Members - in Java
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 A description of an object
Class A description of an object A Class is to an Object as a Type is to a Variable 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: int getStuff(); 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); int getNumer(); int getDenom(); float getDecimal(); private: int numer, 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: returnType className :: funMemName(args) { }

21 Implementation Syntax
Example: in a file called fraction.cpp void fraction::set(int n, 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(int n, 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 int is used).

26 Object Declaration void funstuff() { // allocate 3 variables of type double, 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 }

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: int numer; int denom; }; 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 All Data Members should be private
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: int getNumer(); // return type is the same private: int numer; // 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(int newDen); // argument type private: int denom; // same as member type }; void fraction::setDenom(int newDen) { 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); int getNumer(); int getDenom(); private: int numer, denom; };

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

40 Vocabulary Term Definition Class
a description of an object, including a name, list of data members, list of function members (methods), and the access levels of each. Object allocated instantiation of a Class, containing the members and methods defined in the Class with the designated access levels. Public designates data members and methods that CAN be directly accessed by the code using the object Private designates data members and methods that can NOT be directly access by the code using the object. Paradigm A framework containing the basic assumptions, ways of thinking, and methodology that are commonly accepted by members of a scientific community. Members Data elements of a class/object. Methods Functions/operations of a class/object. Interface Declarative portion of a class: defines the name, members, list of methods, and their access levels. Implementation Coding of the Methods of a class; where the methods are written. Constructor Function member with the same name as the class, used to initialize objects of the class. Free Function A function that is not part of a class.


Download ppt "Object Oriented programming"

Similar presentations


Ads by Google