Hello, I'm Joohwan Kim
I'll start Unreal C++ basic lecture 3: Array Pointer Reference Class
In this lecture, we'll learn about arrays and learn to use them
And we'll also learn about pointer reference
And in order to do object-oriented programming on C++ class
We're going to learn about the needed classes
and write the actual code
Understanding Array, Pointer, and Reference
The word here is array
It says it in the name, it means arranging
We can arrange the same data types and use them
For example, to use multiple integers using variables
int a is 3, int B is 4, int C is 5
We need to declare the variable every time like this
In order to make 100 of these, we need to declare it 100 times
Which is impossible, so we use the data structure called array
It's the most basic data structure
It uses the concept of pointer, which we will learn later
Array declaration looks similar to a function
But it's a little different, and at the very front, it indicates the type of array
int, then MyArray which starts with capital M
Bracket, 3, then bracket
In C++, you always need a semicolon at the end
What this means, is that it declares the array of integers
It declares a 3-numbered array
It means creating a variable called MyArray that can hold 3 integers
For array and others, in order to understand declaration, definition, and identifier better
As I explained before, there's MSDN of Microsoft
You need to know the Microsoft Development Network
I'll open the page and explain the content
If you click the Array Declarations at the top
As you can see, the Microsoft build MSDN page appears
In the middle, it says Array Declaration
Take a look at the content, syntax, examples, and references of this document
The syntax part appears at the bottom
This is also known as grammar
So you can see how the syntax looks in detail
It looks more complicated than what I explained
But this is the most basic and complete syntax that explains array
When you scroll to the bottom, it says example
It literally shows examples
It explains examples of how arrays are used
It uses C or C++ codes as examples
and shows how arrays are actually declared
After the explanation, there are references
Called reference in English
Unlike the pointer and reference of C and C++
It's for looking up, so use these references to search
It says declarer and variable declaration
When you click the link, you can see content about declarer and variable declaration
Array Declaration and Overboard Declarations
are the declaration of variable arrays and the outline of declaration
You can see the exact details through MSDN
When declaring arrays, we use variables
Arrays combine 100 variables into one
So you need to learn about variables
Lifetime is the life time of the variable, Scope is the range of the variable
Visibility is whether we can use the variable or not
It says it in the name
Then there's Linkage and Summary of Lifetime Visibility
These things can be seen in MSDN
And you can learn about how variables work
Back to arrays
We can set the method of resetting the array variable
For example, if there's integer variable A
It will be reset to int a is 3
But for arrays, we need to put multiple values at once
So there's a separate way to reset
At the very top, it says int a[5] =
{1, 2, 3, 4, 5}
The left part is the array variable
The part that says int a[5]
The equal sign is the substitution operator
And the right part that says {1, 2, 3, 4, 5}
show the values that will go into the array
We created a 5-numbered array
So we can put 1, 2, 3, 4, 5 inside, like that
Or we can just put 1 and 2, like the second line
and exclude other parts
Then the excluded parts will be set as the basic value of that type
For int, it will be 0
Just like the third line, if in a[5] is set to 0
Then all of them will be reset to 0
Like the last line, if we insert array without setting the length
It becomes a 3-numbered array
So these are the methods of resetting the array
Like the first one, if there's no length within the brackets after int a
Just like the previous page
Rather than adding 1, 2, 3 as the data
If we just put int a = and put nothing inside the brackets
We don't know the size, so it won't compile
Or like the second line, if we set the int index variable to 5
and use that variable to declare the size of the array
it still won't compile
It means that it's not translating
When we compile, we need to surely know the size of the array
So putting a changeable value
We can put different values into Int index 5
So if we put changeable values, it will not compile
We call this not being the compile type constant expression
You'll know once you learn more about C++
Since we reset the array value
We need to use the value within the array
Then we need a way to approach the array
For arrays, the thing inside the brackets is called index
Index means that it's pointing to something
So it means that we're going to bring the nth element of the array
So if you add a certain index in the brackets of MyArray
The index starts from 0
All programs start from 0, so index also starts from 0
When you put 0
The first int value will come out
If you put 1, the second int value will be brought
In the previous syntax, we put 1, 2, 3, 4, 5 into int a[5]
In that case, the index of the value 1, is 0
Because it starts from 0
So even though we put 0, 1, 2, 3, 4
It actually brings the first, second, third, fourth, and fifth values
To express this with picture, it's as follows
When we put MyArray 0, even though we put 0
It brings the first element of that element, 3
And if we do MyArray 1, it will bring the second element, 79
To think about where we'll use the array
I explained earlier about why arrays were made
If there are 3 or 4 of the same variables
We would declare them 3 or 4 times
But if we need 1 million variables containing an integer
Then we can't write 1 million of them in the code
So we have to use the method of arrays
So when repeating the same content
or sorting
Sorting certain data from the lowest to highest values
Or from highest to lowest
We can't create algorithm implementations for sorting
if we don't use arrays
Since we can't handle the same data types in a linear form
we can't implement sorting
Also, without arrays, everything that handles data dynamically, is impossible
Such as inventory implementation
We can't create data sets that change in real-time
After arrays, I'll now explain the pointer
For pointer, when learning C and C++ language
It's a difficult concept that they call the biggest obstacle
I'll try to explain it as easy as possible
Pointer, just like the name says, it points to something
And the way to express what it's pointing to, is the address
Whether it's variable, object, or function
It can point to a target
For the method of pointing
it brings the address of where the object exists within the memory
Inside the computer, there's the memory
And the values go inside the memory
Where that value is located, is the address
The address is usually in x86 environment
What x86 is, is that
32 bit computer lines are usually called x86
In the past, when computer environments were in 286, 386, and 486
The address value of the pointer, or the address size of the memory was 4 bytes
Since 1 byte is 8 bit and 4 bytes is 32 bit
Then the size of the memory that can be pointed with point variable 4 byte
is about 4 GB
So in x86 environment computers
Most of the point variables are 4 bytes
I say "most" because
Almost all point variables are 4 bytes
because the memory address is the size of 4 bytes
but there may be exceptions
And the pointing target can be variables, objects, or functions
For details about pointer declaration, take a look at MSDN
Let's click on it and take a look together
Click Pointer Declarations
Now it says it in English
In the Learn C++, C and Assembler option
It says Pointer Declarations
Here, there are syntax, example, and see also
In the beginning, there's an abstract description
It says that a pointer declaration names a pointer variable
Just like how I briefly explained earlier
It has the same explanations
It says it in English, but if you want to read it in Korean
you can click the Korean icon at the top right hand corner and change it to Korean
Syntax means the grammar
And after the pointer variable, asterisk is used
to set the pointer variable, is what it says
Same thing for examples
It shows examples of using pointer variables
It explains in detail
C and C++ are almost the same
In C language, the char pointer form and int pointer form
are exampled in the syntax all the way to the bottom
In See also, it says Declarators and Variable Declarations
You also need to know about declarations and the declaration of variables
Pointer, when learning the C and C++ language
is a very difficult concept for beginners
There's a reason why they made this pointer so difficult
To put pointer in simple terms
it's just a variable that points to an address
But it becomes difficult for beginners because
there are two main operators for using pointers
The star operator, which is the indirect reference operator
That star is called asterisk
There's the asterisk operator, and there's the address operator
&, and it's called ampersand
Those two operators
are used as different meanings, so that's why it's difficult
It may cause confusions
That's how C grammar is like
To tell you the basic form
the asterisk, which is the indirect reference operator
When there's a point variable
and you put that operator in the front
Then it brings that value from that address
The address operator, ampersand, on the other hand
When you put ampersand in front of a variable
Then it brings the address of the variable
It's simple if you look at it this way
But it's actually not
Because when you look at the text
For the first one, it says int a is 0
It declares a very simple variable
and defines it as 0
It declares an integer variable, a
and substitutes it for an integer called 0, using the equal sign
The second syntax is the typical usage of point variable
It's called int* pA
When that asterisk is used as punctuation point
it called asterisk
But when we read it, we say point
int pointer type
So the first line is int type a
And the second line is the int pointer type pA
What that part means, is that
Where integer is included
Variable a, where integer is included
That's where this variable can point to
It means that it's a variable that can hold the address that can point to a variable
In int point pA, where address is held
In the variable pA, when we add ampersand
We said earlier that the ampersand, the address operator, brings the memory address
When we do &a for the variable of pA
Then it brings the address of variable a of the top line, int a = 0
So in pA, a won't be substituted for 0
For the variable a
pA will bring in the value of the location within the memory
When it reads hexadecimal
It will be something like 0xffff332
The very long value that expresses the memory address
will be inserted into pA
Then for variable pA
the address of the variable a will come out
Then we can bring in the value through that address
The third line is an example of that
int b brings in the value of pA
That syntax isn't called pointer
It's now called the asterisk operator
It's the dereference of pA
Go to the address of pA
and bring in the value and insert it to b
As a result, in the variable of int b
The value of int a, 0, will be inserted
For putting in values
you may wonder why we use such a complicated process
But in order to do object-orientation later on
pointer is a very basic grammar for pointing to something
So even though it's difficult
This is as much as you should know
And further on, when you do Unreal C++ Programming
it will become an important element, so be sure to learn them
After reasons why pointers are so difficult
I also put C++ reference at the end
Later, another grammar is added in C++
So just like the int pointer pA of the second line
int reference a will be added
Unlike the ampersand operator of the second line
This is a different operator from the operator that brings the address
To be exact, it's not an operator but a punctuation point
Just like the int pointer type
There's something called int reference type
So if you take a look, there's the indirect reference operator asterisk
The indirect reference operator in the third line
which brings the value from the memory
And the asterisk punctuation point that corresponds to int pointer type
Uses the same star symbol
But the stars in the second line and the third line
have two completely different meanings
And the &a of the second line
and the & a in the fourth line, int reference, are also completely different
This is what makes pointers so difficult in C and C++
It's a huge obstacle for beginners
If you think it's too complicated
The indirect reference operator asterisk that I explained first
and the address operator &, are what you should take a look at first
At the end, once you just learn how to use the second one
you will be able to explain the basic pointer
There's the reference grammar that I mentioned as the fourth one
This reference works similar to pointers
It's a newly added grammar
There's a simple syntax called int a = 3
Inside the a, there's 3
If we say that int reference rA is a
Then when the variable of a exists
The variable of a and rA are connected by reference
Before, when we used pointer
the int pointer type, ampersand address reference operator
int b, and asterisk dereference operator were used
But without the need of all these parts
When we only just reference in C++
rA and a are treated the same way
When the value of rA is changed, a value also changes
Then a value is changed, rA value will be changed
It's basically a form of nickname
Many grammars were skipped
so when reference is used
we can use references more easier than pointers
In pointer, there's also multiple grammars
I'll first explain about the double pointer
I said that pointer points to the address of a variable
That's the first syntax that you can see
To explain again
It creates variable pA of the int pointer type
It can hold the address
So at the top, there will be variable a
It brings the address of variable a
and will hold it in pA
The second one, int double point ppA
The pointer variable pA
so the variable that can hold the address
It will bring the address of that variable
As I explained earlier, the pointer of a pointer
and the pointer of that pointer
That's how we can implement multiple pointers
More than 3 pointers are usually not used
That's because address reference
rarely requires multiple steps
Also, it won't be intuitive for people
So if we must use them
Pointer or double pointer types will be replaced
As you will learn earlier
Nicknames will be made for the types
And those will be used as pointers
When explaining pointers
I can't forget this part
The structure, although we haven't learn about it yet
When we learn class
Class and structure are basically the same thing
There are times when we need to use the pointer of class and structure
So class and structure
When they exist as variables in the pointer forms
Then we use the arrow operator
And rather than structure pointer
if it's just the structure object
then we use a period
I'll show you the examples later
Understanding Class
Then earlier, we mentioned class and structure
Those are objects
I'll now explain about object-oriented programming
It's also called OOP
Object Oriented Programming
It's a concept used in C++ and C#
In C++, C#, or Java
before the emergence of OOP
the data and the way of data operation, were divided
To put it in simple terms
In the variable int a
3 goes in as the data
And take that 3 and the 2 from b
And create a function that adds the two variables
Like this, the part where the data was inserted
and the part where the data operated, were divided separately
But with the emergence of object-oriented programming
They created something called object
The object of the object-orientation is
something that combined the data
and the method of how the data operates
For the method of combining, class came to play
Class is the structure of C
It's a form developed from combining multiple variables
The initial name was called C with Classes
It becomes C++ later on
In 1978, Bjarne Stroustrup created
C++, and that becomes the language
As it was called C with Classes at first
Class didn't exist in C at first
In the structure form
And it appeared in C++
That class is the core element
of object-orientation that made C++
As I explained earlier, class, unlike structure
links data and function
And I can make it myself
For example, unlike int that hold integers, float that holds real numbers
and string that holds letters
The player holds people
Monster holds the enemy
Or tools, inventory
Like these, the data and the various operations of the data are linked
into one class
All these are linked at once
and can be considered as one through class
And this is how it looks
If you take a look, there's a 3-line annotation
When we use two or three slashes, it called annotation
Unlike grammar, it doesn't get compiled
It's the part that holds explanations
It says class declaration
and it says class CPlayer
class CPlayer
operates like int
And the part that corresponds to int is CPlayer
We put the keyword 'class' at the front
And after that, put CPlayer
or the type that I want to make
Again, this doesn't correspond to the a of int a = 3
It corresponds to int
It's like a blueprint that makes the type
Put class CPlayer, then open braces
Inside that, put the variables and functions that I want to insert into the class
Then we can link this as one and create an object
I'll now explain the code
It says Private int m_HP
The variable m_HP
is in the integer form
It's a variable called HP that can hold values in integer forms
Usually, prefix m_ is used
This is the prefix that indicates
that it's a variable inside a class
It means member
And since it says HP
It means Hit Point or Health Point
So it's a variable created for indicating the health of the player
After that, it says Private then a colon
Private colon means access modifier
Whether it can be used inside the class
In other words, whether it can be used only within the CPlayer class
Or whether we can make CPlayer outside
and approach the CPlayer from the outside, is what it means
For Private int m_HP
It's the health within the player class
I'll be the only one using my health
It means that only I'll know about it and use it
Under that, for Protected int m_Weapon
It's the index of the weapon
The integer value, at the protected state
We'll learn about the meaning through inheritance later
But when we inherit the class, I'll only use it in mine and my daughter's class
Under that, Public is
being able to use from the outside
It accessible from anywhere
It means Public, as the name says
Under Public, it says Void Move
(float dTime)
As you've seen a lot, this is a function
The return type is in void
What void is
It means that there's no return type
It's a function that doesn't return
It receives float, a real number form, called variable dTime
and moves, is what this function means
We call it member function
To explain the code briefly
dTime in float form is delta time
So this character, within a certain time frame
Delta usually means displacement
In terms of time, it means variable
Move per time unit
is the meaning that it's trying to have
It's the declaration of the function
For the SetPlayer function on the bottom, it even includes the text
Within the class
on top of function declaration
we can even write the text
SetPlayer m_HP is 0
The function Set Player
is the function that makes HealthPoint HP to 0
Class looks like this overall
The class form that I just explained
corresponds to the declaration of class
I explained about declaration and definition earlier
Declaration means I'm going to use this
It literally means declaring
That I'm going to use it
But for definition, the content
is what it defines
The value of M_HP
the value of weapon, and the text of move function
are not written in this declaration
Of course, the SetPlayer function has the text as well
But the move function doesn't have text
That's why this part is class declaration
Just like arrays, if we think about why we use this
Class means the data
and the way of how the data operates
combined together
From here, OOP emerged
So when first learning about class
When learning about class using C++
Any objects can be described with class
For example, person, wheels of a bus
handle, frame, window
Linking these together to make a bus
For all objects, depicting them with class
will be tried when first learning about class
I explained earlier in the class structure
But to go back to it
In the middle, the void Move float dTime
and void SetPlayer
are called member functions
You may wonder why we call it member function
It's because it's a function that belongs to a class
Same thing for M_HP
and M_weapon
They're also called member variables
To explain member function first
Before, the function that had no association with data
are now linked in the name of class CPlayer
So this member function only operates
on the player, is what it means
In other words, the data and operation
The data and operation have cohesiveness
It means they're related
That's one
And a function that we get by using member function
is called data concealment
We can hide the data
As for the class that we talked about earlier
The int form called m_hp
The integer variable that represents health
is declared privately
so it's unapproachable from the outside the class
But if the function called SetPlayer
is called Public, then we can reset it
That's what it means
This is called data concealment
Also known as data hiding
capsule, or incapsulation
And in the class, in the member function
Operator overloading
such as adding or subtracting
The existing operators can be re-defined
We can re-define them as member functions
For example, it may not make sense in reality but
Adding two players together
Or multiplying players, are possible
It's like defining the operators in math
Now this is the capsulation or concealment that I talked about earlier
It's called incapsulation or data hiding
As mentioned earlier, HP or health is set as private
Hide it, then only use it for public function
That's incapsulation or data hiding
They're two similar words
They basically mean the same things
Capsulation, like surrounding it with a capsule so that it can't be seen
Hiding, literally means hiding the data
As mentioned earlier, private, protected, or public
are the access specifiers that we can use for capsulation
Whether we can approach from the outside
or only from the inside, can be distinguished
And through these, we can link the unit of implementation
And this is called wrapping
Literally means wrapping around
In class, there are basic functions
Among them, I'll explain the constructor
It's called constructor in English
For the CPlayer class that we saw earlier
Another function with the same name as the class, is made
Or it can be made
It uses the same name of CPlayer
and if it's in the form of function, then a special function called constructor is created
The top part is the syntax that declares the constructor and the destructor
So there's no text
On the bottom, the constructor of CPlayer
and the destructor of CPlayer are declared
So there are braces and content
Of course, there's nothing inside the braces, so it won't do anything
But there's still content
So constructor of class
When creating a specific class
When creating the CPlayer class, constructor is the function that's automatically called
It's called over automatically
As you've seen the member functions earlier
like m_HP or m_Weapon
It's used for resetting the members inside
The special part is that there's no return value
It means that it doesn't return any value
Its role is to reset while constructing
so it doesn't need to make any values
I've said that overloading is possible
For overloading, it means that
functions with the same name and different roles can be made
Default provided means that
the compiler that compiles the program
basically creates the constructor, and that can be used
There's a similar concept called destructor
CPlayer { } refers to the constructor
and under that, the thing with the ~ sign in the front is the destructor
It's called destructor in English
Before, we also called it the destroyer but now we say destructor more
For the destructor
when a specific object is destructing
it gets called over
The destructor needs to be called because
In the constructor of the class, inside
an object is sometimes made dynamically
The destructor gets rid of them
In other words
The m_HP earlier in int form
The value representing health was set in constructor
but as this object disappears
the value can be reset to English
or be destroyed, and that's what it does
So for destructor, unlike constructor
It has no parameters
There's no parameter
And same for this, it provides the default
For the call time of class
I need to talk about the call time of constructor and destructor
This is also related to the lifetime of the variable
Creating an object by making a class
is similar to creating variable a using int
So the int a variable is created
and destroyed
Just like that, class is created and destroyed
When created, copying a certain object
is called copy constructor
It's also influenced by inheritance structure
I'll end the lecture here
Thank you
Understanding Array, Pointer, and Reference Array Data structure that makes the same data type in linear way
For repeating the same content n times Implements algorithms such as sorting For dynamically handling data
Pointer Expresses the address of an object Can point to variables, objects, functions, EIP
Reference Nickname of object Reference can be made easier compared to using pointer
Understanding Class Class Object that combines data and function All objects can be expressed in class
Member Function Data and operation have coherence Hiding the data is possible Operator overloading function is added
Constructor Called over when class is constructed For resetting purposes, no return Overloading is possible, default provided
Understanding Class Destructor Called over when class is destroyed Detaches objects No parameters, default is provided
The End