المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : ما هو الخطأ في هذا الكود



IGI2000
23-11-2005, 10:50 PM
السلام عليكم

شباب شوفوا هذا الكود وقولولي على الخطأ اللي في
اناعملت البرنامج بس اريده لما يستدعي العضو draw()l يستدعيه حسب ماهو مطلوب بس ما عرفت ايش الخطا في

#include <iostream.h>
class shape
{

public:
shape(){}
virtual ~shape(){}
virtual void draw(){cout<<"You hade not choice the shape "<<endl;}
};
class rectangle:public shape
{
int itslength;
int itswidth;
public:
rectangle(int len,int wid):itslength(len),itswidth(wid){}
virtual ~rectangle(){}
virtual void draw();
};
void rectangle::draw()
{
for(int i=0;i<itslength;i++)
{
for(int j=0;j<itswidth;j++)
cout<<"*";
cout<<endl;
}
}
class square:public rectangle
{
int itslength;
int itswidth;
public:
square(int len,int wid):rectangle(len,wid){}
~square(){}
void draw();
};
void square::draw()
{
for(int i=0;i<itslength;i++)
{
for(int j=0;j<itslength;j++)
cout<<"*";
cout<<endl;
}
}
void main()
{
int len,wid;
cout<<"Enter the length of the shape you want to draw :";
cin>>len;
cout<<"Enter the width of the shape you want to draw :";
cin>>wid;
shape *draw;
int i;
char choice;
for(i=0;i<1;i++)
{
cout<<"1.Rectangle ."<<endl;
cout<<"2.square ."<<endl;
cout<<"Please choose the shape you want to draw :";
cin>>choice;
switch(choice)
{
case '1':draw=new rectangle(len,wid);
case '2':draw=new square(len,wid);
default:draw=new shape();
}
}
for(i=0;i<1;i++)
{
draw->draw();
}
}

اعرف ان الخطأ في ال switch الاخيرة بس ايش لازم اسوي علشان لما استدعي ال draw()l يستدعي الصحيحة

وشكرا

The Coder
24-11-2005, 06:27 AM
السلام عليكم

شباب شوفوا هذا الكود وقولولي على الخطأ اللي في
اناعملت البرنامج بس اريده لما يستدعي العضو draw()l يستدعيه حسب ماهو مطلوب بس ما عرفت ايش الخطا في

#include <iostream.h>
class shape
{

public:
shape(){}
virtual ~shape(){}
virtual void draw(){cout<<"You hade not choice the shape "<<endl;}
};
class rectangle:public shape
{
int itslength;
int itswidth;
public:
rectangle(int len,int wid):itslength(len),itswidth(wid){}
virtual ~rectangle(){}
virtual void draw();
};
void rectangle::draw()
{
for(int i=0;i<itslength;i++)
{
for(int j=0;j<itswidth;j++)
cout<<"*";
cout<<endl;
}
}
class square:public rectangle
{
int itslength;
int itswidth;
public:
square(int len,int wid):rectangle(len,wid){}
~square(){}
void draw();
};
void square::draw()
{
for(int i=0;i<itslength;i++)
{
for(int j=0;j<itslength;j++)
cout<<"*";
cout<<endl;
}
}
void main()
{
int len,wid;
cout<<"Enter the length of the shape you want to draw :";
cin>>len;
cout<<"Enter the width of the shape you want to draw :";
cin>>wid;
shape *draw;
int i;
char choice;
for(i=0;i<1;i++)
{
cout<<"1.Rectangle ."<<endl;
cout<<"2.square ."<<endl;
cout<<"Please choose the shape you want to draw :";
cin>>choice;
switch(choice)
{
case '1':draw=new rectangle(len,wid);
case '2':draw=new square(len,wid);
default:draw=new shape();
}
}
for(i=0;i<1;i++)
{
draw->draw();
}
}

اعرف ان الخطأ في ال switch الاخيرة بس ايش لازم اسوي علشان لما استدعي ال draw()l يستدعي الصحيحة

وشكرا

ولو انني مشغول جدا هذه اليومين ..
ولكن ...
سوف احاول مساعدتك ...
اعذرني ... لا استطيع الان ..
ولكن ... من الممكن ان اساعدك لو شرحت لي اش البرنامج .... << كسول لا يريد ان يقرأ البرنامج اصلا !!

IGI2000
24-11-2005, 01:08 PM
السلام عليكم
اخي جافا
البرنامج وكل ما فيه انا يرسم مستطيل او مربع(حسب اختيارالمستخدم)
انا ما يهمني ايش راح يرسم
المهم عندي ( تعدد الاوجه ) يعني اريد المترجم يفرق بين هذه الدالة والدالة الاخرى مع العلم ان لها نفس الاسم
كيف العمل !!!!
ما عرفت كيف لكن ان شاء الله اجد الاجابة
هذي فكرة بسيطة عن البرنامج والباقي عليك

اخوك زيد

ib_doom
24-11-2005, 08:59 PM
السلام عليكم
أعتقد أن المشكل موجود في square class و ذلك لأنك لم تقم بإعطاء قيم أولية لل itswidth, itslength
لذلك فإن draw تستخدم متغيرات عشوائية. (أعطها قيم len, wid في constructor)
لا تنسى وضع break instruction بين case حتى يتم الخروج بعد الإختيار.

IGI2000
24-11-2005, 10:30 PM
السلام عليكم
شكرا لك اخي ib_doom
انا قد حللت المشكلة بنفسي والحمد لله
صح يمكن تكون المشكلة في ال square class
انا قد غيرت الاعضاء الخاصة إلى محمية protected ووهكذا تنحل المشكلة
وايضا ال switch الاخيرة نسيت ان اكتب البريك break
سبحان الله حللت المشكلة قبل ان ادخل إلى الانترنت ب دقيقتين
وها هو الكود في حلته الجديدة والمنقحة

#include <iostream.h>
class shape
{

public:
shape(){}
virtual ~shape(){}
virtual void draw(){cout<<"You hade not choice the shape "<<endl;}
};
class rectangle:public shape
{
protected:
int itslength;
int itswidth;
public:
rectangle(int len,int wid):itslength(len),itswidth(wid){}
virtual ~rectangle(){}
virtual void draw();
};
void rectangle::draw()
{
for(int i=0;i<itslength;i++)
{
for(int j=0;j<itswidth;j++)
cout<<"*";
cout<<endl;
}
}
class square:public rectangle
{
public:
square(int len,int wid):rectangle(len,wid){}
~square(){}
void draw();
};
void square::draw()
{
cout<<"The square will be make from the first value. "<<endl;
for(int i=0;i<itslength;i++)
{
for(int j=0;j<itslength;j++)
cout<<"*";
cout<<endl;
}
}
void main()
{
int len,wid;

shape *draw;
int i;
int choice;
char quit;
do{
cout<<"Enter the length of the shape you want to draw :";
cin>>len;
cout<<"Enter the width of the shape you want to draw :";
cin>>wid;
for(i=0;i<1;i++)
{
cout<<"1.Rectangle ."<<endl;
cout<<"2.square ."<<endl;
cout<<"Please choose the shape you want to draw :";
cin>>choice;
switch(choice)
{
case 1:draw=new rectangle(len,wid);break;
case 2:draw=new square(len,wid);break;
default:draw=new shape();break;
}
}
for(i=0;i<1;i++)
{
draw->draw();
}
cout<<"If you want to quit press (q) :";
cin>>quit;

}while(quit!='q');
}


وشكرا لكم