G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture...

26
G51PGP Programming Paradigms Lecture OO1 – Intro and Zombies program 1

Transcript of G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture...

Page 1: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

G51PGP

Programming Paradigms

Lecture OO1 – Intro and

Zombies program

1

Page 2: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

This Lecture

• The Object Oriented Paradigm

• A ‘simple’ one-function C program

– Revising or seeing the basic functionality

• Procedural decomposition

– And some problems

• Grouping data members together in a struct2

Page 3: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

Object Oriented Paradigm

• A style/way of programming

– A way of thinking about the problem being solved

– Does not enable us to do anything we couldn’t do in C

– Does make some things a lot easier to do

• Can use the OO paradigm in many languages

– We will see some of the features in C

– Then see how expanded C (i.e. C++) can make things easier (next OO lecture)

– And then see the simpler Java language (3rd OO lecture)

• Both Java and C++ provide OO constructs that C doesn’t

– Aim of first two OO lectures: Understand what Java is really doing, using C, to link it to previous experience

3

Page 4: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

Outline of the Object Oriented side

C codeMove to C++

C with classes (?)

Java

Hello World!

Text and GUI

Member functions,

statics, encapsulation

Things you

already know

OO DesignInheritance

Virtual functions

Simple Java

concurrency

Threads,

synchronized,

volatile

Specialisation,

polymorphismDecomposition

Java UI

Common classes

Useful OO design principles

and design patterns

How to make a ‘good’ design

(Also: ideal vs fast in Java)

Swing,

Collection

Principles and

class ideas are

not unique to

Java4

Page 5: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

Usual Object Oriented Language Features

• Objects (and classes)

• Encapsulation (methods and data together)

• Data hiding (restrict access to data)

• Composition (stronger than aggregation)

• Inheritance (specialisation, reuse, ‘is-a’)

• Abstraction (and interfaces)

• Polymorphism (dynamic dispatch, late binding)

– Behaviour only known at runtime (compare vs FP)

5

Page 6: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

Usual Object Oriented Language Features

• Objects (and classes) – via structs

• Encapsulation (methods and data together)

• Data hiding (restrict access to data)

• Composition (stronger than aggregation)

• Inheritance (specialisation, reuse, ‘is-a’)

• Abstraction (and interfaces)

• Polymorphism (dynamic dispatch, late binding)

– Behaviour only known at runtime (compare vs FP)

6

Page 7: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

Benefits of OO (why do it)

• Re-use

– Easily re-use, adapt and/or modify existing work

• Ease of change

– Modularity should mean that components can be

swapped out more easily

• Ease debugging

– Huge problem with data – ‘what changed it?’

• Easier to enforce constraints

• Easier to validate changes

– Modular nature and encapsulation help to localise

problems – i.e. ‘where in the code is the problem?’ 7

Page 8: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

This lecture: getting started…

• Getting from C to Java

– What is actually happening in Java – bottom-up

– Starting from the C programming that you should

know

8

Page 9: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

Zombies game

9

Page 10: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

Demo :

Playing the game

Looking at the code

10

Page 11: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

Zombies Function

• A program in one function (ignoring library functions like printf() etc )

#include <stdio.h>

int main( int argc, char* argv[] )

{

int i = 0; /* etc for local vars */

while ( !player_dead && … ) /* Loop until end */

{

}

return 0;

} 11

Page 12: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

Key C features : pre-processor

#include : Include header files with

#include < stdio.h >

#define : Intelligent find and replace

#define MAX_ZOMBIES 40

• Optional debugging:

#define debug2( a,b,c ) printf (a,b,c )

#define debug2( a,b,c )

.c : Source

code file.o : Object

code file

Executable

file

LinkerPre-processor C Compiler

Single pass -> needs declarations

12

Page 13: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

Variables, control statements and functions

Local vars : A lot of local vars in main

int i = 0;int zombie_x [MAX_ZOMBIES];

Loops (while , for ) and conditionals (if )

malloc() Allocate block of memory

free() Free allocated memory

printf() Output some text

scanf() Read input (a character here)13

Page 14: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

Zombies Function

• A program in one function

– Ignoring library functions like printf() etc

• Only one function, so must be simple

• Is this really easy to understand?

• How could it be easier?

– (Think of last semester’s module content)

14

Page 15: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

Algorithmic decomposition

