SlideShare a Scribd company logo
1 of 11
Abstract Types
Containers
Virtual Function
‫جداسازی‬Representation‫از‬Definition
‫جداسازی‬Interface‫از‬Implementation
‫گر‬ ‫اشاره‬ ‫با‬ ‫دسترسی‬ ‫و‬ ‫آزاد‬ ‫ی‬ ‫حافظه‬ ‫در‬ ‫ساختن‬
01. class Container {
02. public:
03. virtual double& operator[](int) = 0;// Pure virtual
04. virtual int size() const = 0;// const member function
05. virtual ~Container() {} // Destructor
06. };
01. void use(Container& c)
02. {
03. const int sz = c.size();
04. for (int i = 0; i != sz; ++i)
05. cout << c[i] << 'n';
06. }
01. class Container {
02. public:
03. virtual double& operator[](int) = 0;// Pure virtual
04. virtual int size() const = 0;// const member function
05. virtual ~Container() {} // Destructor
06. };
01. // Vector_container implements Container
02. class Vector_container : public Container {
03. Vector v;
04. public:
05. Vector_container(int s) : v(s) {}// Vector of s elements
06. ~Vector_container() {}
07.
08. double& operator[](int i) { return v[i]; }
09. int size() const { return v.size(); }
10. };
01. // Vector_container implements Container
02. class Vector_container : public Container {
03. Vector v;
04. public:
05. Vector_container(int s) : v(s) {}// Vector of s elements
06. ~Vector_container() {}
07.
08. double& operator[](int i) { return v[i]; }
09. int size() const { return v.size(); }
10. };
01. void g()
02. {
03. Vector_container vc {10,9,8,7,6,5,4,3,2,1,0};
04. use(vc);
05. }
01. // List_container implements Container
02. class List_container : public Container {
03. std::list<double> ld; // list of doubles
04. public:
05. List_container() {} // Empty list
06. List_container(initilizer_list<double> il) : ld{il} {}
07. ~List_container() {}
08. double& operator[](int i);
09. int size() const { return ld.size(); }
10. };
01. double& List_container:: operator[](int i)
02. {
03. for (auto& x : ld) {
04. if (i == 0) return x;
05. --i;
06. }
07. throw out_of_range("List container");
08. }
01. void h()
02. {
03. List_container lc = { 1,2,3,4,5,6,7,8,9 };
04. use(lc);
05. }
01. void use(Container& c)
02. {
03. const int sz = c.size();
04. for (int i = 0; i != sz; ++i)
05. cout << c[i] << 'n';
06. }
8. abstract types

More Related Content

Similar to 8. abstract types

5. struct, class, enum
5. struct, class, enum5. struct, class, enum
5. struct, class, enumVahid Heidari
 
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptxObject Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptxRashidFaridChishti
 
NHibernate Configuration Patterns
NHibernate Configuration PatternsNHibernate Configuration Patterns
NHibernate Configuration PatternsLuca Milan
 
Module wise format oops questions
Module wise format oops questionsModule wise format oops questions
Module wise format oops questionsSANTOSH RATH
 
Oops lab manual2
Oops lab manual2Oops lab manual2
Oops lab manual2Mouna Guru
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1Teksify
 

Similar to 8. abstract types (8)

5. struct, class, enum
5. struct, class, enum5. struct, class, enum
5. struct, class, enum
 
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptxObject Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
 
NHibernate Configuration Patterns
NHibernate Configuration PatternsNHibernate Configuration Patterns
NHibernate Configuration Patterns
 
Module wise format oops questions
Module wise format oops questionsModule wise format oops questions
Module wise format oops questions
 
OOP.pptx
OOP.pptxOOP.pptx
OOP.pptx
 
Op ps
Op psOp ps
Op ps
 
Oops lab manual2
Oops lab manual2Oops lab manual2
Oops lab manual2
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1
 

More from Vahid Heidari

