SlideShare a Scribd company logo
1 of 9
‫ها‬ ‫کالس‬ ‫مراتب‬ ‫سلسله‬
‫کالس‬ ‫مراتب‬ ‫سلسله‬ ‫از‬ ‫استفاده‬ ‫فواید‬‫ها‬
01. class Shape {
02. public:
03. virtual Point center() const = 0;// Pure virtual
04. virtual void move(Point to) = 0;
05. virtual void draw() const = 0;// Draw on current "Canvas"
06. virtual void rotate(int angle) = 0;
07.
08. virtual ~Shape() {}// Destructor
09. // ...
10. };
01. // Rotate v's elements by angle degrees
02. void rotate_all(vector<Shape*>& v, int angle)
03. {
04. for (auto p : v)
05. p->rotate(angle);
06. }
01. class Circle : public Shape {
02. public:
03. Circle(Point p, int rr); // Constructor
04. Point center() const { return x; }
05. void move(Point to) { x = to; }
06. void draw() const;
07. void rotate(int) {} // Nice simple algorithm
08. private:
09. Point x; // Center
10. int r; // Radius
11. };
01. // Use the circle as the base for a face
02. class Smiley : public Circle {
03. public:
04. Smiley(Point p, int r) : Circle(p, r), mouth(nullptr) {}
05. ~Smiley() {
06. delete mouth;
07. for (auto p : eyes) delete p;
08. }
09. void move(Point to);
10. void draw() const;
11. void rotate(int);
12. void add_eye(Shape* s) { eyes.push_back(s); }
13. void set_mouth(Shape* s);
14. virtual void wink(int i); // Wink eye number i
15. // ...
16. private:
17. vector<Shape*> eyes; // Usually two eyes
18. Shape* mouth;
19. };
01. void Smiley::draw()
02. {
03. Circle::draw();
04. for (auto p : eyes)
05. p->draw();
06. mouth->draw();
07. }
‫رسیدن‬ ‫ارث‬ ‫به‬Interface‫ها‬
‫تر‬ ‫راحت‬ ‫سازی‬ ‫پیاده‬
9. class hierarchies

More Related Content

More from Vahid Heidari

7. abstraction mechanisms, containers
7. abstraction mechanisms, containers7. abstraction mechanisms, containers
7. abstraction mechanisms, containersVahid Heidari
 
6. separation, namespace, error
6. separation, namespace, error6. separation, namespace, error
6. separation, namespace, errorVahid Heidari
 
5. struct, class, enum
5. struct, class, enum5. struct, class, enum
5. struct, class, enumVahid Heidari
 
2. types, vars, arith, consts
2. types, vars, arith, consts2. types, vars, arith, consts
2. types, vars, arith, constsVahid Heidari
 
1. preface, hello world
1. preface, hello world1. preface, hello world
1. preface, hello worldVahid Heidari
 

More from Vahid Heidari (9)

10. copy and move
10. copy and move10. copy and move
10. copy and move
 
8. abstract types
8. abstract types8. abstract types
8. abstract types
 
7. abstraction mechanisms, containers
7. abstraction mechanisms, containers7. abstraction mechanisms, containers
7. abstraction mechanisms, containers
 
6. separation, namespace, error
6. separation, namespace, error6. separation, namespace, error
6. separation, namespace, error
 
5. struct, class, enum
5. struct, class, enum5. struct, class, enum
5. struct, class, enum
 
4. pointers, arrays
4. pointers, arrays4. pointers, arrays
4. pointers, arrays
 
3. tests, loops
3. tests, loops3. tests, loops
3. tests, loops
 
2. types, vars, arith, consts
2. types, vars, arith, consts2. types, vars, arith, consts
2. types, vars, arith, consts
 
1. preface, hello world
1. preface, hello world1. preface, hello world
1. preface, hello world
 

