Object Oriented Programming For Noob using C++

Currently all programmers nowadays need to understand the basic concept of Object Oriented Programming (OOP)in a better ways than using old structured programming nowadays.

This understanding is needed because it's more easier to programming something in a real world point of view than structured programming that can make confuse in middle of development....

This example can help you to understand.....

Let say we want to use a line in programming, what a line can do.... and what a line have....??? if you use structured programming the code will be :

void main()
{
int length, int pos_x, int pos_y;

}

but in OOP you can make a class hat can be used several times in case you need to using the line many times, the class code will be :

class CLine
{
private :
int length, int pos_x, int pos_y;
};

Like i say before, you can using it many times...... in void main you can use this code to create 2 lines :

void main()
{
CLine line1, line2;
}

rather than using old structured programming like this :

void main()
{
int length1, int pos_x_line1, int pos_y_line1;
int length1, int pos_x_line2, int pos_y_line2;
}

and it is expandable.... you can modified the class and make it more functionaly and easier to understand, when I Need to setting each line, what I have to do in structured programming is like this :

void main()
{
int length1, int pos_x_line1, int pos_y_line1;
int length2, int pos_x_line2, int pos_y_line2;
/*
Below is changing and setting the value of each line
*/
length1 =3;
pos_x_line1=10;
pos_y_line1=20;

length2 =6;
pos_x_line2=20;
pos_y_line2=30;
}

Imagine if you have 100 line??? you need to type it over and over.... in OOP you can expand the class to accomodate this need by adding a public function like this :


class CLine
{
private :
int length, int pos_x, int pos_y;

public :
void init_line(int the_length, int the_x, int the_y)
{
length = the_length;
pos_x = the_x;
pos_y = the_y;
}
};

and using initial function, this void main will have same result as in the structured programming that shown before:

void main()
{
CLine Line1, Line2;

/*
Below is changing and setting the value of each line
*/

Line1.init_line(3,10,20);
Line2.init_line(6,20,30);
}

Some protest and opinion from my student about OOP is they need some preparation and it's hard to imagine what'll be needed and must think what to be prepared next, but it's common opinion, if you in a middle big project and need to change your software specification to adjust the customer demand, the fatal flaw of structured programming is : YOU HAVE TO REWRITE YOUR CODE.....
and in OOP you just need to tweak and change several lines of code....

For example, when I need the line(s) before in 3D.... what I need to do is just add several code like this :

class CLine
{
private :
int length, int pos_x, int pos_y;
int pos_y;
public :
void init_line(int the_length, int the_x, int the_y)
{
length = the_length;
pos_x = the_x;
pos_y = the_y;
pos_z=0;
}
void init_line_3D(int the_length, int the_x, int the_y, int the_z)
{
length = the_length;
pos_x = the_x;
pos_y = the_y;
pos_z = the_z;
}
};

And just use new modified class in void main


void main()
{
CLine Line1, Line2;

/*
Below is changing and setting the value of each line
*/

// Line1.init_line(3,10,20); THIS CODE IS REMOVED
// Line2.init_line(6,20,30); BECAUSE WE WANT THE LINE LIVE IN 3D
Line1.init_line_3D(3,10,20,30);
Line2.init_line_3D(6,20,30,10);
}

In structured programming what you have to do is modified whole code like this :

void main()
{
int length1, int pos_x_line1, int pos_y_line1;
int length2, int pos_x_line2, int pos_y_line2;
/*
Below is changing and setting the value of each line
*/
length1 =3;
pos_x_line1=10;
pos_y_line1=20;

length2 =6;
pos_x_line2=20;
pos_y_line2=30;

//below is new variable and initial command
int pos_z_line1=30;
int pos_z_line2=10;
}

Just imagine when you have 100 lines.... you need to change and add 100 initial act.... during 100 lines of copy and paste, there are chance when you didn't doing the right things.... and the example just demonstrated how the structured programming can make you frustated in initial phase... believe me the tweaking function, and other headchache problems can make you regret....

The needs to understanding OOP concept and paradigm is important, and since the lates programming language like Java, VB, and others is using this concept as the way to work... you need to understand.... I'll help you in next blogs to understand the OOP.

Regards

0 comments: