مصفوفة من الكائنات object ومؤشرات للكائنات objects

وقت القراءة: 4 دقائق 65 مشاهدة

في عالم البرمجة الكائنية (OOP) مع لغة C++، تعد إدارة كائنات متعددة بكفاءة أمرًا بالغ الأهمية. هناك مفهومان قويان يساعدان في تحقيق ذلك مصفوفة الكائنات Arrays of Objects و مؤشرات للكائنات Pointers to Objects. سنوضح كلا المفهومين وكيفية توظيفهما في لغة C++.

مثال 1:

#include <iostream>
using namespace std;
int main()
{
    int arr[5] = {10, 50, 40, 77, 33};

    // Printing the memory address of the array (address of the first element)
    cout << arr << endl;

    // Printing the value at the first element of the array
    cout << *arr << endl;

    // Printing the memory address of the second element of the array
    cout << arr + 1 << endl;

    // Printing the memory address of the last element of the array
    cout << arr + 4 << endl;

    // Printing the value at the last element of the array
    cout << *(arr + 4) << endl;
    
    // Using a loop to print all elements of the array using pointer arithmetic
    for (size_t i = 0; i < 5; i++)
    {
        cout<< *(arr + i) << endl;
    }

    return 0;
}

يوضح هذا البرنامج استخدام المؤشرات مع المصفوفات.

ستعرض المخرجات عناوين الذاكرة وقيمها وعناصر المصفوفة بناءً على حساب المؤشر وتكرارات الحلقة.

مثال 2:

#include <iostream>
using namespace std;
int main()
{
    int arr[5] = {10, 50, 40, 77, 33};
    int *p;
    p = arr;
    for (size_t i = 0; i < 5; i++)
    {
        cout << *p << endl;
        p++;
    }

    return 0;
}

يوضح برنامج C++ هذا استخدام المؤشر للتكرار عبر مصفوفة.

ستعرض المخرجات قيم كل عنصر في المصفوفة، مطبوعًا واحدًا تلو الآخر، حيث يتم زيادة المؤشر p داخل الحلقة.

10
50
40
77
33

ملحوظة !

  • يتم تعيين المؤشر p مبدئيًا على عنوان العنصر الأول في المصفوفة.
  • زيادة المؤشر (p++) يجعله يشير إلى العنصر التالي في المصفوفة.
  • يوفر هذا الأسلوب طريقة بديلة للتكرار عبر عناصر المصفوفة باستخدام المؤشرات.

مثال 3:

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
class Student
{
        char name[20];
        int ID;
    public:
        Student() //empty constructor
        {
            cout << "empty\n";
            strcpy(name, "No name");
            ID = 0;
        }
        Student(char n[], int id) //constructor overloading
        {
            cout << "parameterize\n";
            strcpy(name, n);
            ID = id;
        }
        void print(void)
        {
            cout << name << "\t" << ID << endl;
        }
};
int main()
{
    Student S1("Ali", 123), S2("Ahmad", 456);
    Student arr[3];
    for (int i = 0; i < 3; i++)
        arr[i].print();
    return 0;
}

يوضح هذا البرنامج استخدام constructor overloading وdefault constructors في الـclass. دعونا نحلل الكود:

  1. Class Student:
    يحدد class يسمى الطالب مع اسم ومعرف أعضاء البيانات الخاصة.
    يحتوي على constructors اثنين: constructor فارغ وparameterized constructor.
    يوفر دالة عضو وهي دالة الطباعة لعرض تفاصيل الطالب.
  2. Empty Constructor:
    Student()
    {
        cout << "empty\n";
        strcpy(name, "No name");
        ID = 0;
    }
    

    تهيئة الاسم بـ "No name" والمعرف بـ 0.
    طباعة "empty" عند إنشاء كائن باستخدام هذا الـconstructor.

  3. Constructor Overloading:
    Student(char n[], int id)
    {
        cout << "parameterize\n";
        strcpy(name, n);
        ID = id;
    }
    

    تهيئة الاسم والمعرف باستخدام الـarguments المتوفرة.
    طباعة "parameterize" عند إنشاء كائن باستخدام هذا الـconstructor.

  4. print() Function:
    void print(void)
    {
        cout << name << "\t" << ID << endl;
    }
    
    

    طباعة اسم الطالب وهويته.

  5. إنشاء كائنات:
    يتم إنشاء الكائنين S1 وS2 باستخدام parameterized constructor، مع عرض "parameterize" لكل كائن.
    يتم إنشاء مصفوفة من كائنات الطالب باستخدام default constructor، مع عرض "empty" لكل كائن.
  6. طباعة تفاصيل الطالب:
    يتم استدعاء الدالة print() لكل كائن في المصفوفة لعرض تفاصيله.

المخرجات:
ستعرض المخرجات رسائل الـconstructor وتفاصيل كل طالب في المصفوفة.

 

 

 

مشاركة هذا

مصفوفة من الكائنات object ومؤشرات للكائنات objects

Or copy link

المحتوى
Arabic