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

خطأ في كود C++

هبة حمدان

السؤال

السلام عليكم 

الحين أنا بواجه هاي المشكلة في كود ++C ومش عارفة ليش مش راضي يغير قيمة الاسم الافتراضية اللي بيدخلها المستخدم رغم انهم في class نفسه فياريت تشوفولي شو المشكلة وهي السؤال الرئيسي هل انا حالّة الكود صح وكيف اعمل اعادة التحميل لدالة بتستقبل ملف يا ريت المساعدة .

1- قم بإنشاء صنف (Customer) وفق التالي:

a.المتغيرات:

   i.      الاسم -:Name محمي من نوع نص.

b.      الدوال:

    i.      بناء يقوم بوضع قيم ابتدائية للاسم.

    ii.      دالة عامة ترجع قيمة الاسم.

   iii.      دالة عامة تستقبل معامل وتغير من خلاله قيمة الاسم. القيمة الافتراضية للمعامل هي "Unknown".

2-      قم بإنشاء صنف (TV) وفق التالي:

a.      المتغيرات:

      i.      الرقم - :No خاص من نوع رقم صحيح.

      ii.      السعر - :price عام رقمي عشري.

b.      الدوال:

       i.      بناء يقوم بقراءة قيم للمتغيرات من خلال لوحة المفاتيح.

       ii.      دالة عامة ترجع قيمة السعر.

       iii.      دالة عامة تستقبل معامل وتغير من خلاله قيمة السعر.

3-      قم بإنشاء صنف (Invoice) يرث من الصنف (Customer) وراثة محمية وفق التالي:

a.      المتغيرات:

      i.      :TVs عامة مصفوفة مكونة من 5 كائنات (TV).

       ii.      Total: خاص رقمي عشري.

b.      الدوال:

      ReadPrices(): عامة وتقوم بقراءة قيم عناصر المصفوفة (TVs) .

     GetTotal(): عامة وتحسب مجموع الاسعار للمصفوفة وتخزينه داخل  المتغير (Total) ثم تقوم بارجاعه.

      بناء يقوم بحساب مجموع الاسعار للمصفوفة وتخزينه داخل  المتغير (Total).

       :Print() عامة وتطبع على الشاشة جميع المتغيرات المحلية والموروثة.

      GetMin(): عامة ترجع  اقل الأسعار في المصفوفة.

      بين مفهوم إعادة التحميل مستخدما الدالة (Print) باستقبالها اسم ملف وكتابة جميع المتغيرات المحلية والموروثة داخل ملف.

4-      الدالة الرئيسية:

a.      انشاء مصفوفة كائنات من نوع (Invoice) تتكون من أي عدد من عندك.

b.      استدعاء جميع الدوال الخاصة والموروثة بالكائن السابق.

وهاد هو الكود :

#include <iostream>
#include <string.h>

using namespace std;

/*---------------------------------------------------*/

class customer{
    protected:
          string Name;
    public:
        customer(){
        Name = "Unknown";}
        
        void get_Name(){
            cout<<Name<<endl;}
            
        void set_Name(){
            cout<<"Enter The Car Name :"<<endl;
            string N ;
            cin>>N;
            Name=N;        }};
            
/*---------------------------------------------------*/
class TV{
    private:
        int No;    
    public:
        float Price;
    TV(){
        cout<<"The Car Number is :"<<endl;
        cin>>No;
        cout<<"The Car Price is :"<<endl;
        cin>>Price;    }
        
    void get_Price(){
        cout<<Price<<endl;    }
        
    void set_Price(float P){
        P = Price;}};
        
/*---------------------------------------------------*/        
class Invoice:protected customer{
    private:
         float Total; 
    public:
        TV TVs[2];
        
        void ReadPrice(){
            cout<<"The Numbers : "<<endl;
            for(int i=0;i<2;i++){
            cout<<TVs[i].Price<<endl;}};
        
        int GetTotal(){
            return Total;}
        
        Invoice(){
            Total = 0;
            for(int i=0;i<2;i++){
            Total+=TVs[i].Price;}    }
        
        void Print(){
            cout<<"The Car Name is :"<<Name<<endl;            
            cout<<"The Sumation is :"<<Total<<endl;    }
        
        int GetMin(){
            float min;
            min=TVs[0].Price;
            for(int i=0;i<2;i++){
                if (TVs[i].Price<min)
                min=TVs[i].Price;}
                cout<<"The minimam :"<<min<<endl;
        }};    
            
/*---------------------------------------------------*/        
int main() {

    Invoice x[3];
    customer y;
    
    for(int i=0; i<3;i++){
    x[i].ReadPrice();
    y.set_Name();
    x[i].Print();
    x[i].GetMin();
    cout<<"********************************"<<endl;
    }
    
    return 0;
}

 

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

Recommended Posts

  • 0
بتاريخ 39 دقائق مضت قال هبة حمدان:

يسملووو كتيير @عبود سميرعلى المساعدة لكن في عندي نقطة صغيرة الحين انا لما نفذت على الرابط اللي أضفته تنفذ الكود تمام لكن لما نسخت الكود على برنامج Dev C++ ما رضي ينفذ الكود وكتبلي الجملة هادي شو بكون السبب :

