الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Slicing

مغلق
بدأه Super_emad55 في 19 يونيو 2006 · 4 رد · 462 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

بسم الله الرحمن الرحيم,

قد يكون الموضوع واضح للكثير و سهل, ولكن بحكم عملي الجامعي ارى الكثير من الاخطأ في هذا الموضوع.

لنبدأ بتعريف ال slicing تعريف بسيط و سيتم فهم معنى هذا المصطلح تماما بعد الشرح, فهو مصطلح يطلق عندما نقوم بتمرير متغير اتشايلد الى متغير بيرنتس في داله. كما هو واضح في المثال هذا

#include <iostream>
using namespace std;
class father
{
public:
void	print( )
	{
		cout<<"father"<<endl;
	}

};
class child: public father
{
public:
void	print( )
	{
		cout<<"child"<<endl;
	}
};
void printout(father c)
{
	c.print();

}
void main()
{
	child c;
	printout(c);
}

بامانه, ماذا تتوقع ناتج هذا الكود بدون عمل كمبايل له.

عارف انك مش حتكون امين :P , و حتعمله كمبايل وحتجد الناتج father

وطبعا هذا ليس هو المطلووووووب.........

لحل المشكله البسيطه عليك بالاتي

1) كتابت كلمة virtual قبل الداله الموجوده في كلاس البيرنتس.

2) وهذه ينساها الكثير و انا اولهم :D , وهي علامة & قبل المتغير في الداله.

ليصبح برنامجنا بهذا الشكل:

#include <iostream>
using namespace std;
class father
{
public:
virtual void	print()
	{
		cout<<"father"<<endl;
	}

};
class child: public father
{
public:
void	print()
	{
		cout<<"child"<<endl;
	}
};
void printout(father & c)
{
	c.print();

}
void main()
{
	child c;
	printout(c);
}

طبعا الان اعمل كمبايل و حتلاقي انه الجواب هو child, عجيــــــــــــــــــــب صح :)

قد يتسأل البعض ماهو المصطلح virtual, وقد يجيب البعض بانه مصطلح خاص لحل مشكلة ال slicing , وانا اتوافق معهم.

ارجو ان يكون مصطلح slicing واضح الان, و إن شاء الله لا اريد ابدا ان ارى احد من شبابنا يقع في هذه المشكله.

ارجوا ان تعم الفائده ...

تم تعديل هذه المشاركة بواسطة eng_3llam في 19 يونيو 2006 في 12:05

ENG. IMAD, Programing is a way of life, It's in the air, we breath It's pumps through our veines, Without it, we soon crumble to dust

#2

up للإفادة ;-)

Muhammad Allam

Computer Science

@Resource(MappedURL="My Blog" )

#3

slicing؟!

عمري لم اسمع احد يشير لهذه العملية بهذا المصطلح ...

اسمهة polymorphism

اما slicing فهي تعني اقتطاع شريحة او جزء من شيء ما.

#4

اخي hasan_aljudy, هذه المشكله يطلق عليها ال slicing problem

بحكم انك تمرر متغير يحتوي على معلومات اكثر و هو الاتشايلد الى متغير اصغر وهو البيرنتس.

ENG. IMAD, Programing is a way of life, It's in the air, we breath It's pumps through our veines, Without it, we soon crumble to dust

#5

اخي ال Polymorphism هي :

One of the key features of derived classes is that a pointer to a derived class is type-compatible with a pointer to its base class.

اي يعني انها ميزه من مزايا التشايلد كلاس ان مؤشر للتشايلد يتوافق مع مؤشر للبيرنتس.

فلننظر مثلا لهذا البرنامج

#include <iostream>
using namespace std;

class CPolygon {
  protected:
	int width, height;
  public:
	void set_values (int a, int b)
	  { width=a; height=b; }
  };

class CRectangle: public CPolygon {
  public:
	int area ()
	  { return (width * height); }
  };

class CTriangle: public CPolygon {
  public:
	int area ()
	  { return (width * height / 2); }
  };
int main () {
  CRectangle rect;
  CTriangle trgl;
  CPolygon * ppoly1 = &rect;
  CPolygon * ppoly2 = &trgl;
  ppoly1->set_values (4,5);
  ppoly2->set_values (4,5);
  cout << rect.area() << endl;
  cout << trgl.area() << endl;
  return 0;
}

لنجد انه صحيح تماما

فلو اردنا ان نقوم بالاتي

int main () {

  CRectangle rect;

  CPolygon * ppoly1 = new CRectangle;

  ppoly1->set_values (4,5);

  cout << ppoly1->area();

  return 0;
}

لوجدنا ان هناك مشكله

والحل كالتالي

#include <iostream>
using namespace std;

class CPolygon {
  protected:
	int width, height;
  public:
	void set_values (int a, int b)
	  { width=a; height=b; }
	virtual int area ()
	  { return (0); }
  };
class CRectangle: public CPolygon {
  public:
	int area ()
	  { return (width * height); }
  };

class CTriangle: public CPolygon {
  public:
	int area ()
	  { return (width * height / 2); }
  };

int main () {
  CRectangle rect;
  CTriangle trgl;
  CPolygon poly;
  CPolygon * ppoly1 = &rect;
  CPolygon * ppoly2 = &trgl;
  CPolygon * ppoly3 = &poly;
  ppoly1->set_values (4,5);
  ppoly2->set_values (4,5);
  ppoly3->set_values (4,5);
  cout << ppoly1->area() << endl;
  cout << ppoly2->area() << endl;
  cout << ppoly3->area() << endl;
  return 0;
}

واخيرا هذه التقنيه تسمى ال polymorphism

و المشكله بحد ذاتها تسمى ال slicing problem بحكم انك تمرر متغير يحتوي على معلومات اكثر و هو الاتشايلد الى متغير اصغر وهو البيرنتس.

ففي موضوعي انا كنت اعرض المشكله و احلها ولم اكن اشرح هذه الميزه على الاطلاق, لهذا طلبت من المشرف تغيير الموضوع من polymorphism الى slicing problem

ارجو ان لا اكون سببت لخبطه بالموضوع

ENG. IMAD, Programing is a way of life, It's in the air, we breath It's pumps through our veines, Without it, we soon crumble to dust

هذا الموضوع مغلق.

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر ساعة · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…