Pages

Showing posts with label C_study_en. Show all posts
Showing posts with label C_study_en. Show all posts

Monday, November 20, 2017

Easy tutorial for C / C ++ - class 1 (Object oriented programming and class)

Easy tutorial for C / C ++ - class 1 (Object oriented programming and class)

Object Oriented Programming (OOP) :

- Old programming is a function-oriented sequential programming method by calling functions in the main function. This is called procedural programming.

- Object-oriented programming is a programming in a way that the program is made up of objects rather than a certain order. Objects can have multiple member variables similar to struct. You can also define member functions (methods) to perform the functions required by the object.

- Features of OOP include data abstraction, encapsulation, data hiding, polymorphism, and inheritance.

Data abstraction : Simplifying the program by hiding unnecessary information and showing only important information.

Encapsulation : A class uses multiple member variables and member functions (methods) in a single unit. That is, you can encapsulate them into one aggregate.

Data Hiding : Indicates that member variables are indirectly accessed through member functions. (are not directly accessible)

Polymorphism : A single element (function or operators) that allows for multiple concepts and behavior (number of data types and arguments). (ex: operator overloading)

Inheritance: Features and functions of a specific object (functions of an already defined class) can be inherited by other objects. This eliminates the need to make redundant content unnecessary.

class :

- It is a class that allows you to abstract data to create new user-defined types. It is similar to a struct in that it has several member variables, but a class can implement member functions (methods) to perform more functions.

- The basic structure of a class is as follows: Class declaration and member function definition.

class 'class name' {
         "Access specifier":
             'Data type' 'member variable';
             ...
         "Access specifier":
             'Data type' 'member function';
};
'Data type' 'class name' :: 'member function ()
{...}

- First, specify 'class' and write the corresponding 'class name'. And declare member variables and member functions to be used. Here, 'access specifier' (private, public, and protected) is a specifier related to access rights and inheritance.

- When defining a member function, 'class name' is created after 'data type' to be returned, and the scope operator (: :) is used to indicate that the function belongs to the class. Then write the name of the member function and write the contents of the function.

Friday, November 17, 2017

Easy tutorial for C / C ++ - struct 5 (Arrays of struct)

Easy tutorial for C / C ++ - struct 5 (Arrays of struct)

- Let's see how to apply a struct to an array. Three struct variables V_1, V_2, and V_3 were declared and initialized as follows.  

     t_Struct V_1 = {10, "orange", "red"};
     t_Struct V_2 = {20, "apple", "blue" };
     t_Struct V_3 = {30 , "banana", "white"};

- An array with the same contents as above can be displayed as follows.

     t_Struct V_a[3] = { {10, "orange", "red"},
                                     {20, "apple", "blue"},
                                     {30, "banana", "white"} };

- To access each element of a struct array, use subscripts in square brackets '[]' and use '.' operator. For example, to access the fruit name member variable of the second element, you would write: (V_a [1]. fruit)

- We confirmed earlier that the array name is the start address value of the array. The name of the struct array also represents the starting address value, and the next element can be accessed through the '+' operator. You can also use arrays of structs as arguments to functions.

- Let's look at an example.

Example Code

#include <iostream>
#include <iomanip>
using namespace std;

struct t_Struct{
   int num;
   char fruit[30];
   char color[30];
};

void C_A(t_Struct *,int);
int main() {

     t_Struct V_a[3] = { {10, "orange", "red"},
                         {20, "apple", "blue"},
                         {30, "banana", "white"} };

     t_Struct *pt;
     pt = V_a;

     C_A(pt,3);

     return 0;
}

void C_A(t_Struct *AA, int n)
{
     cout << setw(10) << "number";
     cout << setw(10) << "fruit";
     cout << setw(10) << "color";
     cout << endl;
    
     for(int i = 0; i < n; i++){
     cout << setw(10) << (AA+i)->num;
     cout << setw(10) << (AA+i)->fruit;
     cout << setw(10) << (AA+i)->color;
     cout << endl ;
          }
}

- We have declaraed the struct array 'V_a', the struct pointer variable 'pt' and function 'C_A'. Here, 'C_A' is a function that takes the number of elements of struct pointer variable and struct array as arguments and prints the value of each member variable. And we have accessed the member variable by using the pointer variable and the '->' operator.

- In the main function, we have declared a structure array 'V_a' and gave it initial values. We have declared the structure pointer variable 'pt' and assign the structure array name to 'pt'.

- The function 'C_A' was called with 'pt' and the number of elements in the array of structures as arguments.

