Showing posts with label Cplusplus. Show all posts
Showing posts with label Cplusplus. Show all posts

Objects and binary files( Объекты и двоичные файлы )

Object and binary file

Stroing data in binary file
Binary file မ်ားသည္ ပိုမို၍ တိက် က်စ္လစ္မွဳ႕ရွိျပီး တစ္ခ်ိဳ႕ေသာအေျခအေနမ်ိဳးတြင္ operate ျပဳလုပ္ရန္ပိုမို၍ အဆင္ေျပလြယ္ကူေစသည္။
Binary file အား open ျပဳလုပ္ရန္အတြက္ access_mode ျဖစ္ေသာ ios::binary အား မျဖစ္မေန အသံုးျပဳရမည္။ ( တစ္ခ်ိဳ႕ေသာ C++ compiler မ်ားတြင္ ios::bin )
Output file အတြက္ object တစ္ခုအားတည္ေဆာက္ရန္အတြက္ ေအာက္ေဖာ္ျပပါအတိုင္းျပဳလုပ္ရမည္။
ofstream out_fil(“Outfil.dat”,ios::out | ios::binary);
if(!out_fil) { cerr<<”Error:Outfil.dat”<<endl;
                   exit(1);
               }
တည္ရွိျပီးသား binary_file တစ္ခုအား read ျပဳလုပ္လိုလွ်င္ ေအာက္ေဖာ္ျပပါအတိုင္း object တည္ေဆာက္ရမည္။
Ifstream in_fil(“Infil.dat”,ios::in | ios::binary);
if(!in_fil) {   cerr<<”Error:Infil.dat”<<endl;
                  exit(2);
              }

သို႕ေသာ္ တကယ့္လက္ေတြ႕တြင္ binary_file မ်ားအား operate ျပဳလုပ္ရန္အတြက္ တစ္ျခား ေသာ လုပ္ေဆာင္ခ်က္ အနည္းအား မိမိျပဳလုပ္လိုသည့္ ပုစၦာေပၚမူတည္၍ အနည္းငယ္ ထပ္မံ ေပါင္းထည့္ရန္ လိုအပ္ေပသည္။

Example. Write double type data into binary file.

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <conio.h>
using namespace std;

Object and File Stream.Text file(Объекты и файловые потоки.Текстовые файлы)

Object and File Stream.Text file

Input/output stream of text file(Потоковый ввод/вывод дисковых файлов)

C++ Language တြင္ input/output operation မ်ားသည္ stream မွတစ္ဆင့္ လုပ္ေဆာင္ၾကသည္ ျဖစ္သည္။
Stream ဆိုသည္မွာ Logical သေဘာတရားအရသာ တည္ရွိသည္ျဖစ္ျပီး information မ်ားအား လက္ခံရယူရန္ႏွင့္ ျပန္လည္ထုတ္ေပးရန္အတြက္ ျပဳလုပ္ေပးသည္ျဖစ္သည္။
Keyboard ႏွင့္ Screen display မ်ားသည္ basic input/ouput Stream မ်ားျဖစ္ၾကသည္။ ၎တို႕၏ လုပ္ေဆာင္ခ်က္မ်ားသည္ text ဖိုင္မ်ားကဲ့သို႕ပင္ျဖစ္ၾကသည္။
Disk ဖိုင္မ်ားႏွင့္ အလုပ္လုပ္ရန္အတြက္ မျဖစ္မေန <fstream> ဟူေသာ header file ကို declare ျပဳလုပ္ရမည္ျဖစ္သည္။<fstream> ဟူေသာ file တြင္ ေအာက္ေဖာ္ျပပါ class မ်ားပါ၀င္သည္။
ifstream        - for input
ofstream       - for output
fstream         - read and write data in the same file

Disk file မ်ားႏွင့္ အလုပ္လုပ္နိုင္ရန္အတြက္ ၎အား ပထမဦးစြာ open ျပဳလုပ္ရန္လိုသည္။ထိုသို႕ open ျပဳလုပ္ရာတြင္ ios class တြင္ရွိေသာ open_mode အား define ျပဳလုပ္ေပးေသာ access_mode မ်ားကိုအသံုးျပဳရမည္ျဖစ္သည္။

