computing for animation 2 ca-3 - bournemouth...

34
Computing For Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University [email protected] nccastaff.bournemouth.ac.uk/hncharif/CA2

Upload: others

Post on 24-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Computing For Animation 2

CA-3

Dr. Hammadi Nait-CharifMedia School

Bournemouth University

[email protected]

nccastaff.bournemouth.ac.uk/hncharif/CA2

Page 2: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Overview

Prerequisite: Programming CP2 Text:

− Effective C++ Scott Meyers − Advanced C++                            James O. Coplien  − Thinking C++  Bruce Eckel

http://www.mindview.net/Books/DownloadSites− Object Oriented Programming Timothy Budd

Credits: 20 Grading: A programming Project

Page 3: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Course Organization

Study hours 180h

Contact − Lectures: Two hours weekly− Supervised Labs: two hours weekly

Linkage: Major Project

Page 4: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Learning Outcomes

An understanding of advanced concepts & techniques of Object Oriented Programming

The ability to apply OOP through the use of C++

Page 5: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Object Oriented Programming

Systems are modelled as sets of interacting objects

Proponents claim that Object Orientation − Is intuitive and abstract− Manages Complexity− Aids communication− Scales up to large systems− Allows consistent representation throughout 

the life­cycle phases

Page 6: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

What is an Object

A tangible or visible 'thing' e.g. a person 'An object represents an individual identifiable 

item, unit or entity, either real or abstract, with a well defined role in the problem domain'

This is good news for graphics programming as most things we want to design are objects or things that act on objects (e.g.  a Shape is an abstract object which can be realised into a more concrete objects such as sphere, cube cone etc.)

Page 7: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Object Structure

What Defines an Object ?− A boundary e.g. a car body or building exterior 

− An internal state e.g. Blood pressure, waiting time

− Behavior ­­ what it does 

− A Unique identity (as with instances, entities or roles) 

Page 8: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Objects have states and behaviour State : height, volume, width, colour, level of 

detail, position Behavior: is how an object acts and reacts An object may support 'services' that are 

invoked when a message is sent An object may invoke 'services' in other objects 

by passing a message to the object

Page 9: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

What is an Object

color

draw()

erase()

move()

getColor()

setColor()

Identity

state

behaviour

ObjectShape

Page 10: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Classes

Representing abstract data types using C++ We need the following C++ keywords

− class keyword to define a class (ADT)− private keyword to define 'hidden' parts of the 

class− public  keyword to define the visible interface of 

the class− The scope resolution operator (::) used to link 

together classes and methods

Page 11: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

The Anatomy of a class

A Class has two parts (just like an ADT)− A private (hidden) part− A public interface

The public part defines the behavior of the object (methods)

The private part contains the data It is normal practice to put attributes in the 

private part of the class where they can only be accessed by methods

Page 12: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

The Anatomy of a class

 It is possible to put attributes (data) in the public part of the class but this undermines encapsulation.

It is also possible to put methods in the private part of a class but these may not be used as an external interface (however they may be used by other methods)

Page 13: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

A Class Example

#ifndef __ExampleClass_H_#define __ExampleClass_H_class ExampleClass{

private :   int i;

public:        void SetValue(int value_in)        int GetValue() };  //end of class#endif

Page 14: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Some observations on the class

A class definition is terminated with a semicolon (unlike a function definition)

 The members of a class are private by default, so the private keyword could  be omitted. 

Methods are usually know as 'member functions' in C++.  setValue is of void type because it doesn't return a value  getValue returns and int (the value of i)

Page 15: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Classes and Objects

A class is a 'blueprint' for all Objects of a certain type 

class defines the attributes (data)  and the operations (methods)

The class may be considered as an object factory

allowing us to instantiate objects of the same type.

A single class allows us to make as many instances of a class as required 

Page 16: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

State Identity and Behavior A class is defined by the following 3 attributes

A unique name, Attributes and Methods

An Object is defined by Identity, State and Behavior

The properties of the Class relate to the properties of the object

Class attributes == Object state data Class Methods == Object behavior

However Class name and Object identity are more complex

Page 17: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Difference between Classes and Objects

Once defined, a class exists all the time the program is running, Objects may be created and destroyed

For a single class there may be any number of objects instantiated at any one time

A class has a unique name, attributes and methods. An Object has identity, state and behavior

A class provides no state values for the object attributes. These can only be given values when applied to specific objects

Page 18: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Constructors

When an object is created there are certain processes which must take place

Instantiation always involves the allocation of memory for the objects state data.

The methods do not require any memory as these are consistent for all objects of the class and are handled in the class itself.

The special method which allocates the memory for an object is know as the 'constructor'

Page 19: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Constructors There are three basic types of constructor

The default constructorUser defined ConstructorThe Copy Constructor

