slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Object Oriented Programming (OOP) 1. PowerPoint Presentation
Download Presentation
Object Oriented Programming (OOP) 1.

Object Oriented Programming (OOP) 1.

220 Vues Download Presentation
Télécharger la présentation

Object Oriented Programming (OOP) 1.

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

  1. Object Oriented Programming (OOP) 1.

  2. 0. Admin. • Return reports. • Take in any more program 6’s • Hand-out program 7 (intro to OOP).

  3. 1. Object Oriented Programming (OOP). • “I would like to object in the strongest possible terms to oriented programming” • --- Brigadier General, Sir Lefty Vole-Strangler (Mrs.). • Did you know there is a secret, Masonic society that swears allegiance to the Object paradigm? Sometimes, they give themselves away… • Why is OOP so popular? • OOP can be understood as the latest in a series of developments in programming methodology.

  4. In standard, procedural programming… • Data and functions are viewed as separate entities,e.g. in C++: • FUNCTIONS: • int Do_Something_Stanley (float X, float Y) { } • void Thats_A_Fine_Mess (float Z) { } • int main () { • float Num1, Num2, Answer; // data • Answer = Do_Something_Stanley (Num1, Num2); • Thats_A_Fine_Mess (Answer); • } // end main.

  5. What problems does this cause? • Functions do not automatically know which data they apply to. • To get functions to do anything, we must pass data to them as parameters. • But this causes problems….

  6. What problems does this cause? • As programs get complex, • (1) there is too much parameter passing, creating opportunities for error. • (2) there are too many separate functions, not grouped together in any meaningful way, making it unclear which functions apply to which data.

  7. Analogy of problem. • Consider the 2 sheds of Sven Svenson, the Swedish Lutheran craftsman. • Sven has set up his sheds as follows. • Shed 1 (Data) Shed 2 (Functions) • Wood WW-tools • Metal MW-tools

  8. What are the consequences? • In order for Sven to produce any wood or metal crafts, he must constantly pass data (wood or metal) to the tool shed. • And, it is quite likely that he will sometimes use the wrong tools as they are always collected together, e.g. use a hacksaw for metal on wood.

  9. This illustrates the problems of • 1) Too much parameter passing. • And • 2) Confusion over which tools (functions) apply to which materials (data). • But how could this problem be solved? • How could Sven Svenson rearrange his sheds?

  10. Reorganization. • Shed 1Shed 2 • Wood & Metal & • WW-tools MW-tools • Idea: bundle the data together with the functions used to process it. • How does this solve the problems?

  11. How it works. • 1) Now data is only sent to a shed as raw input (Set function), and data returns as the final product (Get function). • For all other functions, no parameter passing is necessary, because the data is already in the right place! • So parameter passing is reduced.

  12. How it works. • 2) The right tools are stored with the data they apply to, making it much less likely that we use the wrong tool for the job. • In OOP, only member functions know about the data in the class (rather like members of a club or society); non-members normally cannot access the data.

  13. 2. History of programming languages. • 1) In the beginning….low-level languages (e.g. ASSEMBLY). These languages directly manipulate the computer’s hardware. • Problems: • A) Require user to understand H/W in detail. • B) Programs are H/W specific, hence not portable (bad for commerce).

  14. 2. History of programming languages. • 2) Move to High-Level unstructured languages (e.g., ALGOL, BASIC, FORTRAN). • These allow unconditional transfers of control (GOTO / JMP) and Spaghetti code. • E.g. early air-traffic control programs,

  15. 2. History of programming languages. • 3) Move to High-Level structured languages (e.g., C, COBOL, PASCAL) • Use of control structures—one way in and out of a block of code. • But problem of data protection (global variables). • Solution: subprograms / parameter passing. • But then too much parameter passing / too many separate functions.

  16. 2. History of programming languages. • 4) Object oriented programming. • 1) Bundle data with functions / methods. • 2) Minimize parameter passing. • 3) Protect data even more---only member functions can directly access it, unless special permission is granted (e.g. friend functions).

  17. 3. OOP languages. • OOP languages can be pure or hybrid. • C++ is a hybrid (allows non-OOP C code, useful because of huge amount of legacy code, as well as classes). E.g., the Windows API is written in C, bundled into C++ classes (Microsoft Foundation Classes). • C#, Java, Smalltalk are pure OOP languages (everything is a class).

  18. Advantages of OOP. • 1) Coherent bundles of “nouns” and “verbs.” Analogy: an athlete’s performance data (nouns) and skill set (verbs). • 2) Functions are grouped with the data they apply to. • 3) Functions in a class automatically “know” about the data in the class, so except for Set and Get, parameter passing is unnecessary.

  19. Advantages of OOP. • 4) Data is protected from the client (who must use the member functions, rather than directly accessing it) and from other classes. • 5) Enhanced code re-use: big class libraries can be instantiated by any program. • 6) Simplifies applications by using client / server model. • 7) Classes can be customized.

  20. What is a class? • A class is a bundle, like a record except it contains both data members and member methods. • The member methods are usually “public” (visible to the client program—the interface / communication mechanism). • The data members are usually “private” (invisible to the client which can change the data only via the member methods).

  21. Class Transform Set Get Client Program

  22. The Class is the type of an active thing (object), but… • we must instantiate the class. • Analogy: • a whale (object) is a mammal (class). • a burger (object) is fast food (class). • Socrates (object) is a philosopher (class). • John Madden (object) is a commentator (no class).

  23. Example: Class_Division.cs. • (1) Divide is a separate class from the class that contains Main(); • (2) Data members vs. Member functions; • (3) Private vs. Public; • (4) Constructors; • (5) Set, Transform and Get methods. • (6) Client vs. class data.

  24. Relate to last program. • (1) Using Class_Division.cs as a model Create a Future class with a constructor, Set, Transform and Get method. • How many items need to be sent to the class via the Set method? How many need to be calculated? How many should be returned via the Get method? (2) Code a second class including Main() to instantiate Future and use it to find the future value of an investment.