results :

    number        fruit     color
            10    orange        red
            20      apple       blue
            30    banana     white

- You can see that the initial values of the struct array is printed.

Thursday, November 9, 2017

Easy tutorial for C / C ++ - struct 4 (function call methods)

Easy tutorial for C / C ++ - struct 4 (function call methods)

- We have reviewed the function part by three call methods: call by value, call by reference, and call by address. All three of these methods can be applied to struct. Let's look at an example.

Example Code

#include <iostream>
using namespace std;

struct t_Struct{
   int num;
   char fruit[30];
   char color[30];
};

void C_V(t_Struct);
void C_A(t_Struct *);
void C_R(t_Struct &);

int main() {
     t_Struct V1 = {10, "banana" , "red"};
     t_Struct V2 = {20, "apple"  , "black"};
     t_Struct V3 = {30, "orange" , "white"};

     C_V(V1);

     t_Struct *pt;
     pt = &V2;
     C_A(pt);

     C_R(V3);
     return 0;
}

void C_V(t_Struct A)
{
          cout << " ### Call by value ### " << endl ;
          cout << " number = " << A.num << endl ;
          cout << " fruit  = " << A.fruit << endl ;
          cout << " color  = " << A.color << endl ;
          cout << endl;
}

void C_A(t_Struct *A)
{
          cout << " ### Call by address ### " << endl ;
          cout << " number = " << A->num << endl ;
          cout << " fruit  = " << A->fruit << endl ;
          cout << " color  = " << A->color << endl ;
          cout << endl;
}

void C_R(t_Struct &A)
{
          cout << " ### Call by reference ### " << endl ;
          cout << " number = " << A.num << endl ;
          cout << " fruit  = " << A.fruit << endl ;
          cout << " color  = " << A.color << endl ;
          cout << endl;
}

- We have defined a struct 't_Struct' which has integer variable 'num' and string variables 'fruit' and 'color' as member variables.

(1) We have declared the function 'C_V' by call by value method. We got the struct 't_Struct' as a parameter and output the contents of the member variables of the struct variable that was received as an argument.

(2) We have declared 'C_A' function by call by address method. The struct pointer variable was taken as argument and the contents of the member variable of the struct variable are output.

(3) We have declared 'C_R' function by call by reference method. We took the reference variable as an argument and output the contents of the member variables of the struct variable.

- Struct variables V1, V2, V3 were declared and initialized respectively. We first call function 'C_V' with V1 as its argument.

- The structure pointer variable 'pt' were declared and substituted the starting address value of the struct variable 'V2'. And we called function 'C_A' and gave 'pt' as an argument. Finally, we call the function 'C_R' with the reference variable as an argument.

results :

 ### Call by value ###
 number = 10
 fruit  = banana
 color  = red

 ### Call by address ###
 number = 20
 fruit  = apple
 color  = black

 ### Call by reference ###
 number = 30
 fruit  = orange
 color  = white

- You can see the results by three call methods.

Tuesday, November 7, 2017

Easy tutorial for C / C ++ - struct 3 (pointer to struct)

Easy tutorial for C / C ++ - struct 3 (pointer to struct)

Pointer to struct :

- Struct can use pointers just like any other data type. The pointer is used points to the start address of the declared struct variable. The usage is as follows.

ex) t_Struct *pt ;

- 't_Struct' is the newly defined struct (data type) and 'pt' is the pointer variable.

- Let's look at how to access data through pointers to struct. Because the pointer points to the address, we put the '*' operator before the pointer for indirect reference. For struct, one thing to note is that the member variable access operator '.' (Priority problem with operators.)

- Because the precedence of the operator '.' is higher than the operator '*', you should use the '()' operator around the pointer as follows.

*pt.color    (x)
(*pt).color   (o)

- Another way is to use the '->' operator. For pointers to struct, you can use the '->' operator as follows to access member variables.

pt-> color

- Let's look at an example.

Example Code

#include <iostream>
using namespace std;

struct t_Struct{
   int num;
   char fruit[30];
   char color[30];
};

int main() {
     t_Struct strA = {7, "banana", "blue"};

     cout << " 1st results " << endl ;
     cout << " number = " << strA.num << endl ;
     cout << " fruit  = " << strA.fruit << endl ;
     cout << " color  = " << strA.color << endl ;
     cout << endl;

     t_Struct *pt;
     pt = &strA;
     cout << " 2nd results " << endl ;
     cout << " number = " << (*pt).num << endl ;
     cout << " fruit  = " << (*pt).fruit << endl ;
     cout << " color  = " << (*pt).color << endl ;
     cout << endl;

     cout << " 3rd results " << endl ;
     cout << " number = " << pt->num << endl ;
     cout << " fruit  = " << pt->fruit << endl ;
     cout << " color  = " << pt->color << endl ;
     cout << endl;
     return 0;
}