15. map, unordered map, algorithms
15. map, unordered map, algorithms15. map, unordered map, algorithms
15. map, unordered map, algorithmsVahid Heidari
 
13. string, io streams
13. string, io streams13. string, io streams
13. string, io streamsVahid Heidari
 
12. standard library introduction
12. standard library introduction12. standard library introduction
12. standard library introductionVahid Heidari
 
9. class hierarchies
9. class hierarchies9. class hierarchies
9. class hierarchiesVahid 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
 
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 (12)

18. utilities
18. utilities18. utilities
18. utilities
 
16. smart pointers
16. smart pointers16. smart pointers
16. smart pointers
 
15. map, unordered map, algorithms
15. map, unordered map, algorithms15. map, unordered map, algorithms
15. map, unordered map, algorithms
 
13. string, io streams
13. string, io streams13. string, io streams
13. string, io streams
 
12. standard library introduction
12. standard library introduction12. standard library introduction
12. standard library introduction
 
9. class hierarchies
9. class hierarchies9. class hierarchies
9. class hierarchies
 
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
 
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
 

Recently uploaded

Auto Affiliate AI Earns First Commission in 3 Hours..pdf
Auto Affiliate  AI Earns First Commission in 3 Hours..pdfAuto Affiliate  AI Earns First Commission in 3 Hours..pdf
Auto Affiliate AI Earns First Commission in 3 Hours..pdfSelfMade bd
 
Community is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea GouletCommunity is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea GouletAndrea Goulet
 
BusinessGPT - Security and Governance for Generative AI
BusinessGPT  - Security and Governance for Generative AIBusinessGPT  - Security and Governance for Generative AI
BusinessGPT - Security and Governance for Generative AIAGATSoftware
 
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Lisi Hocke
 
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...drm1699
 
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypseTomasz Kowalczewski
 
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit Milan
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit MilanWorkshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit Milan
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit MilanNeo4j
 
Encryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key ConceptsEncryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key Conceptsthomashtkim
 
Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Maxim Salnikov
 
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale IbridaUNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale IbridaNeo4j
 
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio, Inc.
 
Effective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConEffective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConNatan Silnitsky
 
Evolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI EraEvolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI Eraconfluent
 
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...Flutter Agency
 
The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)Roberto Bettazzoni
 
A Deep Dive into Secure Product Development Frameworks.pdf
A Deep Dive into Secure Product Development Frameworks.pdfA Deep Dive into Secure Product Development Frameworks.pdf
A Deep Dive into Secure Product Development Frameworks.pdfICS
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Clinic
 
Transformer Neural Network Use Cases with Links
Transformer Neural Network Use Cases with LinksTransformer Neural Network Use Cases with Links
Transformer Neural Network Use Cases with LinksJinanKordab
 

Recently uploaded (20)

Auto Affiliate AI Earns First Commission in 3 Hours..pdf
Auto Affiliate  AI Earns First Commission in 3 Hours..pdfAuto Affiliate  AI Earns First Commission in 3 Hours..pdf
Auto Affiliate AI Earns First Commission in 3 Hours..pdf
 
Community is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea GouletCommunity is Just as Important as Code by Andrea Goulet
Community is Just as Important as Code by Andrea Goulet
 
BusinessGPT - Security and Governance for Generative AI
BusinessGPT  - Security and Governance for Generative AIBusinessGPT  - Security and Governance for Generative AI
BusinessGPT - Security and Governance for Generative AI
 
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
 
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
 
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
 
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit Milan
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit MilanWorkshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit Milan
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit Milan
 
Encryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key ConceptsEncryption Recap: A Refresher on Key Concepts
Encryption Recap: A Refresher on Key Concepts
 
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
 
Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?
 
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale IbridaUNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
 
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
 
Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...
Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...
Abortion Pill Prices Germiston ](+27832195400*)[ 🏥 Women's Abortion Clinic in...
 
Effective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeConEffective Strategies for Wix's Scaling challenges - GeeCon
Effective Strategies for Wix's Scaling challenges - GeeCon
 
Evolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI EraEvolving Data Governance for the Real-time Streaming and AI Era
Evolving Data Governance for the Real-time Streaming and AI Era
 
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
 
The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)
 