مرحباً بك،

عندما بحثت عن الأمر وجدت أنه متعلق بإصدار c++ المُستخدم في جهازك بالإضافة إلى الcompiler فالإصدار أقل من 11 سيُسبب لك بعض الأخطاء في حين إذا كان 11 أو أكثر لن تحدث لك أي أخطاء وسيشتغل البرنامج بشكل جيد،

إذا اردت تشغيل البرنامج على جهازك بالإصدار الموجود حالياً بإمكانك عمل بعض التغييرات على كل من الميثود Print التي تكتب في الملف بالشكل التالي:

void Print(string file) {
        ofstream f;
        f.open (file.c_str()); // تغيير هذا السطر
        f << "The Name is : " << name << endl;
        f << "Total is : " << Total << endl;
        f << "TVs: " << endl;
        for(int i=0;i<2;i++){
            f << "TVs[" << i << "] = (Number = " << TVs[i].getNo() << ", Price = " << TVs[i].getPrice() << ")" << endl;
        }

        f.close();
    }

بالإضافة إلى أن الدالة to_string لن تعمل على الإصدار أيضا فيجب تغيير الطريقة إلى شكل آخر و هذه الدالة main:

int main() {

    Invoice* x[2];

    //customer y;

    for(int i=0; i<2;i++){
        char name[20];
        char filename[20];
        sprintf(name, "Invoice %d", i);
        x[i] = new Invoice(name);
        sprintf(filename, "output%d.txt", i);
        x[i]->Print(filename);
        x[i]->getMin();
        cout<<"********************************"<<endl;
    }

    return 0;
}

و هذا الكود ككل:

#include <iostream>
#include <fstream>

using namespace std;

/*---------------------------------------------------*/

class Customer{
protected:
    string name;
public:
    Customer(string name = "Unknown"){
        this->name = name;
    }

    string getName(){
        return name;
    }

    void setName(string name = "Unknown"){
        this->name = name;
    }
};

/*---------------------------------------------------*/
class TV{
private:
    int no;
public:
    float price;
    TV(){
        cout << "The TV Number is : " << endl;
        cin >> no;
        cout << "The TV Price is : " << endl;
        cin >> price;
    }

    float getPrice(){
        return price;
    }

    int getNo(){
        return no;
    }

    void setPrice(float p){
        price = p;
    }
};

/*---------------------------------------------------*/
class Invoice : protected Customer{
private:
    float Total = 0;
public:
    TV TVs[2];

    void ReadPrice(){
        for(int i=0;i<2;i++){
            cout << "TVs[" << i << "] = (Number = " << TVs[i].getNo() << ", Price = " << TVs[i].getPrice() << ")" << endl;
        }
    };

    int getTotal(){
        return Total;
    }

    Invoice(string name) : Customer(name){
        for(int i=0;i<2;i++){
            Total += TVs[i].price;
        }
    }

    void Print(){
        cout << "The Name is : " << name << endl;
        cout << "Total is : " << Total << endl;
        cout << "TVs: " << endl;
        this->ReadPrice();
    }

    void Print(string file) {
        ofstream f;
        f.open (file.c_str());
        f << "The Name is : " << name << endl;
        f << "Total is : " << Total << endl;
        f << "TVs: " << endl;
        for(int i=0;i<2;i++){
            f << "TVs[" << i << "] = (Number = " << TVs[i].getNo() << ", Price = " << TVs[i].getPrice() << ")" << endl;
        }

        f.close();
    }

    int getMin(){
        float min;
        min = TVs[0].price;
        for(int i=0 ;i < 2; i++){
            if (TVs[i].price < min)
                min = TVs[i].price;
        }
        cout<<"The minimum :" << min <<endl;
        return min;
    }
};

/*---------------------------------------------------*/
int main() {

    Invoice* x[2];

    //customer y;

    for(int i=0; i<2;i++){
        char name[20];
        char filename[20];
        sprintf(name, "Invoice %d", i);
        x[i] = new Invoice(name);
        sprintf(filename, "output%d.txt", i);
        x[i]->Print(filename);
        x[i]->getMin();
        cout<<"********************************"<<endl;
    }

    return 0;
}

جربت كلتا الطريقتين على جهازي و اشتغل بشكل جيد، إذا لم ينجح معك بإمكانك تحديث إصدار c++ بالإضافة إلى الcompiler إلى آخر نُسخة بإمكانك إستخدام برنامج code blocks و تحميل الملف الذي يأتي معه compiler ثم تنصيبه على جهازك.

بالتوفيق.

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

  • 0

مرحباً @هبة حمدان 

سبب عدم تغير القيمة الإفتراضية للإسم وهو أن نمط الوراثة في هذه الحالة protected أي أن الدوال و المتغيرات في الكلاس الأب المُعرفة على أنها public ستُصبح protected في الكلاس الإبن و بالتالي فإن الدالة setName ستُصبح محمية أي أنه لا يُمكنك إستخدامها في الدالة الرئيسية main بإمكانك تمرير الإسم من خلال بناء الكلاس Customer بهذا الشكل:

