OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.

Slides:



Advertisements
Similar presentations
This terms course Last term we both worked on learning 2 things –Processing –The concepts of graphics etc. This term will focus more on the basic concepts.
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Defining New Types Lecture 21 Hartmut Kaiser
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
1 Introduction to Object Oriented Programming Chapter 10.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Variables, Operators, and Expressions
Math operations 9/19/16.
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Andrew(amwallis) Classes!
OOP - Object Oriented Programming
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
CO1301: Games ts 2015 Lecture 6 Vectors Dr Nick Mitchell (Room CM 224)
Concepts of Object Oriented Programming
CS 215 Final Review Ismail abumuhfouz Fall 2014.
numerical coefficient
Andy Wang Object Oriented Programming in C++ COP 3330
Inheritance and Polymorphism
4.1 Vectors in Physics Objective: Students will know how to resolve 2-Dimensional Vectors from the Magnitude and Direction of a Vector into their Components/Parts.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
An Introduction to Inheritance
Chapter 3 Inheritance © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Object Oriented Programming
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Inheritance Basics Programming with Inheritance
Unreal Engine and C++ We’re finally at a point where we can start working in C++ with Unreal Engine To get everything set up for this properly, we’re going.
Number and String Operations
Editable Attributes (1)
Theory of Computation Turing Machines.
We’re slowly working our way towards classes and objects
Key Words and Introduction to Expressions
We’re moving on to more recap from other programming languages
Topic 1: Problem Solving
Computer Programming with JAVA
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Kinematics Vectors and Motion
Goal: To understand the mathematics that will be necessary for this course which you do not get in a math class Objectives: Learning how to use Significant.
Topic 1: Problem Solving
Tonga Institute of Higher Education
Solving Linear Equations
CISC/CMPE320 - Prof. McLeod
COP 3330 Object-oriented Programming in C++
Fundaments of Game Design
Overview of C++ Polymorphism
Interfaces.
Review of Previous Lesson
Review of Previous Lesson
Object-Oriented PHP (1)
Output Manipulation.
CMPE212 – Reminders Assignment 2 due next Friday.
Visibilities and Static-ness
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
C++ Object Oriented 1.
Lecture Set 9 Arrays, Collections, and Repetition
Presentation transcript:

OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation (with the use of public and private) The other three are what we’re going to look at in this lesson

We’re going to work with vectors in this lesson Not the list type vector But the maths type vector That’s because Unreal uses vector mathematics for a lot of things So it’ll be worth looking at them now

Vectors But first, some notation that we’re going to have to learn Vectors are one-dimensional data structures that contain any number of values Usually relating to the space we’re working with 2 values: 2D space (x, y) 3 values: 3D space (x, y, z)

Vectors 𝑥 𝑦 or 𝑥 𝑦 Vectors are often shown in brackets, like so Either direction is fine – they both mean the same 𝑥 𝑦 or 𝑥 𝑦

Vectors themselves only contain data Like any other data structure However, they can be used in different ways Here are a few examples

Vectors (Position) We can use a vector to represent a position of something in space For example, this point here We can create a vector to represent its position 5 11

Vectors (Direction) We can also use a vector to represent a direction These always have a start and end The start here is the origin 0 0 The end is where the origin plus the direction – the result here is roughly 7 7 Note the 𝑥 and 𝑦 parts form sides of a right-angled triangle? This will be useful knowledge later

Vectors (Addition) We can add vectors together, and we’ll usually add them in these two combinations A direction to a position This moves the element in that direction A direction to a direction When we add a ‘force’ to an element

Vectors (Addition) Adding vectors works like adding any two numbers We just do it for every component of the vector Take this example A circle is at position 10 20 and is moving in direction 5 3 . We can see the circle’s new position by adding them together. New Position= 10 20 + 5 3 = 10+5 20+3 = 15 23

Vectors (Magnitude) All vectors have a magnitude Easier to visualise with direction vectors Think of the magnitude as the overall strength of the vector The larger the magnitude, the more affect it will have on other vectors For example, adding a stronger direction to a position makes the element move further away

Vectors (Magnitude) When thinking of vectors as right-angled triangles, the magnitude is the hypotenuse Since we know the adjacent (the 𝑥 component) And we know the opposite (the 𝑦 component) We can use Pythagoras’ Theorem to find the hypotenuse (the magnitude)

We often note a vector in the following format Vectors (Magnitude) We often note a vector in the following format And the magnitude as follows 𝐴 = 3 4 𝐴 = 𝑥 2 + 𝑦 2 = 3 2 + 4 2 = 25 =5

Vectors (Unit Vector) Finally, we can think of direction vectors as a combination of Their general direction And their magnitude The magnitude affects how far we move in that general direction But how do we get the general direction?

Vectors (Unit Vector) That’s where unit vectors come in These are special vectors that have a magnitude of 1 Essentially removing any strength from a vector Leaving only their general direction Unit vectors as essential in any mildly-complex 2D/3D game As they’re used to store the direction of entity movement

Vectors (Unit Vector) We can calculate a unit vector by dividing a vector by its magnitude Here’s an example taking the vector from earlier 𝑈 = 𝐴 𝐴 𝑈 = 3 4 5 = 3 5 4 5 = 0.6 0.8

Vectors (Unit Vector) This tells us, for every one magnitude, we move 0.6 along the 𝑥 axis, and 0.8 along the 𝑦 axis If this was the direction of a spaceship, we could multiply this by its speed To calculate the change in position

