150 likes | 486 Views
Object Oriented Programming. A brief review of what you should know about OOP. Programming Paradigms. What is a paradigm? A model, pattern, or typical example A world-view used in doing something What are programming paradigms? Virtual computer models for writing code
E N D
Object Oriented Programming A brief review of what you should know about OOP
Programming Paradigms • What is a paradigm? • A model, pattern, or typical example • A world-view used in doing something • What are programming paradigms? • Virtual computer models for writing code • Examples of programming paradigms • Machine code, assembly language • Structured programming; OOP
Structured programming • Early paradigms focused on the hardware • This approach focused on execution sequence, program flow • Many programmers wrote “spaghetti” code • In 1968, Edgar Dijkstra published a letter: • GOTO considered harmful • Structured logic more readable: • If-then-else, while-loops, single-entry/exit blocks
The OO Paradigm • In 1976, Niklaus Wirth (neeklouse veert) Algorithms+Data Structures = Programs • Don’t focus on just algorithms (code flow) • Balance data & algorithm emphasis at every level of programming • Use modules that include data structure and operations
OOP objects • OOP is based on objects that have data and operations (methods) • The data describes the object’s • State, and • Properties • The methods describe • What you can do to the object, and • What you can have it do for you
Prototype and Instance Objects • Most OO models allow dealing with objects in two ways: • Prototype objects are models for lots of similar objects: car, person, pet, package • Instance objects are specific examples: my 1996 red Civic, Jay Leno, Sarah’s puppy, my Christmas gift from my son Eric • In some languages, prototype objects are called classes and/or types
Object interaction • Objects interact with each other using • Messages! • In some languages, messages are implemented by function calls • When you click on an object to get a context menu and then select a choice from the menu, you are “sending it a message” to perform that method (operation) • It may cause a return message (return value)
Features of OOP • Programming languages that support OOP provide 3 features: • Encapsulation • Polymorphism • Inheritance
Encapsulation • Encapsulation is the OOP feature that provides container modularity 2 ways - • Grouping: allowing related data and a set of operations to be grouped (remember Wirth?) • Hiding: protecting the internal workings of the “black box” and defining the external interface or how you can interact with the box
Polymorphism • Polymorphism is language support for using the same name for similar operations on different kinds of objects • It would be very disappointing to need to learn separate names or operators for adding ints, floats, and doubles instead of just using “+” for each of them
Polymorphism in C++ • Polymorphism is provided in C++ by • Automatic type conversions • Function & operator overloading • Inheritance • Member function overriding • Virtual functions and pure virtual functions • Templates • + overload resolution is involved in all of these • We will use each of them in this class!
Inheritance • In OOP, inheritance is a form of “factoring” • An expression can be easier to calculate if you separate out a common factor • If a program uses the same code several places, “factor” it out as a function to call • If a bunch of objects have shared subsets of data and operations, “factor” out a base class (and only maintain it one place)
Inheritance and Sub-classes • The usual reason why objects share data properties and operations is sub-classing • Cars, trucks, busses, boats, and airplanes share many properties and operations because they are all vehicle sub-classes • Students, faculty, and staff share object properties and operations because they are all people
C++ Features to Review • Here are some OOP features of C++ • Public, private, and protected in classes • Defining classes using inheritance • Visibility rules in derived classes (inheritance) • Function overloading • Operator overloading (including I/O: << and >>) • Differences for member functions • You should review all of these!