A Deep Dive into Secure Product Development Frameworks.pdf
A Deep Dive into Secure Product Development Frameworks.pdfA Deep Dive into Secure Product Development Frameworks.pdf
A Deep Dive into Secure Product Development Frameworks.pdf
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
 
Transformer Neural Network Use Cases with Links
Transformer Neural Network Use Cases with LinksTransformer Neural Network Use Cases with Links
Transformer Neural Network Use Cases with Links
 

8. abstract types

  • 1.
  • 4. 01. class Container { 02. public: 03. virtual double& operator[](int) = 0;// Pure virtual 04. virtual int size() const = 0;// const member function 05. virtual ~Container() {} // Destructor 06. };
  • 5. 01. void use(Container& c) 02. { 03. const int sz = c.size(); 04. for (int i = 0; i != sz; ++i) 05. cout << c[i] << 'n'; 06. } 01. class Container { 02. public: 03. virtual double& operator[](int) = 0;// Pure virtual 04. virtual int size() const = 0;// const member function 05. virtual ~Container() {} // Destructor 06. };
  • 6. 01. // Vector_container implements Container 02. class Vector_container : public Container { 03. Vector v; 04. public: 05. Vector_container(int s) : v(s) {}// Vector of s elements 06. ~Vector_container() {} 07. 08. double& operator[](int i) { return v[i]; } 09. int size() const { return v.size(); } 10. };
  • 7. 01. // Vector_container implements Container 02. class Vector_container : public Container { 03. Vector v; 04. public: 05. Vector_container(int s) : v(s) {}// Vector of s elements 06. ~Vector_container() {} 07. 08. double& operator[](int i) { return v[i]; } 09. int size() const { return v.size(); } 10. }; 01. void g() 02. { 03. Vector_container vc {10,9,8,7,6,5,4,3,2,1,0}; 04. use(vc); 05. }
  • 8. 01. // List_container implements Container 02. class List_container : public Container { 03. std::list<double> ld; // list of doubles 04. public: 05. List_container() {} // Empty list 06. List_container(initilizer_list<double> il) : ld{il} {} 07. ~List_container() {} 08. double& operator[](int i); 09. int size() const { return ld.size(); } 10. };
  • 9. 01. double& List_container:: operator[](int i) 02. { 03. for (auto& x : ld) { 04. if (i == 0) return x; 05. --i; 06. } 07. throw out_of_range("List container"); 08. } 01. void h() 02. { 03. List_container lc = { 1,2,3,4,5,6,7,8,9 }; 04. use(lc); 05. }
  • 10. 01. void use(Container& c) 02. { 03. const int sz = c.size(); 04. for (int i = 0; i != sz; ++i) 05. cout << c[i] << 'n'; 06. }

Editor's Notes

  1. در این ویدیو ادامه ی بحث مکانیزم های Abstraction رو پی می گیریم و در مورد Abstract typeها صحبت خواهیم کرد و با استفاده از مفهوم Container چند مثال می بینیم و بعد در مورد پیاده سازی virtual functionها در کامپایلر یه اشاره ای می کنیم.
  2. کلاسهایی مثل complex و vector که در ویدیوی قبلی بررسی کردیم کلاسهای Concrete بودن.کلاسهایی که Representation اونا قسمتی از definition اونا باشه رو concrete میگیم و رفتار اونا شبیه built-in typeها هست. ولی در Abstract typeها بر خلاف concrete typeها نحوه ی نمایش کلاس رو از خود کلاس جدا میکن و به طور کامل کاربر رو از دونستن جزئیات پیاده سازی بی نیاز می کن. برای اینکه ما بتونیم یک Abstract type بسازیم باید Interface رو از Representation کلاس جدا کنیم و به اصطلاح decouple کنیم و تمام memberهای اون رو باید از داخل کلاس خارج کنیم. این کار باعث میشه ما اطلاعاتی از representation کلاس نداشته باشیم و حتی size اون رو هم نمی دونیم. پس باید در حافظه ی free store یا Heap ساخته بشن و از طریق pointer به اونا دسترسی داشته باشیم.
  3. برای مثال ما یک interface برای vector تعریف می کنیم و یک کلاس Container به صورت Abstract تعریف می کنیم. این کلاس یک pure interface هست که برای این نوشته می شه که بگیم که definition اون رو بعدا انجام خواهیم داد. کلمه ی Virtual به معناست که توابعی که جلوش نوشته شده بعدا در کلاسی که از این interface مشتق میشه پیاده سازی خواهد شد. توابعی که اولش virtual میذاریم virtual function گفته میشه. کلاسی که از کلاس Container مشتق میشه باید پیاده سازی مناسب رو برای واسط های Container فراهم کنه در غیر این صورت کامپایلر error خواهد داد. این = 0 که در انتها نوشته شده به این معناست که تابعی که declare شده به صورت pure virtual هست و در کلاس مشتق پیاده سازی خواهد شد. با تعریف کلاس Container شما نمی توندی یک Object از کلاس Container بسازید و فقط یک Interface برای استفاده ارائه میده و کلاس هایی که از Interface مشتق میشن باید اپراتور subscript و size رو پیاده سازی کنند. کلاسی که یک یا چند تا pure virtual function داشته باشد کلاس abstract گفته میشه.
  4. از Container میشه به این صورت استفاده کرد. تابع use از واسطی که برای Container تعریف کردیم استفاده می کنه و بطور کامل از جزئیات پیاده سازی بی اطلاع هست و اصلا خبر نداره که چطور پیاده سازی شده. کلاسی که یک interface برای کلاس های دیگه تعریف می کنه در اصطلاح polymorphic یا چند ریختی گفته میشه. کلاس Container دارای Constructor نیست چون هیچ memberی نداره که مقدار دهی اولیه بکنه. اما باید دارای Destructor از نوع virtual باشه. بخاطر اینکه کلاسهای Abstract همیشه از طریق pointer یا reference مورد استفاده قرار می گیرند و کسی که داره اون Pointer رو Destruct میکنه اطلاعی از resourceهایی که اون پیاده سازی خاص برای خودش Allocate کرده خبر نداره. تابع use میتونه از توابعی که برای کلاس Container تعریف شدن استفاده کنه.
  5. برای اینکه بتونید interface رو پیاده سازی کنید باید یک کلاس Concrete ایجاد کنید که از کلاس pure interface مشتق شده باشه. با نوشتن :public بعد از اسم کلاس مشخص می کنیم که از چه Interfaceیی می خوایم مشتق ایجاد کنیم. در اصطلاح می گیم که کلاس Vector_container از Container مشتق شده یا کلاس یک Subtype از Container هست.یا به Vector_container می گیم Drived class یا subclass و به Containerمیگیم base class یا superclass. کلاس مشتق تمامی memberهای کلاس base رو به ارث می بره و به این موضوع در اصطلاح Inheritance یا ارث بری گفته میشه. نوشتن پیاده سازی برای اپراتور subscript و size در کلاس Drived باعث میشه که اپراتور subscript و size در کلاس Base به اصطلاح Override بشن. Destructor مربوط به Vector_container هم باعث override شدن Destructor در Container میشه و با فراخوانی Destructor کلاس Drived به صورت ضمنی Destructor کلاس base هم فراخوانی خواهد شد.
  6. در پیاده سازی کلاس Vector_container ما از Vectorیی استفاده کردیم که در ویدیوهای قبلی دیده بودیم. سطح دسترسی توی کلاس به صورت پیش فرض private هست پس وکتور v فقط داخل متدهای کلاس قابل دسترسی خواهد بود ولی خط 4 با نوشتن public: بقیه اعضای کلاس رو public تعریف کردیم. برای استفاده از کلاس Vector_container هم می تونیم به راحتی یک instance از Vector بسازیم و به تابع use بدیم. تابع use چون از interface کلاس container اطلاع داره می تونه بدون دونستن اینکه چه پیاده سازی انجام شده از اون استفاده کنه.
  7. حالا بیاید یک پیاده سازی دیگه برای کلاس Container بنویسم که بجای اینکه از Vector استفاده کنه از list استفاده کنه. Representation کلاس List_container رو نسبت به Vector_container عوض می کنیم و از std::list استفاده می کنیم. کلاس List_container دارای default constructor و initialiser-list constructor هست و یه destructor هم داره. توی تابع size هم اندازه ی لیست رو برگردونده. اما پیاده سازی اپراتور subscript رو در اسلاید بعدی با هم می بینیم.
  8. چون ما از std::list استفاده کردیم برای پیاده سازی اپراتور subscript مجبوریم از for استفاده کنیم. همین جا یه نوع جدیدی از For رو هم معرفی می کنیم. به این نوع for می گیم range for که از C++11 اضافه شده و از range for برای پیمایش Containerها استفاده میشه. از کلمه ی کلیدی auto برای این استفاده میشه که type متغییر رو ننویسیم و کامپایلر خودش type مناسب رو پیدا می کنه و جای auto قرار میده. Range for به این صورت عمل می کنه که یک متغییر به نام x تعریف می کنیم که از جنس المانهای داخل Container هست. بعد یه دو نقطه میذاریم و بعد اون Containerیی که می خوایم پیمایش کنیم رو می نویسیم. نحوه ی استفاده از List_container هم مشابه Vector_container هست. با یک initializer-list مقدار اولیه دادیم و بعد به عنوان ورودی به تابع Use داده میشه. و تابع Use هم چون از interface کلاس Container خبر داره می تونه ازش استفاده کنه. تابع use براش فرقی نداره که ورودی اون از نوع list_container باشه یا vector_container باشه فقط باید interface مربوط به Container رو باید پیاده سازی کرده باشه. تا زمانی که ورودی تابع pointer یا reference به یک Interface باشه میشه هر پیاده سازی رو به تابع داد و تابع از روی Interface میتونه از اون ورودی استفاده کنه.
  9. یه بار دیگه تابع use رو با هم ببینیم. سوالی این جا مطرح میشه که تابع Use چطور میتونه تابع درست رو پیدا کنه و اجرا کنه؟ چطور اپراتور subscript مناسب رو پیدا میکنه؟ جواب اینه که برای پیدا کردن virtual function درست، کامپایلر باید یک سری اطلاعات اضافه در مورد Drived کلاس هایی که از Container به ارث بردند رو نگه داری کنه تا با استفاده از اونا در زمان اجرا، تابع درست رو انتخاب کنه و اجرا کنه. یک راه اینه که کامپایلر نام توابع virtual رو به index تبدیل کنه و یک جدول از Pointerها به virtual functionها درست کنه. به این جدول Virtual Table یا به اختصار vtable می گیم. هر کلاسی که توابع virtual داره یک vtable مختص خودش رو داره. به صورت گرافیکی می بینید. کامپایلر با داشتن این جدول می تونه تابع مناسب رو با پیدا کردن Index اون تابع و یافتن function pointer مربوطه پیدا کنه و با دادن پارامترهای ورودی اون تابع رو اجرا کنه. با این مکانیزم تابع use میتونه بدون دونستن اینکه container چه طور پیاده سازی شده، تابع virtual مناسب رو call کنه.
  10. در این ویدیو یه تعریفی از Abstract typeها گفتیم و مفهوم Container رو تعریف کردیم و یه Interface برای Container نوشتیم و 2 تا پیاده سازی مختلف از این interface رو باهم دیدیم. در آخر در مورد virtual function ها و vtable بحث کردیم.