Vectors Ex A spaceship is at 20 x, 40 y in space, and is moving in -0.86 x and 0.51 y at 100 pixels/s Calculate its new position after 2 seconds Calculate the unit vector for the vector 14 −3 (to two-decimal places, e.g. 0.34)

Creating a Vector Class (1) Let’s create a struct to represent a 2D vector We’ll give it double variables for its 𝑥 and 𝑦 components As well as functions for Constructors Adding a vector Scaling a vector Calculating the magnitude Turning it into a unit vector Called normalising

Creating a Vector Class (1) Note that we have two constructors here? One giving 𝑥 and 𝑦 values One giving a reference to a vector The second has been made in case we want to copy a vector later on Two functions with the same name like this? Known as overloading

Creating a Vector Class (1) We’ll then implement these functions in a source file Including math.h for pow and sqrt functions

Creating a Vector Class (1) Ex Create a new Empty C++ Project Create a header file for your Vector2D struct Give it instance variables for its 𝑥 and 𝑦 components Give it functions for Constructing (either with two doubles, or one vector) Adding (another vector to it) Scaling (multiplying itself by a double) Getting its magnitude Normalising itself (turning itself into a unit vector) Implement each of these functions in a source file

Creating a Vector Class (2) Ex Add another function to the struct called GetDistanceTo Accepts a vector as a parameter This returns the distance form the current vector to the parameter Can calculate distance between vectors by Subtracting their 𝑥 and 𝑦 components from each other Making a vector out of the results, and calculating its magnitude

We can use this vector class in a video game environment Game Entities (1) We can use this vector class in a video game environment Vectors for the position of entities Vectors for the directions of entities We need to start by creating a class for an Entity

Game Entities (1) We’ll do this in a separate header file We’ll give this class A constructor and destructor A function for moving Functions for getting the position, and setting the direction Private variables for the position, direction, and speed

Game Entities (1) We can then implement these functions in a separate source file Be careful with changing the direction We’re replacing the old vector pointer Which means we need to delete it We also normalise it after setting it (to remove any magnitude from it)

Game Entities (1) Then we can test this out by creating an Entity Outputting its current position Moving it a bit Then outputting its position again

Game Entities (1) When we run this, we can see the car move In the direction we’ve set

Game Entities (1) This movement based off of A current position A direction A speed A timeframe to move in Is one that is commonly used in 2D/3D video games to create ‘accurate’ physical movements Can tweak the Entity class to include acceleration (which can factor in forces like gravity)

Game Entities (1) Ex Create a header file for your entities Create an Entity class in it that has A constructor and a destructor Functions for moving, getting its position, and setting its direction Private variables for its position and direction (pointers), and its speed Implement these functions in a separate source file Create a source file with the main() function in it Inside it, create an Entity object and set its direction Output its position before and after moving it for 5 seconds

For example, we could create a Spaceship Game Entities (2) With the base class Entity set up, we can create some specific entities for a game For example, we could create a Spaceship That is an Entity as well But also has a string for the spaceship’s name

Game Entities (2) Making one class use another class like this is known as inheritance We can create this relationship at any point by adding a colon after the name of the class Where we put the name of the parent class

Game Entities (2) This means that the Spaceship is an Entity It inherits all the functions and variables from the Entity class But, can’t use them unless they are public or protected If they are private, we can’t use them Also need to include public after the colon Otherwise we can’t use Entity functions (like Move())

Game Entities (2) If we try and implement the constructor for our Spaceship, we get a problem It complains that we do not have a default constructor for Entity

Game Entities (2) Because we’re technically creating an Entity as well, we need to run its constructor We can do that after the signature of the constructor Typically done on a new line We can’t have a space between the colon and the Entity name

We can then carry on with the constructor for the Spaceship Game Entities (2) We can then carry on with the constructor for the Spaceship By storing its name

Let’s repeat the Entity movement from before Game Entities (2) Let’s repeat the Entity movement from before This time for the Spaceship All using the same functions

Game Entities (2) Here’s what the output looks like Since we’re running the same functions, we could store any Entity and Spaceship object in a single vector list

Game Entities (2) Ex Add a Spaceship class to the entities header file Make it inherit from Entity Make sure the inheritance is public Give it a string variable for its name And a constructor which accepts a string parameter for its name In the entities source file, implement the constructor Include a colon, followed by the Entity constructor, in the Spaceship’s constructor Then store the name in the instance variable In the main() method, repeat the movement code This time for a Spaceship

We may want to change how a function works for its sub-classes Overriding Functions We may want to change how a function works for its sub-classes Moving an entity may be different to moving a spaceship This is easily done, as C++ supports the idea of overriding functions

Overriding Functions To override a function, we first have to redeclare it in the sub-class We’re saying “I’m overriding Move()”

Overriding Functions We can then change how it works in its implementation If we still want to run the original as well as doing other things, we can call the Entity version as well At any point in the function

When we run this, we’ll see the added output when moving the Spaceship Overriding Functions When we run this, we’ll see the added output when moving the Spaceship Even though we’re using the same function

Overriding Functions Ex Add a declaration for the Move function in the Spaceship class Implement it in the entities source file Output a message to the console in the overridden function Then call the Entity version of this function Run your program and make sure this message is visible in the console

END OF UNIT!