• Also called functional decomposition or procedural decomposition

– Functional decomposition is completely different to functional programming : a decomposition not a software design paradigm

• Structured programming paradigm that you have already seen uses it

– Object oriented paradigm also uses it, within objects

• Splitting a function into smaller functions which do parts of the larger function

• So let’s try it…15

Page 16: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

Some function possibilities…

• Initialise – set up initial state, allocate memory

• Game Loop

– Store current position content

– Add zombies and player to map

– Display map

– Ask for player action

– Handle player action

– Check for player in pit

– Move zombies

– Check for zombie eating player

• Display final message (success or failure)

• Deinitialise – free the memory 16

Page 17: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

Demo :

Breaking code into functions

With global data

17

Page 18: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

Global data is not ideal

• In any program which has to be maintained,

global data is usually a bad idea

• If you change something then you need to know

what effects this will have

• If you change some global variable, it could be

used from anywhere!

• How do you create two copies of the program,

to run at once? They would share the data!

18

Page 19: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

Structs and pointers

• You can create structured

data in C, using a struct

• You can then use this data

• Pointers will be important

struct A a;struct A* pa = &a;a.x = 1;a.y = 2;printf( "%d,%d\n", a.x, a.y );printf( "%d,%d\n", (*pa).x, (*pa).y );printf( "%d,%d\n", pa->x, pa->y );

struct A

{

int x;

int y;

};

19

Page 20: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

Important things to know for C …

• In C you use the . operator to access a struct

member

• You can take the address of a struct (and many

other things) in C

– use the & operator

• You can create a pointer of type struct * and

You can store an address in a pointer

struct A* pa = &a;

• You can use -> to avoid the (*x). Construct

printf ("% d,%d\n", pa->x, pa->y); 20

Page 21: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

Demo :

Wrapping things in a struct and

passing in a pointer to it

21

Page 22: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

We can decompose further but…

• Breaking up a task often increases overall cost (group project?)– One person doing a task has no communication issues

– Two people doing it involves coordination and communication

• When a task is broken down, an interface between the components needs to be specified/designed, showing how they link together– E.g. The function name and parameters

– Each side can change independently• Caller can change without function internals changing

• Internals can change without changing caller code

– Getting the interface correct is vital for success

• And information needs to be passed between the components– The variable values are passed

• And there is extra code to write– Function header/footer – extra lines of code wrapping the body of the

function

– Code for the function call itself – an extra line

– This is not unusual (but can be minimised by an OO language)

• The increased cost is worth it for maintainability22

Page 23: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

Summary so far…

• One aim was to remind you of basic C code– Including structs and pointers

• Splitting up a program into multiple parts can make it easier to understand– It can also promote reuse / avoid repetition in code

• We could make the data globals– Trust me that global data is a bad thing in general

– We will see some of the advantages of other approaches later

• We could put all of our data into a struct and pass a single pointer into functions– That’s an important concept to understand…

– … but we can do much better than that…23

Page 24: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

An alternative decomposition

• Object oriented decomposition considers the ‘world’ as a set of interacting objects

• From a bottom-up point of view, objects are state (data) and methods (functions) to act on this state– Important note: We are going to be creating the

example program in a specific way in order to see the relationship between Java and C

– Don’t actually ever try to write a program in this way

• To understand objects and what is done with them, we will try to simulate Java/C++ objects in C– We will do with objects exactly what we just did with

one struct, passing pointers to the structs into functions

• Over the next few lectures we will use some simple extensions to C (in the C++ language) to link to Java

24

Page 25: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

What we will do…

• Identify useful data which should go together

• Put it together into structs

• Create functions which manipulate that data

• Only the functions for that data should access that data (can’t easily enforce that here)

• A pointer to the data to act upon is passed to the function

• We saw an example using one struct for now

• More useful examples next lecture

25

Page 26: G51PGP Programming Paradigmspszja/pgp1516/g51pgp-lecture-oo1.pdf · Programming Paradigms Lecture OO1 – Intro and Zombies program 1. This Lecture • The Object Oriented Paradigm

Next OO lecture

• Breaking up our data into many different structs

– Containing related data

• And passing pointers around to functions

• Having dedicated functions for each struct

• This is the most basic form of object oriented programming (sort-of)

• OO languages provide us with many features to automate things and reduce the amount of code needed to do this

– And many other extra features26