Page 20: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

The Default Constructor

The default constructor takes no parameters (or return type)

It performs no processing on the object data just memory allocation

It will always be called by the compiler if no user defined constructor is provided

The default constructor is not referred to by the programmer in the class definition

Page 21: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

User Defined Constructor

These constructors may be used to pass parameter values to the object

These may be used to set default object values

It is possible to have more than one constructor in a class passing different parameters

This is known as overloading and gives more flexibility to the way the object can be instantiated

Page 22: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

The Copy Constructor

The copy constructor allows a new object to be created as a copy of an existing object.  e.g.                 

Therefore all of the objects state data is copied to the new object

This object will be identical to the first except for it's identity

This remains until methods are called on the new object to change the data

ExampleClass object2 = object1;

Page 23: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Coding a Constructor A C++ constructor has three important aspects

It takes the same name as the class It may take arguments It cannot return a value

For example the Point2D Class constructor would be

 class Point2D{........... public :     Point2D();...........};

Point2D::Point2D(){

x = 0;y = 0; 

};

Page 24: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

What to put in the Constructor

Usually initialization code is put in the constructor

As shown in the previous slide

Now every time the object is created the current point is set to a useful default value (0,0)

However sometimes this is not applicable to the program requirements

Page 25: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Parametrized Constructor As mentioned previously parameters can be passed 

to the constructor for example class Point2D{........... public :

Point2D(); Point2D(float Xin, float Yin);

...........};Point2D::Point2D(float Xin, float Yin);{

x = Xin;y = Yin; 

};

Page 26: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Default Parameter Values

The above example may be modified to give a default value for example

This means that if the Point2D constructor is called with no value it will default to [0.0,0.0] however this method may be overridden by using another value.

For example

Will construct a new class with a default value of [4.0,12.0]

       Point2D(float Xin=0.0,Yin=0.0);

       Point2D point(4.0,12.0);

Page 27: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

User Defined Copy Constructor

A programmer may also create a copy constructor by passing a reference to the object to be copied for example

& denotes pass by reference and the const prefix indicates that the object being passed in must not be modified  

The code for the copy constructor may look like this

Point2D(const Point2D &tocopy);

Point2D::Point2D(const Point2D &tocopy)

{

  x = tocopy.x;

y = tocopy.y;

}

Page 28: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Clean-up and Garbage Collection The default constructor is used to allocate memory for 

the creation of the Object

The default destructor is used to de­allocate this memory

With a static object this is done implicitly however for dynamic objects this must be done by the programmer

This now allows us to determine exactly the life­cycle of the object by

− being able to control both the creation and destruction of the object

Page 29: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Defining a destructor method

The destructor method may be defined to add extra functionality to the destruction of an object

It has certain characteristic which mark it out from the other methods as follows

It takes the same name as the class, preceded 

by the 'tilde' character ~It cannot take argumentsIt cannot return a value

Page 30: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Copy Constructor with dynamic memory

If we have dynamic memory in our classes we must copy the contents of the memory into a new class when using the =

This is done with the copy constructor as follows

Page 31: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Copy Constructor with dynamic memory

#ifndef __TEST_CLASS_H__ #define __TEST_CLASS_H__class TestClass

{

private :

int *mem;

int Size;

public :

TestClass();

TestClass(int Size);

TestClass(const TestClass& in);

~TestClass();

void Print(void);

int size(void){return Size;}

int& operator [] (int index) { return mem[index]; } 

};

#endif;

Page 32: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Copy Constructor with dynamic memory

#include "TestClass.h"#include <iostream>#include <string.h> // for memcpyusing namespace std;

TestClass::TestClass(){

Size=0;mem=NULL;

}TestClass::TestClass(int size){

mem  = new int[size];Size = size;

}TestClass::TestClass(const TestClass& in){

Size=in.Size;mem = new int[Size];for(int i=0; i<Size; ++i){

mem[i]=in.mem[i];}

}

Page 33: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Copy Constructor with dynamic memory TestClass::~TestClass(){   if(mem !=NULL)

  delete [] mem;

}

void TestClass::Print(void){  for(int i=0; i<Size; i++)    cout<<"Mem["<<i<<"]="<<mem[i]<<endl;

}

Page 34: Computing For Animation 2 CA-3 - Bournemouth Universitynccastaff.bmth.ac.uk/hncharif/CA2/lecture1.pdf · Animation 2 CA-3 Dr. Hammadi Nait-Charif Media School Bournemouth University

Copy Constructor with dynamic memory #include <iostream>#include "TestClass.h"

using namespace std;

int main(void){

TestClass A(10);

for(int i=0; i<A.size(); i++)A[i]=i; 

A.Print();

TestClass B=A;B.Print();

return 1;}