computing for animation 2 ca-3 - bournemouth...
TRANSCRIPT
Computing For Animation 2
CA-3
Dr. Hammadi Nait-CharifMedia School
Bournemouth University
nccastaff.bournemouth.ac.uk/hncharif/CA2
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
Course Organization
Study hours 180h
Contact − Lectures: Two hours weekly− Supervised Labs: two hours weekly
Linkage: Major Project
Learning Outcomes
An understanding of advanced concepts & techniques of Object Oriented Programming
The ability to apply OOP through the use of C++
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 lifecycle phases
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.)
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)
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
What is an Object
color
draw()
erase()
move()
getColor()
setColor()
Identity
state
behaviour
ObjectShape
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
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
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)
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
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)
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
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
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
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'
Constructors There are three basic types of constructor
The default constructorUser defined ConstructorThe Copy Constructor
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
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
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;
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;
};
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
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;
};
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);
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;
}
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 deallocate 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 lifecycle of the object by
− being able to control both the creation and destruction of the object
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
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
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;
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];}
}
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;
}
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;}