اذهب إلى المحتوى
  • 0

إنشاء الدالة البانية (constructor) في لغة ++C

Mari Carmen

السؤال

Recommended Posts

  • 0

مرحباً @Mari Carmen

الأسئلة الثلاثة الأولى تطلب منك إنشاء الدالة البانية (constructor) لأصناف الثلاثة (School، Node، Student) أما السؤال الرابع لإنشاء مُدمر (destructor) للصنف Student

  • إنشاء School constructor
class school
{
  string Name;
  Node *Head;
  int n;//number of classes in school

  public:
    school(string name) {
      this->Name = name;
      this->n = 0;
    }
};
  • إنشاء Node constructor 
class Node {
  int ID;
  int NoOfStudents;  int NoOfExams;
  student *t;// a linked list of students is allocated dynamically
  Node *Next;

  public:
    Node(int id) {
      this->ID = id;
      this->NoOfStudents = 0;
      this->NoOfExams = 0;
      this->t = nullptr;
      this->Next = nullptr;
    }

};
  • إنشاء Student constructor 
class student
{
    long ID;
    string Name;
    string  Address;
    float grades[3];
    student *below;

public:
    student(long id, string name, string address) {
        this->ID = id;
        this->Name = name;
        this->Address = address;
    }

};
  • إنشاء Student destructor: يقوم فقط بطباعة الجملة المطلوبة:
~student() {
  cout << "student destructor is called";
}

ليُصبح الكود بهذا الشكل:

class student
{
    long ID;
    string Name;
    string  Address;
    float grades[3];
    student *below;

public:
    student(long id, string name, string address) {
        this->ID = id;
        this->Name = name;
        this->Address = address;
    }

    ~student() {
        cout << "student destructor is called";
    }

};



class Node {
    int ID;
    int NoOfStudents;  int NoOfExams;
    student *t;// a linked list of students is allocated dynamically
    Node *Next;

public:
    Node(int id) {
        this->ID = id;
        this->NoOfStudents = 0;
        this->NoOfExams = 0;
        this->t = nullptr;
        this->Next = nullptr;
    }

};

class school
{
    string Name;
    Node *Head;
    int n;//number of classes in school

public:
    school(string name) {
        this->Name = name;
        this->n = 0;
    }
};

يُمكنك إنجاز بقية الأسئلة كما يُمكنك الإعتماد على هذه الدروس: سلسلة ++c للمحترفين 

بالتوفيق.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

مرحبا ماري

ستجدين ها هنا جواب للأسئلة 6 الموجودة في الباوربوينت مع شرح لها:

#include <string>
using namespace std;

class student{
    int ID; 
    string   Name; 
    string  Address;
    float grades[3];
    student *below;
    
    public:
        // سؤال 3
        // هنا قمت بإنشاء البناء
        // الخاص بالكلاس
        student(string Name, string Address){
            this->Name = Name;
            this->Address = Address;
        }
        // سؤال 4
        // هنا قمت بتدمير جميع الأوبجكت الموجودة بالكلاس التي تم حجزها يدويا
        ~student() {
           delete below;
        }
};

class Node{	// the node represents a class in school
    int ID;
    int NoOfStudents;  
    int NoOfExams;
    student *t;// a linked list of students is allocated dynamically
    Node *Next;
    
    public:
        // سؤال 2
        // هنا قمت بإنشاء البناء
        // الخاص بالكلاس
        Node(string school_name){
            this->NoOfStudents = 0;
            this->NoOfExams = 0;
            this->Next = NULL;
        }
        // سؤال 5
        // هنا قمت بتدمير جميع الأوبجكت الموجودة بالكلاس التي تم حجزها يدويا
        ~student() {
           delete Next;
        }
};

class school{
    string Name;  
    Node *Head;
    int n;//number of classes in school
    
    public:
        // سؤال 1
        // هنا قمت بإنشاء البناء
        // الخاص بالكلاس
        school(string school_name){
            this->Name = school_name;
            this->n = 0;
        }
        // سؤال 6
        // هنا قمت بتدمير جميع الأوبجكت الموجودة بالكلاس التي تم حجزها يدويا
        ~school() {
           delete Head;
        }
};

int main(){
    
    return 0;
}

كما يمكنك من هنا تنفيد البرنامج مباشرة.