9. class hierarchies

  • 1.
  • 2. ‫ها‬ ‫کالس‬ ‫مراتب‬ ‫سلسله‬ ‫کالس‬ ‫مراتب‬ ‫سلسله‬ ‫از‬ ‫استفاده‬ ‫فواید‬‫ها‬
  • 3. 01. class Shape { 02. public: 03. virtual Point center() const = 0;// Pure virtual 04. virtual void move(Point to) = 0; 05. virtual void draw() const = 0;// Draw on current "Canvas" 06. virtual void rotate(int angle) = 0; 07. 08. virtual ~Shape() {}// Destructor 09. // ... 10. };
  • 4. 01. // Rotate v's elements by angle degrees 02. void rotate_all(vector<Shape*>& v, int angle) 03. { 04. for (auto p : v) 05. p->rotate(angle); 06. }
  • 5. 01. class Circle : public Shape { 02. public: 03. Circle(Point p, int rr); // Constructor 04. Point center() const { return x; } 05. void move(Point to) { x = to; } 06. void draw() const; 07. void rotate(int) {} // Nice simple algorithm 08. private: 09. Point x; // Center 10. int r; // Radius 11. };
  • 6. 01. // Use the circle as the base for a face 02. class Smiley : public Circle { 03. public: 04. Smiley(Point p, int r) : Circle(p, r), mouth(nullptr) {} 05. ~Smiley() { 06. delete mouth; 07. for (auto p : eyes) delete p; 08. } 09. void move(Point to); 10. void draw() const; 11. void rotate(int); 12. void add_eye(Shape* s) { eyes.push_back(s); } 13. void set_mouth(Shape* s); 14. virtual void wink(int i); // Wink eye number i 15. // ... 16. private: 17. vector<Shape*> eyes; // Usually two eyes 18. Shape* mouth; 19. };
  • 7. 01. void Smiley::draw() 02. { 03. Circle::draw(); 04. for (auto p : eyes) 05. p->draw(); 06. mouth->draw(); 07. }

Editor's Notes

  1. مثال Container که در ویدیوی قبلی بررسی کردیم یک مثال ساده از calss hierarchie بود و فقط یک کلاس base داشتیم و یک کلاس drived ولی میتونیم هر تعداد که بخوایم این سلسله مراتب رو بیشتر کنیم و مثلا از کلاس drived هم یه کلاس دیگه مشتق بگیریم. سلسله مراتب کلاسها با derivation و یا اشتقاق کلاسها از base ایجاد می شه و این سلسله مراتب یکسری relation یا روابط بین کلاسها بوجود میاره. توی این دیاگرام می بینید که smiley یا صورتک خندان یک نوع circle یا دایره هست و دایره هم یک نوع shape و یا شکل هست. فلش ها روابط وراثت رو نشون می دند. برای مثال circle از shape مشتق شده. برای اینکه این دیاگرام رو به صورت کد در بیاریم باید اول کلاسی رو که خواص عمومی مربوط به تمامی Shapeها رو داره بنویسیم. مثلا یه تابع به نام center داریم مرکز شکل رو میده. تابع move یه نقطه به عنوان ورودی می گیره و شکل رو انتقال میده به نقطه جدید. تابع draw شکل رو روی صفحه می کشه و تابع rotate شکل رو چرخش میده. برای تمامی شکل ها این تابع ها مشترک هست ولی پیاده سازی اونا برای هر شکل متفاوته. چون این کلاس دارای pure virtual function هست پس یک کلاس abstract هست و تا زمانی که Representation کلاس مشتق شده از اون کاملا مشخص نشده هیچ چیزی بجز اشاره گر vtable مشخص نمی شه.
  2. اما با داشتن definition مربوط به Shape می تونیم یکسری کارهای عام و عمومی رو با استفاده از اشاره گر ها انجام بدیم. می تونیم یه تابع بنویسیم که یه وکتوری از shapeها و یه زاویه رو به عنوان ورودی میگیره می گیره. و تمام shapeهای داخل vector رو چرخش بده.
  3. برای مشخص کردن یک نوع خاصی از Shape ما اول باید با :public Shape بگیم که این type جدید از نوع Shape مشتق شده و یکسری از Propertyهای خاص اون و همین طور Virtual functionها رو باز تعریف می کنیم. تا اینجا نسبت به Container و Vector_container تفاوتی نداره. ولی ما می تونیم جلو تر بریم.
  4. توی این مثال کلاس Smiley رو می تونیم از کلاس circle مشتق بگیریم برای این کار با :public circle مشخص می کنیم که می خوایم از circle مشتق بسازیم. با استفاده از Vector که در کتابخونه استاندارد وجود داره eyes رو نگه می داره. کتابخونه استاندارد یک vector که خیلی پیشرفته تر از اون vectorیی که ما به عنوان مثال بررسی کردیم به ما میده. مثلا المانهای داخل اون لازم نیست که فقط float باشن و می تونه هر typeیی باشه برای مشخص کردن اینکه چه typeیی رو می تونیم توی vector نگه داریم داخل علامت کوچکتر و بزرگتر اونو مشخص می کنیم. در آینده به طور کامل این مورد رو بررسی خواهیم کرد. تابع add_eye آرگمان ورودیش رو میگره و با push_back به vector اضافه می کنه و size رو یکی اضافه می کنه.
  5. برای کشیدن Smiley ما به این صورت عمل میکنیم. توی این خط با نوشتن Circle::draw میگیم که تابع draw از کلاس Circle که Base هست فراخوانی بشه. پس ما می تونیم به این صورت با نوشتن اسم کلاس base و بعد :: توابع کلاس base رو فراخوانی کنیم. با با استفاده از range for شکل های موجود در eyes رو میکشیم و در آخر هم mouth رو می کشیم.
  6. با کلاس مشتق شده می شه هر زمان که نیاز باشه به صورت یک Object از نوع base رفتار کرد به این صورت که کلاس base مثل یک interface برای کلاس derived رفتار می کنه. به این شکل که interfaceهای کلاس base به کلاس derived به ارث میرسه. کلاس base برای کلاس derived یکسری function و data فراهم میکنه که کار پیاده سازی رو برای کلاس derived ساده تر میکنه. مثلا در مثال قبلی دیدیم که چطور می تونستیم در کلاس Smiley از متد های Draw که از Circle به ارث رسیده بود برای راحتر کردن کار ها استفاده کرد.
  7. در این ویدیو در مورد سلسله مراتب کلاسها صحبت کردیم و یک مثال ساده رو بررسی کردیم و نحوه ی پیاده سازی توابع virtual رو در hierarchy کلاسها با هم دیدیم.