Menu
Coddy logo textTech
flag Ar iconالعربيةdown icon

الـ Attributes (بيانات الأعضاء)

جزء من قسم Object Oriented Programming في رحلة C++ على Coddy — الدرس 9 من 104.

تخزن أعضاء البيانات (السمات) حالة الكائن. وعادةً ما يتم التصريح عنها كـ private ويتم الوصول إليها من خلال دوال getter/setter العامة. يُعرف هذا بالتغليف (encapsulation).

أعضاء البيانات الخاصة

class BankAccount {
private:
    std::string accountName;
    int balance;
};

دوال الحصول (getters) والتعيين (setters) العامة

class BankAccount {
private:
    std::string accountName;
    int balance;

public:
    void setBalance(int balance) {
        this->balance = balance;
    }
    
    int getBalance() {
        return this->balance;
    }
};

دوال التعيين (Setters) مع التحقق من الصحة

void BankAccount::setBalance(int balance) {
    if (balance >= 0) {
        this->balance = balance;
    }
}

محددات الوصول

class MyClass {
private:    // يمكن الوصول إليها فقط داخل الفئة
    int secret;

public:     // يمكن الوصول إليها من أي مكان
    int getSecret() { return secret; }

protected:  // يمكن الوصول إليها في الفئة والفئات الفرعية
    int shared;
};

جعل أعضاء البيانات private يخفيها عن الكود الخارجي. تقوم دوال الجلب (Getters) بإرجاع قيمها، بينما تقوم دوال التعيين (setters) بتعديلها مع إمكانية التحقق من صحتها. هذا يحمي بياناتك ويتحكم في كيفية الوصول إليها.

challenge icon

التحدي

متوسط

أنشئ فئة BankAccount مع أعضاء بيانات خاصة وطرق وصول عامة:

  • الأعضاء الخاصون: accountName (string)، balance (int)
  • دوال التعيين (Setters): setAccountName، setBalance
  • دوال الجلب (Getters): getAccountName، getBalance
  • deposit(int amount) — يضيف إلى الرصيد إذا كان المبلغ > 0
  • withdraw(int amount) — يرجع "Success" إذا كان ذلك ممكناً، وإلا يرجع "Insufficient funds"

ورقة مرجعية

تُخزن أعضاء البيانات (السمات) حالة الكائن، وعادةً ما يتم التصريح عنها كـ private لتحقيق مبدأ التغليف (encapsulation).

أعضاء بيانات خاصة (Private data members):

class BankAccount {
private:
    std::string accountName;
    int balance;
};

دوال الوصول والتعديل العامة (Public getters and setters):

class BankAccount {
private:
    std::string accountName;
    int balance;

public:
    void setBalance(int balance) {
        this->balance = balance;
    }
    
    int getBalance() {
        return this->balance;
    }
};

دوال التعديل مع التحقق من الصحة (Setters with validation):

void BankAccount::setBalance(int balance) {
    if (balance >= 0) {
        this->balance = balance;
    }
}

محددات الوصول (Access specifiers):

class MyClass {
private:    // Only accessible inside the class
    int secret;

public:     // Accessible from anywhere
    int getSecret() { return secret; }

protected:  // Accessible in class and subclasses
    int shared;
};

جرّب بنفسك

#include <iostream>
#include "BankAccount.h"

int main() {
    std::string name;
    int initial, depositAmt;
    std::getline(std::cin, name);
    std::cin >> initial >> depositAmt;
    
    BankAccount account;
    account.setAccountName(name);
    account.setBalance(initial);
    
    std::cout << "Account: " << account.getAccountName() << std::endl;
    std::cout << "Balance: " << account.getBalance() << std::endl;
    
    account.deposit(depositAmt);
    std::cout << "After deposit: " << account.getBalance() << std::endl;
    
    std::string result = account.withdraw(2000);
    std::cout << "Withdraw 2000: " << result << std::endl;
    std::cout << "Final Balance: " << account.getBalance() << std::endl;
    return 0;
}
quiz iconاختبر نفسك

يتضمن هذا الدرس اختبارًا قصيرًا. ابدأ الدرس للإجابة عليه وتتبّع تقدمك.

جميع دروس Object Oriented Programming