class Customer{
protected:
    string name;
public:
    Customer(string name = "Unknown"){
        this->name = name;
    }

    string getName(){
        return name;
    }

    void setName(string name = "Unknown"){
        this->name = name;
    }
};

و بالتالي عند إنشاء كائن من الصنف Customer يمكننا تحديد الإسم له، أيضا سنقوم بتوريث البناء للكلاس Invoice و تكون الطريقة كما هي موضحة أدناه:

class Invoice : protected Customer{
  private:
    float Total = 0;
  public:
    TV TVs[2];
  
  Invoice(string name) : Customer(name){
    for(int i=0;i<2;i++){
      Total += TVs[i].price;
    }
  }
}

و في الدالة الرئيسية main يُمكننا عمل التالي:

int main() {

    Invoice* x[2];

    for(int i=0; i<2;i++){
        x[i] = new Invoice("Invoice " + to_string(i)); // قمنا بتحديد الإسم هنا
        x[i]->Print(); 
        x[i]->getMin();
        cout<<"********************************"<<endl;
    }

    return 0;
}

 

بتاريخ 5 ساعات قال هبة حمدان:

وكيف اعمل اعادة التحميل لدالة بتستقبل ملف يا ريت المساعدة .

بخصوص هذا الجزء إعادة التحميل أو overloading تسمح لنا بتعريف دوال تحمل نفس الإسم لكن بمُعاملات مُختلفة لِنقم بإعادة تحميل الدالة Print بهذا الشكل: 

void Print(string file) {
  ofstream f;
  f.open (file);
  f << "The Name is : " << name << endl;
  f << "Total is : " << Total << endl;
  f << "TVs: " << endl;
  for(int i=0;i<2;i++){
    f << "TVs[" << i << "] = (Number = " << TVs[i].getNo() << ", Price = " << TVs[i].getPrice() << ")" << endl;
  }

  f.close();
}

و هنا قمنا بإنشاء ملف بإسم قيمة المُعامل المُمرر و قمنا بملئه بجميع المتغيرات المحلية و الموروثة و في الأخير قمنا بإغلاق الملف.

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

#include <iostream>
#include <fstream>

using namespace std;

/*---------------------------------------------------*/

class Customer{
protected:
    string name;
public:
    Customer(string name = "Unknown"){
        this->name = name;
    }

    string getName(){
        return name;
    }

    void setName(string name = "Unknown"){
        this->name = name;
    }
};

/*---------------------------------------------------*/
class TV{
private:
    int no;
public:
    float price;
    TV(){
        cout << "The TV Number is : " << endl;
        cin >> no;
        cout << "The TV Price is : " << endl;
        cin >> price;
    }

    float getPrice(){
        return price;
    }

    int getNo(){
        return no;
    }

    void setPrice(float p){
        price = p;
    }
};

/*---------------------------------------------------*/
class Invoice : protected Customer{
private:
    float Total = 0;
public:
    TV TVs[2];

    void ReadPrice(){
        for(int i=0;i<2;i++){
            cout << "TVs[" << i << "] = (Number = " << TVs[i].getNo() << ", Price = " << TVs[i].getPrice() << ")" << endl;
        }
    };

    int getTotal(){
        return Total;
    }

    Invoice(string name) : Customer(name){
        for(int i=0;i<2;i++){
            Total += TVs[i].price;
        }
    }

    void Print(){
        cout << "The Name is : " << name << endl;
        cout << "Total is : " << Total << endl;
        cout << "TVs: " << endl;
        this->ReadPrice();
    }

    void Print(string file) {
        ofstream f;
        f.open (file);
        f << "The Name is : " << name << endl;
        f << "Total is : " << Total << endl;
        f << "TVs: " << endl;
        for(int i=0;i<2;i++){
            f << "TVs[" << i << "] = (Number = " << TVs[i].getNo() << ", Price = " << TVs[i].getPrice() << ")" << endl;
        }

        f.close();
    }

    int getMin(){
        float min;
        min = TVs[0].price;
        for(int i=0 ;i < 2; i++){
            if (TVs[i].price < min)
                min = TVs[i].price;
        }
        cout<<"The minimum :" << min <<endl;
        return min;
    }
};

/*---------------------------------------------------*/
int main() {

    Invoice* x[2];

    //customer y;

    for(int i=0; i<2;i++){
        x[i] = new Invoice("Invoice " + to_string(i));
        string filename = "output" + to_string(i) + ".txt";
        x[i]->Print(filename);
        x[i]->getMin();
        cout<<"********************************"<<endl;
    }

    return 0;
}

بإمكانك تجربة المثال من خلال هذا الرابط: هنا
بالتوفيق.

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

  • 0

يسملووو كتيير @عبود سميرعلى المساعدة لكن في عندي نقطة صغيرة الحين انا لما نفذت على الرابط اللي أضفته تنفذ الكود تمام لكن لما نسخت الكود على برنامج Dev C++ ما رضي ينفذ الكود وكتبلي الجملة هادي شو بكون السبب :

‏‏لقطة الشاشة (154).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.

  • إعلانات

  • تابعنا على



×
×
  • أضف...