Access_mode
Standard
Action
ate(attend)
Yes
When opening a file sets the file pointer to the end of file
binary (bin)
Yes
Open the file in binary representation
in
Yes
Open the file for reading (input)
out
Yes
Open the file for writing (output)


Text file(
Текстовые файлы)

Creating and write(Создание и запись)

Text file ေတြကိုတည္ေဆာက္ဖို႕အတြက္ ofstream class ရဲ႕ object အေနနဲ႕ တည္ေဆာက္ရမည္ျဖစ္ျပီး class ရဲ႕ constructor ရဲ႕ ပထမ parameter ေနရာမွာ ဖိုင္နာမည္ျဖစ္ျပီး ဒုတိယ parameter ေနရာမွာ access_mode ကိုေရးသားရမည္ျဖစ္သည္။
ofstream out_file(“out.txt”,ios::out);

Algorithm(алгоритм)

Algorithm(алгоритм)

ပထမဆံုး အေနနဲ႕ algorithm ဆိုတာဘာလဲဆိုေတာ့္ instruction sequence (ညြန္းၾကားခ်က္(လုပ္ေဆာင္ခ်က္) အစဥ္အစီ ကိုေခၚဆိုတာပါ။
( /* ကၽြန္ေတာ္တို႕ computer science အေၾကာင္းေလ့လာခဲ့ရာတြင္ computer ဆိုတာ computer science နယ္ပယ္တြင္ tool တစ္ခုၿဖစ္ၿပီး ထို႕ေနာက္ မည္ကဲ့သို႕ အသံုးၿပဳနိဳင္ေၾကာင္းနွင့္ ၎အကူအညီၿဖင့္ အစြမ္းထက္ေစနိဳင္ေသာ programming ဆုိတာ coding အဓိက မဟုတ္ပဲ problem solving ၿဖစ္ၿပီး ထို problem solve ၿပဳလုပ္ရာတြင္ မည္ကဲ့သို႕ step by step ၿပဳလုပ္ရန္ေလ့လာရာတြင္ algorithm ၿဖင့္ အက်ဥ္းမ်ဥ္း သံုးသပ္ရၿဖင့္ computer_algorithm ကို စတင္ေလ့လာခဲ့ရၿခင္းၿဖစ္သည္ */ )

Algorithm မွာ အဓိက အသံုးၿပဳတဲ့ support ၿဖစ္ေစတဲ့ tool နွစ္ခုကေတာ့္ flowchart နဲ႕ Pseudocode တို႕ပဲၿဖစ္ပါတယ္။flowchart ကေတာ့္ program အေသးေတြအတြက္ၿဖစ္ၿပီး Pseudocode ကေတာ့္ large problem အတြက္ပဲၿဖစ္ပါတယ္။
Flowchart ဆိုတာကေတာ့္ algorithm ကို graphical နဲ႕ေဖာ္ၿပတယ္လို႕ပဲ အလြယ္မွတ္ယူနိဳင္ပါတယ္။
ကၽြန္ေတာ္တို႕ graphical နဲ႕ေဖာ္ၿပဖို႕အတြက္ symbol ေတြအမ်ားၾကီးရွိပါတယ္။အဲထဲကမွ အဓိကအသံုးၿပဳတဲ့ symbol 6 ခု (အေၿခခံ အက်ဆံုး ) ကေအာက္ပါအတိုင္းပဲ ၿဖစ္ပါတယ္။

INHERITANCE(Наследование)

Inheritance(Наследование)
Inheritance ဆိုသည္မွာ class(класс) တစ္ခုသည္ အၿခား class တစ္ခုမွ properties မ်ားအားရရွိနိဳင္ေစရန္နွင့္ ထို class ထဲသို႕လည္း မိမိ၏ properties မ်ားအားထည့္သြင္းနိဳင္ရန္အတြက္ classes မ်ားအား Hierarchy (иерархия классов)စနစ္ကဲ့သို႕ ၿပဳလုပ္ဖန္တီးၿခင္းၿဖစ္သည္။ Class တစ္ခုသည္ အၿခား class တစ္ခု၏ inherits ၿဖစ္ေသာအခါ inherits ၿဖစ္ခံရေသာ class ကို base class(базовый класс) ခုေခၚသည္။Inheriting ၿဖစ္ေနေသာ class အား derived class(производного класса) ဟုေခၚသည္။ base class တစ္ခုအား စတင္ assignment ၿပဳလုပ္ၿပီး process တစ္ခုစတင္ခဲ့လွ်င္ ထို base class ၏ qualities မ်ားသည္ ထို base class တစ္ခုမွခြဲထြက္သြားေသာ class မ်ား၏ qualities မ်ားနွင့္ တူညီမည္ၿဖစ္သည္။ထို base class သည္ စတင္သူၿဖစ္ေသာေၾကာင့္  ancestor(предок)(စတင္သူ) ဟုေခၚဆိုၿပီး derived class အား  တစ္ခါတစ္ရံ - descendant(потомки) (ဆင္းသက္လာသူ ) ဟုေခၚဆိုသည္။
Inheritance ၏ syntex(ေရးနည္းပံုစံ) သည္ေအာက္ပါအတိုင္းၿဖစ္သည္။

class derived_class_name:access_specifier  base_class_name   
{  // Declaration of a derived class };

//access specifiers ဆိုရာတြင္ ( public , protected , private ) တစ္ခုခု ၿဖစ္နိဳင္သည္။

OPERATOR OVERLOADING(Перегрузка операторов)


Operator overloading(Перегрузка операторов)

Operator overloading နွင့္ fuction overloading တို႕၏ ၿခားနားခ်က္သည္ operator overloading သည္ class နွင့္အၿမဲတမ္းဆက္စပ္ေနေလ့ရွိသည္။မူရင္း တန္ဖိုးတစ္ခုမွေန၍ operator overload ၿပဳလုပ္လွ်င္ ၎၏ original တန္ဖိုးသည္ ဆံုးရွံဳးေပ်ာက္ကြယ္သြားလိမ့္မည္ မဟုတ္ေပ။သို႕ေသာ္ ၎သည္ ေၿပာင္းလဲမွဳ႕အေၿခအေနတစ္ရပ္ အသစ္တစ္ခု ထပ္မံၿဖစ္ေပၚလာလိမ့္မည္ၿဖစ္သည္။
Operator overloading အား operator_function(оператор-функция)အားၿဖင့္ define ၿပဳလုပ္ရမည္ၿဖစ္သည္။ ၎ function သည္ class ၏ member တစ္ခုၿဖစ္နိဳင္သကဲ့သို႕ friend function လည္းၿဖစ္နိဳင္သည္။
type classname :: operator # (list)
{        action_performed
}
သည္တြင္ # သေကၤတေနရာတြင္ overloaded operator တစ္ခုခုအား အသံုးၿပဳရမည္ၿဖစ္သည္။ ေအာက္ေဖာ္ၿပပါ overloaded operator မ်ားအား အစားထိုးသံုးစြဲနိဳင္သည္။
+  -  *  /  %  ^  &  |  ~  !  !=  ==  <  >  <=  >=  <<  >>  &&  ||  ++  --  =  +=  -=  *=  /=  %=  ^=  &=  |=  <<=  >>=  ->  ->*  ,  []  ()  new  delete.

Operator overloading တြင္ ကန္႕သန္႕ခ်က္ရွိေသာ အေၿခအေနတို႕မွာ ေအာက္ပါအတိုင္းၿဖစ္သည္။  
  • operators can not be overloaded:. . * ::?: Sizeof preprocessor operators;
  • you can not change the priority of operators;
  • can not change the number of operands of the operator with respect to its original state;
  • You can not create new transactions, you can only overload the already existing ones;
  • operator functions can not have parameters passed by default. 

FUNCTION AND OBJECT(Функции и объекты)

Function and object(Функции и объекты)

Pointer to object(Указатели на объекты)
Object ၏ arithmetic pointer(Арифметика указателей ) မ်ားသည္ arithmetic pointer မ်ားအား အၿခား မိမိနွစ္သက္ data type မ်ားနွင့္ အသံုးၿပဳၿခင္းနွင့္ အတူတူပင္ၿဖစ္သည္။၎သည္ သက္ဆိုင္ရာ object မ်ားအား operate ၿပဳလုပ္လိမ့္မည္ၿဖစ္သည္။

Example. Defining and using a pointer to the class object.

#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
class AnyCls {   int a, b;
                        public:
                           AnyCls (int m, int n)  { a = m; b = n; }
                           int get_a() {  return a;  }
                           int get_b() {  return b;  }
                      };
int _tmain(int argc, _TCHAR* argv[])
{ AnyCls ob[4] = { AnyCls(1, 2), AnyCls(3, 4), AnyCls(5, 6), AnyCls(7, 8)   };
    int i;
    AnyCls *Ptr;              // объявление указателя на объект класса
    Ptr = ob;          // адрес начала массива объектов
    for (i = 0; i < 4; i++)  {  cout << Ptr -> get_a() << ' ' << Ptr -> get_b() << endl;
                                           Ptr++;   // переход к следующему объекту
                                       }
    cout << endl;
   _getch();
          return 0;
}

C++ တြင္ special pointer တစ္ခုၿဖစ္ေသာ this – pointer(указатель this) ပါ၀င္ေပသည္။၎ pointer အား call ေခၚလိုက္ေသာ အခါ class ၏ မည္သည့္ function-element(функции-элементу класса) မဆို passing ၿပဳလုပ္နိဳင္ၿပီး call ၿပဳလုပ္လိုက္ေသာ object အားလည္း point ၿပဳလုပ္နိဳင္ေပသည္။ this_pointer သည္ function_element မ်ားသာလွ်င္ passing ၿပဳလုပ္နိဳင္ၿပီး friend_function ၏ element မ်ားအား passing မၿပဳလုပ္နိဳင္ေပ။ class တစ္ခုထဲတြင္ရွိေသာ function_member မ်ားအား တစ္ခုနွင့္တစ္ခု ဆက္စပ္အလုပ္လုပ္လိုလွ်င္ အၿခားေသာ pointer မ်ာအား သီးသန္႕အသံုးၿပဳရန္မလိုေပ။အဘယ္ေၾကာင့္ဆိုေသာ္ function_member အား call ေခၚလိုက္ေသာအခါ ထို object ဆီသို႕ this_pointer သည္အလိုအေလွ်ာက္ passing ၿပဳလုပ္သြားေသာေၾကာင့္ ၿဖစ္သည္။
ေအာက္တြင္ code အမ်ိဳးအစား နွစ္ခုအားေဖာ္ၿပထားသည္။၎တို႕သည္ အတူတူပင္ၿဖစ္သည္။သို႕ေသာ္ မ်ားေသာအားၿဖင့္ ဒုတိယ code အားအသံုးၿပဳေလ့မရွိေပ။
  Overload operation တြင္ this_pointer အသံုးၿပဳနည္းအား နားလည္ေစရန္အတြက္ ေအာက္တြင္ ေဖာ္ၿပၿခင္းၿဖစ္သည္။


#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
class PhotoModel
      {  char name[10];              // имя
          char c_hair[10];            // цвет волос
          char c_eyes[10];          // цвет глаз
          double weight;            // вес
          int height;                // рост
        public:
          PhotoModel (char *n, char *c_h, char *c_y, double w, int h )

              { strcpy(name, n);
                 strcpy(c_hair, c_h);
                 strcpy(c_eyes, c_y);
                 weight = w;
                 height = h;
              }
                { strcpy(this->name, n);
                 strcpy(this->c_hair, c_h);
                 strcpy(this->c_eyes, c_y);
                 this->weight = w;
                 this->height = h;
              }
                                                              
         void show()
 { cout << name << ' '
        << c_hair << ' '
        << c_eyes << ' '
        << weight << ' '
        << height << endl;
             }
  };
  
              void show(){
                { cout << this->name << ' '
                        << this->c_hair << ' '
                        << this->c_eyes << ' '
                        << this->weight << ' '
                        << this->height << endl;
              }
      };

int _tmain(int argc, _TCHAR* argv[])
{ PhotoModel Olga("Olga", "black", "black", 56.5, 170);
   Olga.show();
   _getch();
          return 0;
}

Passing object to functions(Передача объектов в функции )

Passing object by value(Передача объектов по значению)
Object မ်ားတြင္ function passing ၿပဳလုပ္ေသာပံုစံသည္ parameter မ်ားအား အၿခားေသာ data_type မ်ားၿဖင့္ အသံုးၿပဳၿခင္းပင္ၿဖစ္သည္။ထိုသို႕ ၿပဳလုပ္ရန္အတြက္ ၎ class type ၏ function parameter မ်ားအား declare ၿပဳလုပ္ရန္လိုအပ္ေပသည္။ထို႕ေနာက္တြင္ function အား call ေခၚေသာအခါ ထို class ၏ object အတြင္းရွိ argument မ်ားအား အသံုးၿပဳမည္ၿဖစ္သည္။default အေနၿဖင့္ object အား by value အားၿဖင့္ passing(transfer) ၿပဳလုပ္သြားမည္ၿဖစ္သည္။ထိုသို႕ၿပဳလုပ္ၿခင္းတြင္ computer(system) သည္ function တြင္ အသံုးၿပဳေသာ argument မ်ားအား stack-memory တြင္ copy ကူးယူလိမ့္မည္ၿဖစ္သည္။

Example. Passing object to a function by value.

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
class circle {   int x, y, r;
                     public:
                        circle(int kx, int ky, int rad)   { x = kx;  y = ky;  r = rad;  }
                        void get_c(int &kx, int &ky, int &rad)   { kx = x;  ky = y;  rad = r;  }
                        float get_square()  { return 3.14159 * r * r;  }
                   };
// функция point_in() определяет, принадлежит ли точка с координатами (xp, yp)
// кругу с координатами центра и радиусом, определенными в объекте класса circle
int point_in(circle ob, int xp, int yp)
 {    int res, r_x, r_y, r_r;
       ob.get_c(r_x, r_y, r_r);
       if  ( pow(double(xp - r_x), 2) + pow(double(yp - r_y), 2) < pow(double (r_r), 2) )      res = 1;
                 else res = 0;
       return res;
   }
int _tmain(int argc, _TCHAR* argv[])
{ int result, xt = 7, yt = 10;
   circle c1(5, 2, 17), c2(8, 15, 3);
   cout << c1.get_square() << endl;
   cout << c2.get_square() << endl;
   result = point_in(c1, xt, yt);
   if (result == 1) cout << "Принадлежит" << endl;
       else cout << "Не принадлежит" << endl;
   result = point_in(c2, xt, yt);
   if (result ==1) cout << "Принадлежит" << endl;
       else cout << "Не принадлежит" << endl;
   _getch();
          return 0;
}


Passing objects by pointer(Передача объектов по указателю)
Objet to function အား pointer ၿဖင့္ passing ၿပဳလုပ္ၿခင္းသည္ ၎၏ တန္ဖိုးအား passing(transfer) ၿပဳလုပ္ၿခင္းမဟုတ္ပဲ ၎၏ address ကိုသာလွ်င္passing(transfer) ၿပဳလုပ္ၿခင္းပင္ ၿဖစ္သည္။call ေခၚလိုက္ေသာအခါတြင္လည္း function သည္ ၎၏ argument မ်ားအား ေၿပာင္းလဲနိဳင္သည္ၿဖစ္သည္။

Example. Passing object to a function at the sign.
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
class my_cls {   int x;
                        public:
                          my_cls(int n) { x = n; }
                          void set_x(int n) { x = n; }
                          int get_x() { return x; }
                      };
void cube_obj(my_cls *object)
   { object->set_x(pow(double (object->get_x()), 3));
      cout << object->get_x() << endl;                  // 1000
   }

int _tmain(int argc, _TCHAR* argv[])
{ my_cls ob(10);
   cube_obj(&ob);
   cout << ob.get_x() << endl;                    // 1000
   _getch();
          return 0;
}
Object to function အား by value နည္းၿဖင့္ passing ၿပဳလုပ္ေသာအခါ ထို object အား copy ၿပဳလုပ္လိမ့္မည္ၿဖစ္သည္။ထို function အား အလုပ္လုပ္ၿခင္းၿပီးဆံုးသြားေသာအခါ copy တည္ေဆာက္
ထားေသာ object အား ပယ္ဖ်က္ပစ္လိမ့္မည္ၿဖစ္သည္။function အား call ေခၚလိုက္ေသာအခါ copy တည္ေဆာက္ထားေသာ object အား constructor သည္ call ေခၚလိမ့္မည္မဟုတ္ေပ။ထို အရာသည္ object ၏ element မ်ားအား initialize ၿပဳလုပ္ရန္အသံုးၿပဳၿခင္းသာ ၿဖစ္သည္။

Passing object by reference(Передача объектов по ссылке)
Reference ဆိုသည္မွာ hidden pointer တစ္ခုၿဖစ္ၿပီး အၿမဲတမ္း အၿခားေသာ variable name ၿဖင့္ အလုပ္လုပ္ေလ့ရွိသည္။parameter မ်ားအား by reference ၿဖင့္ pass ၿပဳလုပ္ပါက object အတြင္းရွိ function မ်ားသည္ မူရင္း object မွေၿပာင္းလဲသြားနိဳင္ၿပီး operate ၿပီးဆံုးသြားေသာ အခါတြင္ ထိုအတိုင္းပင္ save ၿပဳလုပ္သြားလိမ့္မည္ၿဖစ္သည္။
Reference သည္ pointer နွင့္အသြင္မတူေပ။ထို႕ေၾကာင့္ object အား by reference ၿဖင့္ passing ၿပဳလုပ္ပါက element မ်ားအား access ၿပဳလုပ္ရန္အတြက္ operator “arrow”(->) အားအသံုးၿပဳရန္မလိုအပ္ပဲ operator “dot”(.) သာလွ်င္အသံုးၿပဳရမည္ၿဖစ္သည္။

Example . Passing object to a function by reference.

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
class my_cls {   int x;
                        public:
                          my_cls(int n) { x = n; }
                          void set_x(int n) { x = n; }
                          int get_x() { return x; }
                      };
void cube_obj(my_cls &object)
   { object.set_x(pow(double (object.get_x()), 3));
      cout << object.get_x() << endl;            // 1000
   }
int _tmain(int argc, _TCHAR* argv[])
{ my_cls ob(10);
   cube_obj(ob);
   cout << ob.get_x() << endl;                    // 1000
   _getch();
          return 0;
}


Objects as return values(Объекты в качестве возвращаемых значений)
Function တြင္ object သည္ return ၿပန္နိဳင္ရန္အတြက္ return value အား class_name အတိုင္းေၾကၿငာ ေပးရမည္ၿဖစ္သည္။ထို႕ေနာက္တြင္ operator “ return” ေနာက္တြင္ ထို object အားထားရွိရမည္ၿဖစ္သည္။

Example6. Class declaration, closed elements are hours and minutes. Will add to the minutes and hours, and return some value result.
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
class time {  int hour;
                     int minute;
                   public:
                     time() { hour = 0; minute = 0;  }
                     time(int h, int m) { hour = h; minute = m;  }
                     void showtime() { cout << hour << ':' << minute << endl;  }
                     time add_time(time);
                 };
time time :: add_time(time t2)
{ time tmp;
   tmp.minute = minute + t2.minute;
   if (tmp.minute >= 60)
      { tmp.minute -= 60;
         tmp.hour = 1 + hour + t2.hour;
      }
      else  tmp.hour = hour + t2.hour;
   return tmp;
}
int _tmain(int argc, _TCHAR* argv[])
{time t1(12, 15), t2(17, 50), t3;
     t3 = t1.add_time(t2);
     t1.showtime();
     t2.showtime();
     t3.showtime();
   _getch();
          return 0;
}
Function သည္ object အား return ၿပန္ေသာအခါ ၎အတြက္ ယာယီ object အား အလိုအလွ်ာက္ ဖန္တီးသြားလိမ့္မည္ၿဖစ္သည္။ထိုသို႕ return ၿပန္ၿပီးေသာအခါ ထို ယာယီ object အား ပယ္ဖ်က္ၿပစ္လိမ့္မည္ ၿဖစ္သည္။

Friend functions
Friend function သည္ class ၏ element မဟုတ္ေပ။သို႕ေသာ္ class ၏ private နွင့္ protect member မ်ား access ၿပဳလုပ္နိဳင္စြမ္းရွိသည္။class တစ္ခုတြင္ friend function အားေၾကၿငာရန္အတြက္ friend ဆိုေသာ keyword အားအသံုးၿပဳရမည္ၿဖစ္သည္။
Friend function သည္ အၿခား class တစ္ခု၏ member အၿဖစ္လည္း တည္ရွိေစနိဳင္သည္။ထို႕သို႕မဟုတ္ပဲ  မည္သည့္ class ၏ member အၿဖစ္မတည္ရွိပဲ သီးၿခားလည္းၿဖစ္တည္နိဳင္သည္။
Friend function အား declare ၿပဳလုပ္ေသာ syntax သည္ ေအာက္ပါအတိုင္း ၿဖစ္သည္။
class class_name
{        friend type name_other_class :: function_name(list_of_parameter);
          friend type function_name(list_of_parameter);
}

Example
Class MyCls
{        friend viod AnotherClass :: MemberFnName(int,float);
          Friend void RegularFnName(char);
}

Example8. Use of friend function.
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
class MyCls
    {   int div, val;
       public:
         MyCls(int d, int v)  { div = d; val = v; }
         friend int Divisibility(MyCls ob);
    };
int Divisibility(MyCls ob)
    {  if ( !(ob.div % ob.val) )   return 1;
             else return 0;
    }
int _tmain(int argc, _TCHAR* argv[])
{MyCls ob1(10, 5), ob2(13, 7);
    if (Divisibility(ob1)) cout << " ob1.div кратно ob1.val" << endl;
         else cout << " ob1.div не кратно ob1.val" << endl;
    if (Divisibility(ob2)) cout << " ob2.div кратно ob2.val" << endl;
         else cout << " ob2.div не кратно ob2.val" << endl;
   _getch();
          return 0;
}
Friend function သည္ class ၏ member တစ္ခုမဟုတ္ေပ class ၏ friend တစ္ခုသာၿဖစ္ေသာေၾကာင့္  object name အားအသံုးၿပဳ၍ call ေခၚ၍မရေပ။
Friend function တြင္ class ၏ close(private) member မ်ားအား access ၿပဳလုပ္ရန္အတြက္ class ၏ object မွတစ္ဆင့္ ၿပဳလုပ္ရသည္ၿဖစ္သည္။ထို႕ေၾကာင့္ member_function မ်ားနွင့္ friend_function ၏ၿခားနားခ်က္သည္ function_member မ်ားသည္ member_function သည္ private_member မ်ားအား object_name မွတစ္ဆင့္ access ၿပဳလုပ္စရာမလိုပဲ function_member မ်ားသည္ parameter မ်ားအား access ၿပဳလုပ္ရာတြင္ object မွတစ္ဆင့္ ဆက္သြယ္ၿပဳလုပ္ရသည္ၿဖစ္သည္။

Example . Declare two classes that describe the passenger and cargo aircraft and compare their speed and range.
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
class aero_w;                           // ссылка вперед
class aero_p {   int passengers;
                          int dist_of_flight;
                          int speed;
                       public:
                          aero_p(int p, int d, int s)  {  passengers = p; dist_of_flight = d; speed =s;  }
                          friend int comp_char(aero_p a_p, aero_w a_w, int &charact);
                     };

class aero_w {   int weight;
                          int dist_of_flight;
                          int speed;
                       public:
                          aero_w(int w, int d, int s)  { weight = w; dist_of_flight = d; speed =s;  }
                          friend int comp_char(aero_p a_p, aero_w a_w, int &charact);
                     };
// программа сравнения характеристик самолетов
int comp_char(aero_p a_p, aero_w a_w, int &charact)
   {   int result;
        switch (charact)
          { case 1: result = a_p.dist_of_flight - a_w.dist_of_flight;       
                         break;
             case 2: result = a_p. speed - a_w. speed;      
                         break;
          }
        return result;
   }
int _tmain(int argc, _TCHAR* argv[])
{int res, p_p, p_d, p_s, w_w, w_d, w_s, condition;
   cout << "Характеристики пассажирского самолета: ";
   cin >> p_p >> p_d >> p_s;
   cout << "Характеристики грузового самолета: ";
   cin >> w_w >> w_d >> w_s;
   aero_p pas(p_p, p_d, p_s);
   aero_w wei(w_w, w_d, w_s);
   condition = 2;
   res = comp_char(pas, wei, condition);
   if (res > 0) cout << "Пассажирский летает быстрее";
      else if (res < 0) cout << "Грузовой летает быстрее ";
                 else cout << "Скорости одинаковы";
   _getch();
          return 0;
}

Friend function တြင္ ေအာက္ေဖာ္ၿပပါ စည္းမ်ဥ္းမ်ားရွိရသည္ ၿဖစ္သည္။
-၎၏ specifier မ်ားေဖာ္ၿပရာတြင္ specifier မ်ားၿဖစ္ေသာ public, protected နွင့္ private မ်ားသကဲ့သို႕လုပ္ေဆာင္ခ်က္မ်ိဳး မဟုတ္ေပ။
-၎သည္ တစ္ခုနွင့္တစ္ခု အခ်င္းခ်င္း ဆက္စပ္မွဳ႕မ်ိဳးမရွိေပ။ဆိုလိုသည္မွာ A သည္ အၿခားေသာ B ဆီသို႕ declare ၿပဳလုပ္သည္နွင့္တစ္ၿပိဳင္နက္ ့ B သည္ A ဆီသို႕ declare ၿပဳလုပ္ၿခင္းမ်ိဳးၿဖစ္ေပၚလာလိမ့္မည္ မဟုတ္ေပ။
-friend_function သည္ inherited ၿပဳလုပ္လိမ့္မည္မဟုတ္ေပ။ဆိုလိုသည္မွာ အကယ္၍ B သည္ A ၏ friend ၿဖစ္လွ်င္ B မွ ၿဖစ္လာေသာ class မ်ားအားလံုးသည္ A ၏ private element မ်ားအၿဖစ္သို႕ အလိုအေလွ်ာက္ access ၿဖစ္ေပၚသြားလိမ့္မည္ မဟုတ္ေပ။
-friend_function သည္ ကူးေၿပာင္းနိဳင္ေသာ ဂုဏ္သတၱိမရွိေပ။ဆိုလိုသည္မွာ A သည္ B ဆီသို႕ေၾကၿငာလွ်င္ A ၏ မွဆင္းသက္လာေသာ class မ်ားအားလံုးသည္ B ဆီသို႕ အလိုအေလွ်ာက္ friend အၿဖစ္သတ္မွတ္လိမ့္မည္ မဟုတ္ေပ။
-friend_function သည္ေရြ႕ေၿပာင္းနိဳင္ေသာ ဂုဏ္သတၱိမရွိေပ။ဆိုလိုသည္မွာ A သည္ B ၏ friend ၿဖစ္ၿပီး B သည္အၿခားေသာ C ၏ friend ၿဖစ္ပါက A သည္ C ၏ friend အၿဖစ္သို႕ အလိုေလ်ာက္ ရာက္ရွိလိမ့္မည္မဟုတ္ေပ။

translated by zmk@miet51