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() определяет, принадлежит ли точка с координатами (xpyp)
// кругу с координатами центра и радиусом, определенными в объекте класса 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

No comments:

Post a Comment