للمزيد من المعلومات حول البناء constructors في C++ من هنا ومن هنا حول destructors.

تحياتي.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

1. Create a class: This function creates a new class in the school by reading the class information from a text file that has the following format. The class is added to the end of the school linked list. Note that this will call the Node constructor (once)+ the student constructor (multiple times). 2. Read exam grades: This function takes the class ID and the number of the exam (first:0, second:1, Final:2) from keyboard and reads the grades for all students in the class in a certain exam. Sort the students alphabetically then read their grades (this calls the function sort students (8)). 3. Compute student’s average: Take a student’s ID and return his average 4. Add student: Enter class ID, read a student’s information (ID & name & address from keyboard), add a student to the beginning of the class list. Note that this calls the student’s constructor. Also, read (NoOfExams) grades for this student

 

اريد هذه 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0
بتاريخ 10 ساعات قال Mari Carmen:

1. Create a class: This function creates a new class in the school by reading the class information from a text file that has the following format. The class is added to the end of the school linked list. Note that this will call the Node constructor (once)+ the student constructor (multiple times). 2. Read exam grades: This function takes the class ID and the number of the exam (first:0, second:1, Final:2) from keyboard and reads the grades for all students in the class in a certain exam. Sort the students alphabetically then read their grades (this calls the function sort students (8)). 3. Compute student’s average: Take a student’s ID and return his average 4. Add student: Enter class ID, read a student’s information (ID & name & address from keyboard), add a student to the beginning of the class list. Note that this calls the student’s constructor. Also, read (NoOfExams) grades for this student

 

اريد هذه 

مرحباً بك،
الأسئلة ليست سهلة و ليست صعبة لكنها تحتاج منك بحث لذلك أنصحك بالتمرن عليها و البحث حتى تستطيعي حلها بخصوص السؤال الأول يطلب منك إنشاء class أي قسم جديد داخل المدرسة و قراءة معلومات القسم من ملف txt مع إدخال كافة الطلاب في القائمة المترابطة الخاصة بالطلاب  بإمكانك عمل هذا من خلال الدالة التالية:

Node* create_class() {
    ifstream file("file.txt"); // فتح الملف
    string line; // إنشاء متغير سيحمل مُحتوى كل سطر من الملف
    Node* node; // إنشاء مؤشر على كائن من النوع قسم
    int node_id; //  إنشاء متغير سيحمل قيمة ID للقسم التي سنقرأها من الملف
    int i = 0; // عداد
    if (file.is_open()) { // فحص إذا كان الملف مفتوح
        while (getline(file, line)) // عمل حلقة و جلب كل مرة سطر من الملف
        {
            stringstream ss(line); // نستخدمه لفصل الكلمات التي في السطر الواحد
            if (i == 0) { // في حالة السطر الأول
                ss >> node_id; // الخاصة بالقسم وتخزينها في متغير IDأخذ قيمة ال
                node = new Node(node_id); // إنشاء القسم
            } else if(i == 1) { // في حالة السطر الثاني
                ss >> node->NoOfStudents; // أخذ قيمة عدد الطلاب و تخزينها
            } else { // من هنا نبدأ في قراءة معلومات الطلاب و كل سطر يحوي معلومات طالب
                long id; // إنشاء بعض المتغيرات لتخزين البيانات
                string name;
                string address;
                ss >> id; // IDأخذ ال
                getline(ss, name, ','); // أخذ الإسم
                getline(ss, address); // أخذ العُنوان 
                
                student* s = new student(id, name, address); // إنشاء طالب بإستخدام المعلومات
                s->below = nullptr; //NULL جعل مؤشر الطالب الموالي 
                node->add_student(s); //  إضافة الطالب للقسم
            }
    
            i++; // رفع قيمة العداد
        }
        
        file.close (); // غلق الملف
    }  
    else
        cout << "Unable to open file";
        
    node->Next = nullptr; // NULL جعل مؤشر القسم التالي 
    return node; // إرجاع مؤشر القسم الذي أنشأناه
}

بإمكانك تجربة المثال من هنا و هذا مُحتوى الملف:

file_content.PNG.99d2e8891e62a8e85bab0d807f721fb7.PNG

و هذه نتيجة المثال:

output_linked.PNG.5d222d63281032d16fe52c4200ff8977.PNG

بالتوفيق.

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...