- We have declared a struct named 't_Struct'. We have defined the integer variable 'num', the string variables 'fruit' and 'color' as a member variable.

- In the main function, we have declared the struct variable 'strA' and initialized it with {7, "banana", "blue"}. And we accessed the member variable through the '.' operator and printed it.

- We have declare the pointer variable 'pt' and assigned the address value of the struct variable 'strA' to 'pt'.

- Struct pointer variable was used to output data of member variables. First, using the '.' operator "(* pt).", we will print the result. Second, we use '->' operator "pt->" for output.

results :

 1st results
 number = 7
 fruit  = banana
 color  = blue

 2nd results
 number = 7
 fruit  = banana
 color  = blue

 3rd results
 number = 7
 fruit  = banana
 color  = blue

- You can see that all the three give the same result.

Sunday, November 5, 2017

Easy tutorial for C / C ++ - struct 2 (Initial values of struct variables, function arguments, return value)

Easy tutorial for C / C ++ - struct 2 (Initial values of struct variables, function arguments, return value)

- Let's look at how to give initial values to struct variables. The struct defined in the previous example is:

struct t_Struct{
   int num;
   char fruit[30];
   char color[30];
};

- Member variables are integer variable 'num' and string variables 'fruit' and 'color'. Initialization can be done by member variable as follows. Separate it with a comma (,) and enclose it in curly braces ({}).

ex) :
t_Struct strA = {5, "banana","white"};

- A struct can be used as a function's return value and return value. Let's look at an example.

Example Code

#include <iostream>
using namespace std;

struct t_Struct{
   int num;
   char fruit[30];
   char color[30];
};

void prn_1(t_Struct);

t_Struct prn_2();

int main() {

     t_Struct strA = {5, "banana","white"};

     cout << "*** \'prn_1\' results (1st) ***" << endl;
     prn_1(strA);

     strA = prn_2();

     cout << "*** \'prn_1\' results (2nd) ***" << endl;
     prn_1(strA);

     return 0;
}

void prn_1(t_Struct AA)
{
          cout << " number = " << AA.num << endl ;
          cout << " fruit  = " << AA.fruit << endl ;
          cout << " color  = " << AA.color << endl ;
          cout << endl;
}

t_Struct prn_2()
{
     t_Struct BB = {7, "apple", "green"} ;
     return BB;
}

- We defined a structure 't_Struct' which has integer variable 'num' and string variables 'fruit' and 'color' as member variables.

(1) We declare an output function that takes the struct 't_Struct' as an argument and has no return value. (void prn_1 (t_Struct);) This function outputs the contents of the member variables of the struct variable received as an argument.

(2) We declared a function with no argument and a return value of struct 't_Struct'. (t_Struct prn_2 ();) This function declares and initializes a struct variable ('BB') and returns {7, "apple", "green"} structure variables.

- Declared a struct variable 'strA' in the main function and initialized it with {5, "banana", "white"}. We then called the function 'prn_1' and passed 'strA' as an argument.

- Called 'prn_2 ()' and assigned to 'strA'. And once again we call the function 'prn_1' and pass 'strA' as an argument.

results :

*** 'prn_1' results (1st) ***
 number = 5
 fruit  = banana
 color  = white

*** 'prn_1' results (2nd) ***
 number = 7
 fruit  = apple
 color  = green

- As a result of the first output, we can see that the initial value 5, banana, white of the struct variable strA is output.

- The function 'prn_2 ()' is called and the returned value is assigned to 'strA'. As a result, we can see that the initial value 7, apple, green of the struct variables defined in the 'prn_2 ()' function is output.

Saturday, November 4, 2017

Easy tutorial for C / C ++ - struct 1

Easy tutorial for C / C ++ - struct 1

struct :

- A datatype can be provided by the compiler or it can be defined by a user. A user-defined type can be defined as 'struct' to help you define it. A new data type defined using 'struct' is called a struct.

- To declare a struct:

struct 'struct name' {
          'Data type' 'member variable 1';
          'Data type' 'member variable 2';
          'Data type' 'member variable 3';
          ...
};

'Struct name' : Indicates the name of the struct to be used.

