Learning ©➕➕
4.52K subscribers
81 photos
2 videos
38 files
182 links
ک
Download Telegram
#70
#class
#struct
#call #by #value
#call #by #reference

✅ فراخوانی struct ها به صورت call by value انجام میشه که نیاز به & هست ولی فراخوانی class ها به صورت call by reference صورت میگیره یعنی :
#include "stdafx.h"

#include <iostream>

using namespace std;

🔹تعریف struct که برای مقدار دادن به داده هاش باید & توی تابع set_data گذاشت :
struct d

{

  int x;

  int y;

  int z;

void set_data(struct d &w, int x, int y, int z);

void show(struct d a);

};

void set_data(struct d &w, int x, int y, int z)

{

  w.x = x;

  w.y = y;

  w.z = z;

}

void show(struct d a)

{

  cout << a.x << " " << a.y << " " << a.z;

}

int main()

{

  d a{};

  set_data(a, 2, 3, 4);

  show(a);

  cout << endl;

  system("pause");

  return 0;

}

🔹تعریف class که برای مقدار دادن به داده هاش نیازی به استفاده از & نیست :
class test

{

public:

  void set(int a, int b, int c)

  {

    x = a;

    y = b;

    z = c;

  }

  void show()

  {

    cout << x << " " << y << " " << z;

  }

private:

  int x, y, z;

};

int main()

{

  test t;

  t.set(10, 20, 30);

  t.show();

  cout << endl;

  system("pause");

  return 0;

}

@Learncpp