Showing posts with label Object Oriented Programming. Show all posts
Showing posts with label Object Oriented Programming. Show all posts

Sunday, August 13, 2017

What is OOP(Object Oriented Programmming)?? -- the real meaning in C++ - 2/2

In previous post, We learn meanings of Class, Methods in C++,which is the set of data and how to process that.

And see the simple video game's Player class in C++ like

class Player
{
private:
    int pos_x;
    int pos_y;    

public:
    void apply_input(float direction);
    void move_to(int x, int y);
};

Ok next, we will make class Enemy.

class Enemy
{
private:
    int hate;
    int pos_x;
    int pos_y;    

public:
    void apply_hate(int hate);
    void move_to(int x, int y);
};

Class Enemy does not require 'apply_input()' method because it will not move by joystick input.
And Enemy has hate data(declared as int to make it simple), which will used for enemy's behaviour.

mmm... It seems to be OK, but the Player and Enemy has same data and methods.
It's waste of codes.

Let's think about 'pos_x' and 'pos_y'.
These are attributes of all object in the game.
So we declare Object class which have position data and related method.

class Object
{
private:
    int pos_x;
    int pos_y;
public:
    void move_to(int x, int y);
};

and the class Player and Enemy 'Inherits' the class Object.

class Player : public Object
{
public:
    void apply_input(float direction);
};

class Enemy : public Object
{
private:
    int hate;
public:
    void apply_hate(int hate);
};

Inheritance in C++ means addition of data and methods.
In this example, Object has position of object.
And in addition, Player has method 'apply_input', Enemy has data 'hate' and a method 'apply_hate()'.

Inheritance is usable when some code is common in some classes.



What is OOP(Object Oriented Programmming)?? -- the real meaning in C++ - 1/2

Object Oriented Programming(OOP) -- when you start to learning programming, this may the first paradigm you will meet. And most explanations of OOP I saw in books or websites, does not explain the real meaning of OOP.

You may see this sort of explanation.
Animals ----- Birds
         +- Fishes
         +- Mammals
'Animals','Fishes','Birds','Mammals' are Class. 'Animals' is a parent Class of other ones. and Birds has a method of 'fly', Fishes has a method of 'swim', and so on.

With this kind of explanation, You can understand the meanings of words 'Class' or 'Method' in abstract concept. But you can't learn the real meanings of C++.

In C++, the 'Class' means the set of data and how to process that.
'How to process that?' is the Class Method.

Let's see some example.
In video game, a player is a typical object.
the Player class is declared like this.

class Player
{
private:
    int pos_x;
    int pos_y;    

public:
    void apply_input(float direction);
    void move_to(int x, int y);

};

You see the data 'pos_x','pos_y' and methods 'apply_input()','move_to()'.
As you can easily understand, 'pos_x' and 'pos_y' means position of the player.
The method 'apply_input()' applies input data of joystick, 'move_to()' moves the player to specified position.

Simple, isn't it !?

For comparision, let's see the C language implementation of this Player.

struct Player
{
    int pos_x;
    int pos_y;
};

void apply_input(struct Player*, float direction);
void move_to(struct Player*, int x, int y);

In C, struct can not have methods. so we must declare functions outside of the struct, receiving pointer to the struct.

so, class in C++ is the set of data and how to process it.

Would you like to learn more?
Visit the next post.

It's magic! std::bind

'std::bind' is a powerful helper for working around 'std::function'. 'std::bind' makes a instance of 'std::fun...