PROGRAMMING
Teaching real stuff
INTRODUCTION
INTRODUCTION
Problem: How to communicate between human and machine
CREATED BY DAVE DE BREUCK
INTRODUCTION
Problem: How to communicate between human and machine
We need to speak the language of machine
CREATED BY DAVE DE BREUCK
INTRODUCTION
Problem: How to communicate between human and machine
We need to speak the language of machine
Intermediate solution are programming languages
CREATED BY DAVE DE BREUCK
INTRODUCTION
Code example:
//Code to draw a rectangle
bool GameEngine::DrawRect(RECT2 rect, double strokeWidth)
{
if (!CanIPaint()) return false;
D2D1_RECT_F d2dRect = D2D1::RectF((FLOAT)
(FLOAT)
(FLOAT)
(FLOAT)
rect.left,
rect.top,
rect.right,
rect.bottom);
m_RenderTargetPtr->DrawRectangle(d2dRect, m_ColorBrushPtr,
(FLOAT) strokeWidth);
return true;
}
CREATED BY DAVE DE BREUCK
INTRODUCTION
Programming language -> compiled (translated)
CREATED BY DAVE DE BREUCK
INTRODUCTION
Programming language -> compiled (translated)
To a readable language by the computer
CREATED BY DAVE DE BREUCK
INTRODUCTION
Programming language -> compiled (translated)
To a readable language by the computer
Each file is compiled to a corresponding .obj file
CREATED BY DAVE DE BREUCK
INTRODUCTION
Programming language -> compiled (translated)
To a readable language by the computer
Each file is compiled to a corresponding .obj file
Link all files together (build)
CREATED BY DAVE DE BREUCK
INTRODUCTION
Programming language -> compiled (translated)
To a readable language by the computer
Each file is compiled to a corresponding .obj file
Link all files together (build)
Result is an executable file (.exe): contains computer language
CREATED BY DAVE DE BREUCK
INTRODUCTION
FirstExerc.cpp
GameEngine.cpp
Compile
obj
GameWinMain.cpp
Compile
obj
EXE
CREATED BY DAVE DE BREUCK
Compile
obj
LINK
Libraries
Lib
or
Dll
CODE LAYOUT
CODE LAYOUT
Organize your code
CREATED BY DAVE DE BREUCK
CODE LAYOUT
Organize your code
Different pieces of c++ code
CREATED BY DAVE DE BREUCK
CODE LAYOUT
Organize your code
Different pieces of c++ code
Classes
CREATED BY DAVE DE BREUCK
CODE LAYOUT
Organize your code
Different pieces of c++ code
Classes
Structs
CREATED BY DAVE DE BREUCK
CODE LAYOUT
Organize your code
Different pieces of c++ code
Classes
Structs
Methods
CREATED BY DAVE DE BREUCK
CODE LAYOUT
Organize your code
Different pieces of c++ code
Classes
Structs
Methods
Variables
CREATED BY DAVE DE BREUCK
CODE LAYOUT
Organize your code
Different pieces of c++ code
Classes
Structs
Methods
Variables
CREATED BY DAVE DE BREUCK
CODE LAYOUT
Not only code is separated into pieces
CREATED BY DAVE DE BREUCK
CODE LAYOUT
Not only code is separated into pieces
Source files
CREATED BY DAVE DE BREUCK
CODE LAYOUT
Not only code is separated into pieces
Source files
Header files
CREATED BY DAVE DE BREUCK
CODE LAYOUT
Header files
CREATED BY DAVE DE BREUCK
CODE LAYOUT
Header files
Overview of what we will find inside the source file
CREATED BY DAVE DE BREUCK
CODE LAYOUT
Header files
Overview of what we will find inside the source file
Visible for everybody
CREATED BY DAVE DE BREUCK
CODE LAYOUT
Header files
Overview of what we will find inside the source file
Visible for everybody
Used for declaration of variables, methods, etc.
CREATED BY DAVE DE BREUCK
CODE LAYOUT
Source files
CREATED BY DAVE DE BREUCK
CODE LAYOUT
Source files
Converted into zero and ones
CREATED BY DAVE DE BREUCK
CODE LAYOUT
Source files
Converted into zero and ones
Only visible for people who have the source files
CREATED BY DAVE DE BREUCK
CODE LAYOUT
Source files
Converted into zero and ones
Only visible for people who have the source files
Used to initialize variables and define methods etc.
CREATED BY DAVE DE BREUCK
CODE LAYOUT
Header file
CREATED BY DAVE DE BREUCK
CODE LAYOUT
Source file
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
CREATING A PROJECT
Search for Visual Studio in your start menu
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Or type devenv in your start menu
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Once Visual Studio is started you create a new project
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Once Visual Studio is started you create a new project
Click File
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Once Visual Studio is started you create a new project
Click File
New
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Once Visual Studio is started you create a new project
Click File
New
Project
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Select the correct project type
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Select the correct project type
Installed Templates
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Select the correct project type
Installed Templates
Visual C++
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Select the correct project type
Installed Templates
Visual C++
Win32 Project (NOT the Win32 console application)
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Name your project
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Name your project
Assign a location to save your project
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Name your project
Assign a location to save your project
Uncheck the create directory for solution checkbox
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Application setting
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Application setting
Click on application settings
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Application setting
Click on application settings
Make sure the application type is set to Windows application
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Application setting
Click on application settings
Make sure the application type is set to Windows application
Select empty project in the additional options
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Project structure in Windows Explorer
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Project structure in Windows Explorer
Navigate to the location where you created your project
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Project structure in Windows Explorer
Navigate to the location where you created your project
Find the game engine files we provided to you
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Project structure in Windows Explorer
Navigate to the location where you created your project
Find the game engine files we provided to you
Copy ( NOT move) them to your project folder
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Modifying Filters in Solution Explorer
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Modifying Filters in Solution Explorer
Re-open visual studio
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Modifying Filters in Solution Explorer
Re-open visual studio
Find the solution explorer
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Modifying Filters in Solution Explorer
Re-open visual studio
Find the solution explorer
You will notice a few folders under the project, they are called filters
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Filters
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Filters
Subdivisions to organize files inside your project
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Filters
Subdivisions to organize files inside your project
Do not affect your projects folder
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Divide your project inside 3 parts
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Divide your project inside 3 parts
Engine files
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Divide your project inside 3 parts
Engine files
Resource files
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Divide your project inside 3 parts
Engine files
Resource files
Game files
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Divide your project inside 3 parts
Engine files
Resource files
Game files
Therefore rename the following folders
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Divide your project inside 3 parts
Engine files
Resource files
Game files
Therefore rename the following folders
Header files into Engine files
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Divide your project inside 3 parts
Engine files
Resource files
Game files
Therefore rename the following folders
Header files into Engine files
Source files into Game files
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Adding files
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Adding files
Right click on a filter in the solution explorer
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Adding files
Right click on a filter in the solution explorer
Add
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Adding files
Right click on a filter in the solution explorer
Add
Existing Item
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Add the following files to your project
AbstractGame.cpp
AbstractGame.h
Big.ico
GameEngine.cpp
GameEngine.h
GameWinMain.cpp
GameWinMain.h
Resource.h
Small.ico
X.cpp
X.h
X.rc
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Rename following files
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Rename following files
X.rc to resource.rc
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Rename following files
X.rc to resource.rc
X.cpp to Main.cpp
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Rename following files
X.rc to resource.rc
X.cpp to Main.cpp
X.h to Main.h
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Replace all files in the correct filter
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Replace all files in the correct filter
Main.cpp and Main.h inside Game files
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Replace all files in the correct filter
Main.cpp and Main.h inside Game files
Resource.rc, Big.ico, Small.ico, resource.h inside Resource files
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Replace all files in the correct filter
Main.cpp and Main.h inside Game files
Resource.rc, Big.ico, Small.ico, resource.h inside Resource files
The others inside Engine files
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Replace X by the name of the class
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Replace X by the name of the class
Open Main.cpp, Main.h, GameWinMain.cpp
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Replace X by the name of the class
Open Main.cpp, Main.h, GameWinMain.cpp
Search and replace (Ctrl + h) the X with Main
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Replace X by the name of the class
Open Main.cpp, Main.h, GameWinMain.cpp
Search and replace (Ctrl + h) the X with Main
Make sure you check Match case
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Replace X by the name of the class
Open Main.cpp, Main.h, GameWinMain.cpp
Search and replace (Ctrl + h) the X with Main
Make sure you check Match case
Make sure you check Match whole word
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Replace X by the name of the class
Open Main.cpp, Main.h, GameWinMain.cpp
Search and replace (Ctrl + h) the X with Main
Make sure you check Match case
Make sure you check Match whole word
Search inside the entire solution
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Fill in your personal data
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Fill in your personal data
Open Main.h
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Fill in your personal data
Open Main.h
Add your data at the top of the file
//---------------------------------------------// Student data
// Name: Dave De Breuck
// Group: 3DAEGAMEDEV
//----------------------------------------------
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Fill in your personal data
Open Main.cpp
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Fill in your personal data
Open Main.cpp
Find the method Main::GameInitialize(HINSTANCE hInstance)
void Main::GameInitialize(HINSTANCE hInstance)
{
// Set the required values
AbstractGame::GameInitialize(hInstance);
GAME_ENGINE->SetTitle("Main - De Breuck Dave - 3DAEGAMEDEV");
GAME_ENGINE->RunGameLoop(false);
// Set the optional values
GAME_ENGINE->SetWidth(640);
GAME_ENGINE->SetHeight(480);
//GAME_ENGINE->SetKeyList(String(WASD") + (TCHAR)VK_SPACE);
}
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Test your project
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Test your project
Rebuild the solution (CTRL + ALT + F7)
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Test your project
Rebuild the solution (CTRL + ALT + F7)
Run the program (F5)
CREATED BY DAVE DE BREUCK
CREATING A PROJECT
Congratulations your created your first project!
CREATED BY DAVE DE BREUCK
VARIABLES
VARIABLES
What is animation
CREATED BY DAVE DE BREUCK
VARIABLES
What is animation
Still image
CREATED BY DAVE DE BREUCK
VARIABLES
What is animation
Still image
Repeatedly shown
CREATED BY DAVE DE BREUCK
VARIABLES
What is animation
Still image
Repeatedly shown
Small change are made
CREATED BY DAVE DE BREUCK
VARIABLES
CREATED BY DAVE DE BREUCK
VARIABLES
To be able to affect changes
CREATED BY DAVE DE BREUCK
VARIABLES
To be able to affect changes
Access the state of animation
CREATED BY DAVE DE BREUCK
VARIABLES
To be able to affect changes
Access the state of animation
We access the state by using variables
CREATED BY DAVE DE BREUCK
VARIABLES
To be able to affect changes
Access the state of animation
We access the state by using variables
We create a variable for each value that needs to be changed during runtime
CREATED BY DAVE DE BREUCK
VARIABLES
What is a variable
CREATED BY DAVE DE BREUCK
VARIABLES
What is a variable
Piece of memory
CREATED BY DAVE DE BREUCK
VARIABLES
What is a variable
Piece of memory
Assigned to a name
CREATED BY DAVE DE BREUCK
VARIABLES
What is a variable
Piece of memory
Assigned a name
Value can be stored inside the memory
CREATED BY DAVE DE BREUCK
VARIABLES
Using variables
CREATED BY DAVE DE BREUCK
VARIABLES
Using variables
Write the name of the variable to access the value that is stored inside the memory
CREATED BY DAVE DE BREUCK
VARIABLES
Using variables
Write the name of the variable to access the value that is stored inside the memory
To change the value we use the assignment operator = "
CREATED BY DAVE DE BREUCK
VARIABLES
Example
//Creating 2 variables named "x" and "y"
//Storing a value inside both variables
int x = 1;
int y = 2;
//Creating a variable named "result"
//Storing the sum of variable "x" and "y"
//inside this variable
int result = x + y;
CREATED BY DAVE DE BREUCK
VARIABLES
Exercise Open Laptop
CREATED BY DAVE DE BREUCK
VARIABLES
Variable types
CREATED BY DAVE DE BREUCK
VARIABLES
Variable types
Integral numbers
CREATED BY DAVE DE BREUCK
VARIABLES
Variable types
Integral numbers
Decimal numbers
CREATED BY DAVE DE BREUCK
VARIABLES
Variable types
Integral numbers
Decimal numbers
Boolean values
CREATED BY DAVE DE BREUCK
VARIABLES
Variable types
Integral numbers
Decimal numbers
Boolean values
Text
CREATED BY DAVE DE BREUCK
VARIABLES
Variable types
Integral numbers
Decimal numbers
Boolean values
Text
CREATED BY DAVE DE BREUCK
VARIABLES
Variable types
Integral numbers
Decimal numbers
Boolean values
Text
For every kind of data there is a kind of variable also know as type
CREATED BY DAVE DE BREUCK
VARIABLES
Variable types
Integral numbers
Decimal numbers
Boolean values
Text
For every kind of data there is a kind of variable also know as type
Type determines
CREATED BY DAVE DE BREUCK
VARIABLES
Variable types
Integral numbers
Decimal numbers
Boolean values
Text
For every kind of data there is a kind of variable also know as type
Type determines
Value of the variable
What operations can be performed on the value
CREATED BY DAVE DE BREUCK
VARIABLES
Variable type categories
CREATED BY DAVE DE BREUCK
VARIABLES
Variable type categories
Primitive variables
CREATED BY DAVE DE BREUCK
VARIABLES
Variable type categories
Primitive variables
Composite variables
CREATED BY DAVE DE BREUCK
VARIABLES
Variable type categories
Primitive variables
Composite variables
Abstract variables
CREATED BY DAVE DE BREUCK
VARIABLES
Primitive variables
CREATED BY DAVE DE BREUCK
VARIABLES
Primitive variables
Integer
CREATED BY DAVE DE BREUCK
VARIABLES
Primitive variables
Integer
Double
CREATED BY DAVE DE BREUCK
VARIABLES
Primitive variables
Integer
Double
Float
CREATED BY DAVE DE BREUCK
VARIABLES
Primitive variables
Integer
Double
Float
Bool
CREATED BY DAVE DE BREUCK
VARIABLES
Primitive variables
Integer
Double
Float
Bool
Char
CREATED BY DAVE DE BREUCK
VARIABLES
Primitive variables
Integer
Double
Float
Bool
Char
CREATED BY DAVE DE BREUCK
VARIABLES
Integers
CREATED BY DAVE DE BREUCK
VARIABLES
Integers
Without fractional or decimal component
CREATED BY DAVE DE BREUCK
VARIABLES
Integers
Without fractional or decimal component
Range { , -2, -1, 0, 1, 2, }
CREATED BY DAVE DE BREUCK
VARIABLES
Integers
Without fractional or decimal component
Range { , -2, -1, 0, 1, 2, }
32 bits ( 4 bytes of memory )
CREATED BY DAVE DE BREUCK
VARIABLES
Integers
Without fractional or decimal component
Range { , -2, -1, 0, 1, 2, }
32 bits ( 4 bytes of memory )
Signed or unsigned
CREATED BY DAVE DE BREUCK
VARIABLES
Float
CREATED BY DAVE DE BREUCK
VARIABLES
Float
With decimal component
CREATED BY DAVE DE BREUCK
VARIABLES
Float
With decimal component
Range { , -0.2, -0.1, 0.0, 0.1, 0.2, }
CREATED BY DAVE DE BREUCK
VARIABLES
Float
With decimal component
Range { , -0.2, -0.1, 0.0, 0.1, 0.2, }
32 bits ( 4 bytes of memory )
CREATED BY DAVE DE BREUCK
VARIABLES
Float
With decimal component
Range { , -0.2, -0.1, 0.0, 0.1, 0.2, }
32 bits ( 4 bytes of memory )
Singed or unsigned
CREATED BY DAVE DE BREUCK
VARIABLES
Double
CREATED BY DAVE DE BREUCK
VARIABLES
Double
With decimal component
CREATED BY DAVE DE BREUCK
VARIABLES
Double
With decimal component
Range { , -0.2, -0.1, 0.0, 0.1, 0.2, }
CREATED BY DAVE DE BREUCK
VARIABLES
Double
With decimal component
Range { , -0.2, -0.1, 0.0, 0.1, 0.2, }
64 bits ( 8 bytes of memory )
CREATED BY DAVE DE BREUCK
VARIABLES
Double
With decimal component
Range { , -0.2, -0.1, 0.0, 0.1, 0.2, }
64 bits ( 8 bytes of memory )
Singed or unsigned
CREATED BY DAVE DE BREUCK
VARIABLES
TCHAR
CREATED BY DAVE DE BREUCK
VARIABLES
TCHAR
Used for single characters
CREATED BY DAVE DE BREUCK
VARIABLES
TCHAR
Used for single characters
Define it with single quotes ( character = 'a )
CREATED BY DAVE DE BREUCK
VARIABLES
TCHAR
Used for single characters
Define it with single quotes ( character = 'a )
ASCII (American Standard Code for Information Interchange)
CREATED BY DAVE DE BREUCK
VARIABLES
TCHAR
Used for single characters
Define it with single quotes ( character = 'a )
ASCII (American Standard Code for Information Interchange)
8 bits ( 1 byte of memory )
CREATED BY DAVE DE BREUCK
VARIABLES
CREATED BY DAVE DE BREUCK
VARIABLES
String
CREATED BY DAVE DE BREUCK
VARIABLES
String
Sequence of characters
CREATED BY DAVE DE BREUCK
VARIABLES
String
Sequence of characters
Define it with double quotes ( string sequence_characters = "aaaa"; )
CREATED BY DAVE DE BREUCK
SELECTIONS
SELECTIONS
A selection is a condition inside your code
CREATED BY DAVE DE BREUCK
SELECTIONS
Example
Adventure game
CREATED BY DAVE DE BREUCK
SELECTIONS
There are 3 types of conditions
CREATED BY DAVE DE BREUCK
SELECTIONS
There are 3 types of conditions
If-else
CREATED BY DAVE DE BREUCK
SELECTIONS
There are 3 types of conditions
If-else
Switch-case
CREATED BY DAVE DE BREUCK
SELECTIONS
There are 3 types of conditions
If-else
Switch-case
?:
CREATED BY DAVE DE BREUCK
SELECTIONS
If-else
CREATED BY DAVE DE BREUCK
SELECTIONS
If-else
Uses a Boolean expression
CREATED BY DAVE DE BREUCK
SELECTIONS
If-else
Uses a Boolean expression
Rational expression
CREATED BY DAVE DE BREUCK
SELECTIONS
If-else
Uses a Boolean expression
Rational expression
Logical expression
CREATED BY DAVE DE BREUCK
SELECTIONS
If-else
Uses a Boolean expression
Rational expression
Logical expression
Combination of both
CREATED BY DAVE DE BREUCK
SELECTIONS
Rational expression
<
>
<=
>=
==
!=
CREATED BY DAVE DE BREUCK
SELECTIONS
Logical expression
&&
||
!
CREATED BY DAVE DE BREUCK
SELECTIONS
Exercise - rational expressions
m_Year = 2014
CREATED BY DAVE DE BREUCK
SELECTIONS
m_Year < 1900
m_Year == 1850
m_Year != 1850
m_Year >= 1800
m_Year <= 1850
CREATED BY DAVE DE BREUCK
SELECTIONS
m_Year < 1900
m_Year == 1850
m_Year != 1850
m_Year >= 1800
m_Year <= 1850
CREATED BY DAVE DE BREUCK
SELECTIONS
m_Year < 1900
m_Year == 1850
m_Year != 1850
m_Year >= 1800
m_Year <= 1850
CREATED BY DAVE DE BREUCK
SELECTIONS
m_Year < 1900
m_Year == 1850
m_Year != 1850
m_Year >= 1800
m_Year <= 1850
CREATED BY DAVE DE BREUCK
SELECTIONS
m_Year < 1900
m_Year == 1850
m_Year != 1850
m_Year >= 1800
m_Year <= 1850
CREATED BY DAVE DE BREUCK
SELECTIONS
m_Year < 1900
m_Year == 1850
m_Year != 1850
m_Year >= 1800
m_Year <= 1850
CREATED BY DAVE DE BREUCK
SELECTIONS
Exercise logical expressions
m_Year = 2014
CREATED BY DAVE DE BREUCK
SELECTIONS
true && true
true && false
true || true
true || false
!true
CREATED BY DAVE DE BREUCK
SELECTIONS
true && true
true && false
true || true
true || false
!true
CREATED BY DAVE DE BREUCK
SELECTIONS
true && true
true && false
true || true
true || false
!true
CREATED BY DAVE DE BREUCK
SELECTIONS
true && true
true && false
true || true
true || false
!true
CREATED BY DAVE DE BREUCK
SELECTIONS
true && true
true && false
true || true
true || false
!true
CREATED BY DAVE DE BREUCK
SELECTIONS
true && true
true && false
true || true
true || false
!true
CREATED BY DAVE DE BREUCK
SELECTIONS
! ( m_Year == 1995 || 2001 )
CREATED BY DAVE DE BREUCK
SELECTIONS
! ( m_Year == 1995 || 2001 )
! ( false || true)
CREATED BY DAVE DE BREUCK
SELECTIONS
! ( m_Year == 1995 || 2001 )
! ( false || true)
!(true)
CREATED BY DAVE DE BREUCK
SELECTIONS
! ( m_Year == 1995 || 2001 )
! ( false || true)
!(true)
false
CREATED BY DAVE DE BREUCK
SELECTIONS
The syntax
CREATED BY DAVE DE BREUCK
SELECTIONS
The syntax
Simple statement (without brackets)
CREATED BY DAVE DE BREUCK
SELECTIONS
The syntax
Simple statement (without brackets)
CREATED BY DAVE DE BREUCK
int m_number = 5;
if (m_number == 5)
m_number += 1;
SELECTIONS
The syntax
Simple statement (without brackets)
Compound statement (with brackets)
CREATED BY DAVE DE BREUCK
int m_number = 5;
if (m_number == 5)
m_number += 1;
SELECTIONS
The syntax
Simple statement (without brackets)
Compound statement (with brackets)
CREATED BY DAVE DE BREUCK
int m_number = 5;
if (m_number == 5)
m_number += 1;
int m_number = 6;
if (m_number == 6)
{
m_number += 5;
m_number += 10;
}
SELECTIONS
Watch out!
int m_number = 10;
if (m_number<5)
m_number = 5;
m_number += 1;
m_number = ?
CREATED BY DAVE DE BREUCK
SELECTIONS
Watch out!
int m_number = 10;
if (m_number<5)
m_number = 5;
m_number += 1;
m_number = 11
CREATED BY DAVE DE BREUCK
SELECTIONS
If-else syntax
if (condition1)
{
// execute if condition1 == true
}
else if (condition2)
{
// execute if condition1 == false
// and
// execute if condition2 == true
}
else
{
// execute if condition1 == false
// and
// execute if condition2 == false
}
CREATED BY DAVE DE BREUCK
METHODS
METHODS
Used to split up a problem (for which the program is the solution) into sub-problems
CREATED BY DAVE DE BREUCK
METHODS
Using them goes into 2 steps
CREATED BY DAVE DE BREUCK
METHODS
Using them goes into 2 steps
Write the method (what does it do?)
CREATED BY DAVE DE BREUCK
METHODS
Using them goes into 2 steps
Write the method (what does it do?)
Call the method (when does it start?)
CREATED BY DAVE DE BREUCK
METHODS
Example
void Demo::DrawStaff()
{
GAME_ENGINE->DrawLine(20, 20, 200, 20);
GAME_ENGINE->DrawLine(20, 40, 200, 40);
GAME_ENGINE->DrawLine(20, 60, 200, 60);
GAME_ENGINE->DrawLine(20, 80, 200, 80);
GAME_ENGINE->DrawLine(20, 100, 200, 100);
}
CREATED BY DAVE DE BREUCK
METHODS
Syntax of a method
CREATED BY DAVE DE BREUCK
METHODS
Syntax of a method
Return value
CREATED BY DAVE DE BREUCK
METHODS
Syntax of a method
Return value
Scope (Demo::)
CREATED BY DAVE DE BREUCK
METHODS
Syntax of a method
Return value
Scope (Demo::)
Name
CREATED BY DAVE DE BREUCK
METHODS
Syntax of a method
Return value
Scope (Demo::)
Name
()
CREATED BY DAVE DE BREUCK
METHODS
Syntax of a method
Return value
Scope (Demo::)
Name
()
Body (enclosed with { } )
CREATED BY DAVE DE BREUCK
METHODS
Example
Return value
Scope
Name
void Demo::GamePaint(RECT rect)
{
DrawStaff();
}
Body
CREATED BY DAVE DE BREUCK
()
METHODS
Example
Return value
Scope
Name
()
void Demo::GamePaint(RECT rect)
{
DrawStaff();
}
Body
CREATED BY DAVE DE BREUCK
Parameter
METHODS
Methods with parameters
CREATED BY DAVE DE BREUCK
METHODS
Methods with parameters
Parameters a.k.a. arguments
CREATED BY DAVE DE BREUCK
METHODS
Methods with parameters
Parameters a.k.a. arguments
Those things between the round brackets
CREATED BY DAVE DE BREUCK
METHODS
Methods with parameters
Parameters a.k.a. arguments
Those things between the round brackets
void Demo::GamePaint(RECT rect)
rect of type RECT is the parameter of this method
CREATED BY DAVE DE BREUCK
METHODS
Used to call the same method
CREATED BY DAVE DE BREUCK
METHODS
Used to call the same method
Have a different outcome
CREATED BY DAVE DE BREUCK
METHODS
void Demo::DrawStaff(int x, int
{
GAME_ENGINE->DrawLine(x,
GAME_ENGINE->DrawLine(x,
GAME_ENGINE->DrawLine(x,
GAME_ENGINE->DrawLine(x,
GAME_ENGINE->DrawLine(x,
}
void Demo::GamePaint(RECT rect)
{
DrawStaff(40, 40);
}
CREATED BY DAVE DE BREUCK
y)
y, x + 200, y);
y + 8, x + 200, y + 8);
y + 16, x + 200, y + 16);
y + 24, x + 200, y + 24);
y + 32, x + 200, y + 32);
METHODS
A methods return value
CREATED BY DAVE DE BREUCK
METHODS
A methods return value
Optional (return type = void)
CREATED BY DAVE DE BREUCK
METHODS
A methods return value
Optional (return type = void)
Can be a primitive/composite/abstract value
CREATED BY DAVE DE BREUCK
METHODS
A methods return value
Optional (return type = void)
Can be a primitive/composite/abstract value
Using keyword return
CREATED BY DAVE DE BREUCK
METHODS
void Demo::GamePaint(RECT rect)
{
int sum = 0;
sum = CalculateSum(1, 2);
}
int Demo::CalculateSum(int number1, int number2)
{
int result = number1 + number2;
return result;
}
CREATED BY DAVE DE BREUCK
METHODS
void Demo::GamePaint(RECT rect)
{
int sum = 0;
sum = CalculateSum(1, 2);
}
int Demo::CalculateSum(int number1, int number2)
{
int result = number1 + number2;
return result;
}
CREATED BY DAVE DE BREUCK
METHODS
void Demo::GamePaint(RECT rect)
{
int sum = 0;
sum = CalculateSum(20, 40);
}
int Demo::CalculateSum(int number1, int number2)
{
int result = number1 + number2;
return result;
}
CREATED BY DAVE DE BREUCK
METHODS
void Demo::GamePaint(RECT rect)
{
int sum = 0;
sum = CalculateSum(20, 40);
}
int Demo::CalculateSum(int number1, int number2)
{
int result = number1 + number2;
return result;
}
CREATED BY DAVE DE BREUCK
Int number1 = 20
Int number2 = 40
METHODS
void Demo::GamePaint(RECT rect)
{
int sum = 0;
sum = CalculateSum(20, 40);
}
int Demo::CalculateSum(int number1, int number2)
{
int result = number1 + number2;
return result;
}
CREATED BY DAVE DE BREUCK
Int number1 = 20
Int number2 = 40
Result = 60
METHODS
void Demo::GamePaint(RECT rect)
{
int sum = 0;
sum = CalculateSum(20, 40);
}
int Demo::CalculateSum(int number1, int number2)
{
int result = number1 + number2;
return result;
}
CREATED BY DAVE DE BREUCK
Int number1 = 20
Int number2 = 40
Result = 60
METHODS
void Demo::GamePaint(RECT rect)
{
int sum = 0;
sum = 60;
}
int Demo::CalculateSum(int number1, int number2)
{
int result = number1 + number2;
return result;
}
CREATED BY DAVE DE BREUCK
METHODS
Exercise
int Demo::Calculate(int number1, int number2)
{
int result = number1 + number2;
result * = 2;
return result;
}
CREATED BY DAVE DE BREUCK
METHODS
Exercise
Calculate(10, 10); = ?
Calculate(5, 1); =
Calculate(20, 8); =
Calculate(Calculate(6,3), 10); = ?
Calculate(10, 10) + Calculate(80, 5); = ?
Calculate(Calculate(4 , 5), Calculate( 8 , 3)); = ?
CREATED BY DAVE DE BREUCK
METHODS
Exercise
Calculate(10, 10); = 40
Calculate(5, 1); =
Calculate(20, 8); =
Calculate(Calculate(6,3), 10); = ?
Calculate(10, 10) + Calculate(80, 5); = ?
Calculate(Calculate(4 , 5), Calculate( 8 , 3)); = ?
CREATED BY DAVE DE BREUCK
METHODS
Exercise
Calculate(10, 10); = 40
Calculate(5, 1); =
12
Calculate(20, 8); =
Calculate(Calculate(6,3), 10); = ?
Calculate(10, 10) + Calculate(80, 5); = ?
Calculate(Calculate(4 , 5), Calculate( 8 , 3)); = ?
CREATED BY DAVE DE BREUCK
METHODS
Exercise
Calculate(10, 10); = 40
Calculate(5, 1); =
12
Calculate(20, 8); =
56
Calculate(Calculate(6,3), 10); = ?
Calculate(10, 10) + Calculate(80, 5); = ?
Calculate(Calculate(4 , 5), Calculate( 8 , 3)); = ?
CREATED BY DAVE DE BREUCK
METHODS
Exercise
Calculate(10, 10); = 40
Calculate(5, 1); =
12
Calculate(20, 8); =
56
Calculate(Calculate(6,3), 10); = 56
Calculate(10, 10) + Calculate(80, 5); = ?
Calculate(Calculate(4 , 5), Calculate( 8 , 3)); = ?
CREATED BY DAVE DE BREUCK
METHODS
Exercise
Calculate(10, 10); = 40
Calculate(5, 1); =
12
Calculate(20, 8); =
56
Calculate(Calculate(6,3), 10); = 56
Calculate(10, 10) + Calculate(80, 5); = 210
Calculate(Calculate(4 , 5), Calculate( 8 , 3)); = ?
CREATED BY DAVE DE BREUCK
METHODS
Exercise
Calculate(10, 10); = 40
Calculate(5, 1); =
12
Calculate(20, 8); =
56
Calculate(Calculate(6,3), 10); = 56
Calculate(10, 10) + Calculate(80, 5); = 210
Calculate(Calculate(4 , 5), Calculate( 8 , 3)); = 80
CREATED BY DAVE DE BREUCK
METHODS
Stack & Heap
CREATED BY DAVE DE BREUCK
METHODS
Stack & Heap
Stack
CREATED BY DAVE DE BREUCK
METHODS
Stack & Heap
Stack
Very fast
CREATED BY DAVE DE BREUCK
METHODS
Stack & Heap
Stack
Very fast
Low memory size (1MB)
CREATED BY DAVE DE BREUCK
METHODS
Stack & Heap
Stack
Very fast
Low memory size (1MB)
Heap
CREATED BY DAVE DE BREUCK
METHODS
Stack & Heap
Stack
Very fast
Low memory size (1MB)
Heap
Slower
CREATED BY DAVE DE BREUCK
METHODS
Stack & Heap
Stack
Very fast
Low memory size (1MB)
Heap
Slower
Large memory size ( present day 5GB/6GB )
CREATED BY DAVE DE BREUCK
METHODS
Stack
CREATED BY DAVE DE BREUCK
METHODS
Stack
Variables are stacked upon this memory
CREATED BY DAVE DE BREUCK
METHODS
Stack
Variables are stacked upon this memory
Deleted when they run out of scope
CREATED BY DAVE DE BREUCK
METHODS
Stack
Variables are stacked upon this memory
Deleted when they run out of scope
Mostly local variables will be stored here
CREATED BY DAVE DE BREUCK
METHODS
Stack
When it run out of memory you will get a stack overflow
CREATED BY DAVE DE BREUCK
METHODS
Heap
CREATED BY DAVE DE BREUCK
METHODS
Heap
All the memory that is not used by the computer
CREATED BY DAVE DE BREUCK
METHODS
Heap
All the memory that is not used by the computer
Mostly pointers will be created on the heap
CREATED BY DAVE DE BREUCK
METHODS
Exercise Open laptop
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
CLASSES AND OBJECTS
Class is some kind of an object ( an instance of a class )
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
Classes always have
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
Classes always have
A constructor
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
Classes always have
A constructor
A destructor
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
To create a class we use the keyword new
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
To create a class we use the keyword new
When we use new the constructor of our class will be called
Creating an instance of a textbox class
TextBox* m_txtNamePtr = new TextBox()
Pointer to a textbox object
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
Access specifiers
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
Access specifiers
Private: only accessible inside the class
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
Access specifiers
Private: only accessible inside the class
Public: accessible inside and outside the class
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
Access specifiers
Private: only accessible inside the class
Public: accessible inside and outside the class
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
Pointers
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
Pointers
Created with *
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
Pointers
Created with *
Dont forget to also initialize pointers -> m_txtNamePtr(nullptr)
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
Pointers
Created with *
Dont forget to also initialize pointers -> m_txtNamePtr(nullptr)
TextBox* m_txtNamePtr
Pointer to a textbox object
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
Destruction of an object
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
Destruction of an object
Pointers need to be deleted
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
Destruction of an object
Pointers need to be deleted
When we dont need our object anymore we call delete on our object
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
Destruction of an object
Pointers need to be deleted
When we dont need our object anymore we call delete on our object
Objects that are not deleted create a memory leak
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
Destruction of an object
Pointers need to be deleted
When we dont need our object anymore we call delete on our object
Objects that are not deleted create a memory leak
In the destructor of our class we delete our object
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
Destruction of an object
Pointers need to be deleted
When we dont need our object anymore we call delete on our object
Objects that are not deleted create a memory leak
In the destructor of our class we delete our object
Main::~Main()
{
delete m_txtNamePtr;
}
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
Destruction of an object
Always delete your object inside the class you created it
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
Destruction of an object
Always delete your object inside the class you created it to avoid deleting an object
twice
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
Destruction of an object
This is the error that will occur when delete is called on an object that is already
deleted
CREATED BY DAVE DE BREUCK
CLASSES AND OBJECTS
Exercise Open laptop
Add 2 existing items Y.cpp and Y.h and rename them to Ball.cpp and Ball.h
Replace the Y with Ball in Ball.cpp and Ball.h (CTRL + H)
CREATED BY DAVE DE BREUCK