'Data type' 'member variable' : Declaration of the member variables of the struct, same as general variable declaration. However, the initial value can not be given because it only tells the struct that composes the data type.

- To declare a struct variable for a new data type, of course, the struct must be declared before declaration of the struct variables.

a) struct 'struct name' 'struct variable' (C language, C ++ language)
b) 'struct name' 'struct variable' (C ++ language)
- In C, the new data type 'struct name' must be prefixed with struct.

- To use member variables of a struct, use the following dot operator.

'Structure name'.'Struct variable'

- You can use the dot operator to store data in member variables or use member variables. Let's look at an example.

Example Code

#include <iostream>
using namespace std;

struct t_Struct{
   int num;
   char fruit[30];
   char color[30];
};

int main() {
     t_Struct strA;

     cout << " Insert number = " ;
     cin  >> strA.num ;

     cout << " Insert fruit  = " ;
     cin  >> strA.fruit ;

     cout << " Insert color  = " ;
     cin  >> strA.color ;

     cout << endl;
     cout << "*** Print results ***" << endl;

     cout << " number = " << strA.num << endl ;
     cout << " fruit  = " << strA.fruit << endl ;
     cout << " color  = " << strA.color << endl ;

     return 0;
}

- We have declared a struct named 'strA'. And we have defined the integer variable 'num', the string variables 'fruit' and 'color' as member variables.

- We have declared the struct variable 'strA' in the main function. And we saved the data to each member variable through keyboard input. (ex: "cin >> strA.num;")

- We have also written the syntax to output the input data.

results :

 Insert number = 10
 Insert fruit  = banana
 Insert color  = blue

- You will be asked to enter numbers, fruits and colors. Type 10, banana, blue in turn, and hit Enter.

 Insert number = 10
 Insert fruit  = banana
 Insert color  = blue

*** Print results ***
 number = 10
 fruit  = banana
 color  = blue

- You can see that the input result is printed.

Easy tutorial for C / C ++ - string 4 (Two-dimensional arrays and pointer to string arrays)

Easy tutorial for C / C ++ - string 4 (Two-dimensional arrays and pointer to string arrays)

- Learn how to store multiple strings. You can define multiple one-dimensional arrays and save them as different array names. And you can also use a two-dimensional array to store multiple strings. Let's look at how to store strings using a two-dimensional array.

- In a two-dimensional array, rows can be represented by the number of strings to be stored, and columns by one greater than the length of the longest string (including the null character).

- Let's look at it in two ways: a two-dimensional array and a pointer to string array.

Example Code

#include <iostream>
#include <string.h>
using namespace std;

int main() {
     char st1[3][10] = { "black",
                                     "blue",
                                     "orange" };

     cout << " Print \'st1[3][10]\' " << endl ;
     for(int i=0; i<3; i++){
          cout << "st1[" << i << "] = " << st1[i] <<endl;
          }
     cout << endl ;

     char *st2[3] = { "white",
                                  "red",
                                  "pink" };

     cout << " Print \'*st2[3]\' " << endl ;

          for(int i=0; i<3; i++){
          cout << "st2[" << i << "] = " << st2[i] <<endl;
          }
     cout << endl ;

          for(int i=0; i<3; i++){
          cout << "*st2[" << i << "] = " << *st2[i] <<endl;
          }
     return 0;
}

- We have defined a two-dimensional array 'st1' to store strings. To store the three strings 'black, blue, orange', the rows and columns were set to 3 and 10, respectively. Let's print out the stored strings using the for loop. If you omit a column in a two-dimensional array and represent only the row elements, it represents the starting address of the row. We will print the start address (st1 [i]) of each line after the '<<' operator.

- Defined a pointer array '* st2 [3]' that stores three character pointers, and gave 'white, red, pink' as initial value. The pointer to array stores the start address value of each string.

- After the '<<' operator, we represent the elements of the pointer array (st2 [i]) and output the stored strings. Let's also print the '*' operator in front of the pointer array element.

results :

Print 'st1[3][10]'
st1[0] = black
st1[1] = blue
st1[2] = orange

 Print '*st2[3]'
st2[0] = white
st2[1] = red
st2[2] = pink

*st2[0] = w
*st2[1] = r
*st2[2] = p

- You can see three strings 'black, blue, orange' as a result of a two-dimensional array 'st1'.

- As a result of the pointer array 'st2', the string 'white, red, pink' is output.

- Each element of the pointer to array 'st2' stores the start address value of each string. You can see that the start (first) value of the string is printed because the '*' operator is prepended.