Table of Contents
Video - Intro to OOP:
Video:
<iframe src="https://www.youtube.com/embed/Dm8dQ-1QoTI" width="270" height="480" title="YouTube video player" frameborder="0" allowfullscreen> </iframe>
Overview:
- Terms to know: Object Oriented Programming, abstraction, encapsulation, coupling, cohesion, inheritance, composition, accessibility levels, public, private, protected, UML
- Concepts: Be able to read UML diagrams
1. Programming paradigms
Programming paradigms (pronounced "pair-uh-dimes") are ways we can classify different programming languages based on features they have available or the style programs are written. Different paradigms have popped up over the life of computers as programming languages grow and evolve.
- Machine code:
At the beginning, computers were programmed with machine code, where you
work directly with instructions supported by the hardware - for example,
add,sub, storing data in registers, and other relatively simple commands. - Procedural Languages: Eventually, those commands were abstracted into higher-level languages, where one command in, say, C, could "translate" to several machine-code instructions. Languages like C, Fortran, Algol, BASIC, and C are known as Procedural Languages, where these programs would describe a procedure to follow and instructions are executed one-at-a-time in a specific order (top-to-bottom, or calling a function and returning).
- Object Oriented Programming: Languages like C supported our basic control flow and using functions, but did not include classes - a way to make more sophisticated data types. A class is a structure that can store its own member variables and member functions. A variable whose data type is from some defined class is known as an object.
There are other programming paradigms, but we are going to focus on Object Oriented Programming (OOP) now since it's a large part of using C++ and other languages like Java, C#, and Python.
1.1. Introduction to Object Oriented Programming
Object Oriented Programming (or "OOP") is probably one of the most popular ways to design and implement software these days. In programming, an object is some sort of structure that stores its own data (variables) and functionality (functions). We try to design our programs as a collection of objects that interact with each other to get some job done.
In C++ and many other languages functions always end with parentheses ( ), which is how you can quickly identify them. Sometimes, there is data in the parentheses - these are input parameters. Functions can also return data as well, so each interaction between objects can pass inputs between objects, and return data between objects.
1.2. Design ideals
Designing programs in an OOP style helps us pursue certain design goals:
- Abstraction: Hiding complexity in the design, creating an "interface" for
the user, generalizing functionality to a more "digestible" form. Think of a
button on any device: If you open up the device, there's a lot actually going
on, but we just see the simple button and don't have to worry about the rest.
- Certain functions are made public that other programmers can use to interface with the object.
- The other programmers don't need to worry about the inner-workings of the object in order to use it.
- The developer of the class can modify how the internals work without breaking the public interface.
- Helps protect the data within the class from being accessed or modified by things it shouldn't.
- Encapsulation: Also goes along with creating an "interface", though in this regard we are encapsulating related pieces together. We can store data (variables) with the functions that operate on that data (functions) all within a class object.
- Loose coupling: Ideally, different objects in a program shouldn't have their functionality tied to other objects too closely; we want to reduce inter-dependence between objects. When objects are more independent from each other, they are loosely coupled.
- High cohesion: When we design our objects, we shouldn't just throw everything and the kitchen sink into one object. To design an object with high cohesion means that everything inside the object belongs in that object. Reduce the clutter.
Review 1. Following good design, a single class should represent
○ Multiple items / ○ A single item
Review 2. It is good to mark member variables as…
○ public / ○ private
…so that…
○ fewer regions of code touch that data
○ the data is accessible to all items in the program
○ we maintain job security because nobody knows what our classes do
Review 3. We want two different classes to be
○ highly dependent on each other
○ independent from each other
2. Inheritance and composition
Sometimes we have multiple objects that are similar but still have different sets of data (member variables) - think of a File, how we might have an Image File, Sound File, Video File… Each file has a name, file extension, and size, but the actual file contents are different.
And sometimes we have an object that is best represented by being "inside" another object - think of a House with an array of Room objects. These are the two ideas between inheritance, an "is-a" relationship, and composition, a "has-a" relationship.
With inheritance:
- Variables and functions from a class can be inherited by child classes. This means that the child class will have these same variables and functions, without having to re-write it all.
- We use this to create specialized versions of some generic object, like a GameObject, PlayerObject, and NPCObject, or a Shape, Square, and Circle.
With composition:
- One class is a member variable of another class, and is generally a private member.
- Often this means that the internal class can't be accessed by outside functions, and the "containing" class needs to have some kind of interface layer to allow interactions with that internal item.
- Some people argue that you should prefer using composition over inheritance because the act of composing another class keeps it "modularized"; that class' features are encapsulated within a variable, and not more inherited variables added onto the pile-o-variables a class has.
2.1. Accessibility levels
When we're designing our objects we don't want to give unrestricted access to every part of the program. We generally create a class and it is responsible for certain things, including modifying its own data. This helps us protect the integrity of the data. Each member variable and function belonging to a class can be marked as one of these three accessibility levels:
- public:
- Any part of the program can access these members. This is usually used for the member functions of a class.
- private:
- These members can only be accessed by the class itself, from within its functions.
- protected:
- Similar to private except that classes that inherit from the class we're defining will also have access to protected members. More on this when we cover inheritance.
- Review 4. Identify which description refers to inheritance and which
description refers to composition.
Class B has all the public/protected member variables and functions of Class A.
○ Inheritance / ○ Composition
Class B is a member variable of Class A
○ Inheritance / ○ Composition
Review 5. Why is inheritance and composition used?
○ It looks fanicer
○ It makes the codebase harder to read
○ It reduces having duplicate code
Review 6. What can access public members of a class? (Select ALL correct answers)
□ The class' functions
□ The class' childrens' functions
□ Functions that do not belong to the class' family
Review 7. What can access protected members of a class? (Select ALL correct answers)
□ The class' functions
□ The class' childrens' functions
□ Functions that do not belong to the class' family
Review 8. What can access private members of a class? (Select ALL correct answers)
□ The class' functions
□ The class' childrens' functions
□ Functions that do not belong to the class' family
3. Modeling C++ classes using UML
UML (Unified Modeling Language) is a way to diagram classes, their member variables and functions (methods), and relationships between those classes. It is meant as a language-agnostic way to describe your codebase or how something should be designed.
3.1. Describing a single object
A UML diagram for a single class looks like this:
| Player |
|---|
- x : int |
- y : int |
- image : Texture |
+ Setup() : void |
+ Move(velX: float, velY: float): void |
+ BeginJump() : void |
+ BeginAttack() : void |
+ Draw(screen:Window) : void |
Here's how to read it:
- The TOP region is the name of the class.
- The MIDDLE region is its member variables.
- The BOTTOM region is its member methods/functions.
- The
-minus sign signifiesprivateaccessibility level, which means these members can only be accessed by this class' functions. - The
+plus sign signifiespublicaccessibility level, which means these members can be accessed by this class' functions or any functions external to it (like belonging to another class, or inmain()). - A
#pound sign signifiesprotectedaccessibility level (used with inheritance, which we will cover more later).
3.2. Describing relationships between objects
UML diagrams are also used to show the relationship between classes.
In a diagram that shows a relationship between objects you will see arrows going between classes.
- A line coming FROM a class and ending in a triangle arrow to another class shows an inheritance relationship, with the former inheriting from the latter.
- A line coming FROM a class and ending in a diamond shape to another class shows a composition relationship, with the former being contained within the latter.
Using UML diagrams can be (relatively) quick to write out and to read, compared to just giving someone a .h header file or all the code itself. Often, companies will have some kind of global UML diagram of all of the components in their codebase and how they interact - developers are expected to update this diagram as they add/remove classes or members of classes.
- Review 9. In UML…
- Variables are shown as the (○ top / ○ middle / ○ bottom) section.
- Functions are shown as the (○ top / ○ middle / ○ bottom) section.
- The (○ + / ○ -) sign denotes "public accessibility".
- The (○ + / ○ -) sign denotes "private accessibility".