SlideShare uma empresa Scribd logo
1 de 13
VIRTUAL BASE CLASS
DIAMOND PROBLEM

                 Base Class




     Derived 1                 Derived 2




                   Derived 3
EXAMPLE
class one
{
    public:
         int a;
     public:
              one() { cout<<"onen"; }
};


class two : public one
{
    public:
         int a2;
    public:
              two(){ cout<<"twon"; }
};
class three : public one
{
    public:
         int a3;
     public:
              three(){ cout<<"threen"; }
};

class four: public two , public three
{
    private:
         int a4;
     public:
              four(){ a4=6;cout<<"fourn"; }
};
int main()
{
    four f;
return 0;
}

OUTPUT:
One
              Two copies copies of one present
Two           in an object of type four.
One
Three
Four
int main()
{
  four f;
  cout<<f.a; //error : request for member ‘a’ is ambiguous
  return 0;
}

   Ambiguous because variable „a‟ is present twice in the object
    of four, one in two class and one in three class.


   Because there are two copies of „a‟ present in object „f‟, the
    compiler doesn‟t not know which one is being referred.
HOW TO RESOLVE


Two ways:

   Manual Selection.

   Using virtual base class.
MANUAL SELECTION
   Manually select the required variable ‘a’ by the using
    scope resolution operator.

    int main()
    {
        four f;

       f.two::a=40;                       OUTPUT:

       f.three::a=50;                     40
                                          50
       cout<<f.two::a<<“n”;
       cout<<f.three::a<<“n”;

    return 0;
    }
   f.two::a=40; and f.three::a=50;

   In the above statements, use of scope resolution operators
    resolves the problem of ambiguity.

   But this is not an efficient way because still two copies of
    „a‟ is available in object „f‟.

   How to make only one copy of „a‟ available in the object
    „f‟ which consumes less memory and easy to access without
    using scope resolution operator.

   This can be done by using virtual base class.
VIRTUAL BASE CLASS
 class one                          class two : virtual public one
 {                                  {
     public:                            public:
          int a;                             int a2;
      public:                           public:
      one() {cout<<"onen"; }               two(){ cout<<"twon"; }
 };                                 };


class three:virtual public one      class four: public two , public three
{                                   {
    public:                             private:
         int a3;                             int a4;
     public:                             public:
       three(){cout<<"threen"; }              four(){ a4=6;cout<<"fourn"; }
};                                  };
int main()
{
    four f;
    return 0;
}


OUTPUT:
ONE             Only one copy is maintained.
TWO             No ambiguity.
THREE
FOUR
int main()
{
    four f;
    f.a=40; //    umambigious since only one copy of ‘a’ is present in
                  object ‘f’
    cout<<f.a;
     return 0;
}


   Now that both two and three have inherited base as virtual, any
    multiple inheritance involving them will cause only one copy of base to
    be present.

   Therefore, in FOUR, there is only one copy of base.
THANK YOU

Mais conteúdo relacionado

Mais procurados

Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vishal Patil
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++Shyam Gupta
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaMonika Mishra
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)Ritika Sharma
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsMayank Jain
 
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++Arpita Patel
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
Array of objects.pptx
Array of objects.pptxArray of objects.pptx
Array of objects.pptxRAGAVIC2
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaRahulAnanda1
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVAVINOTH R
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Inline function
Inline functionInline function
Inline functionTech_MX
 

Mais procurados (20)

Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
 
Array of objects.pptx
Array of objects.pptxArray of objects.pptx
Array of objects.pptx
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Inline function
Inline functionInline function
Inline function
 

Destaque

Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examplesgreatqadirgee4u
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application Tech_MX
 
Stack data structure
Stack data structureStack data structure
Stack data structureTech_MX
 
Permutation & Combination
Permutation & CombinationPermutation & Combination
Permutation & CombinationPuru Agrawal
 
Permutations and combinations examples
Permutations and combinations examplesPermutations and combinations examples
Permutations and combinations examplesLeo Crisologo
 
Permutation and combination
Permutation and combinationPermutation and combination
Permutation and combinationSadia Zareen
 
Permutations & Combinations
Permutations & CombinationsPermutations & Combinations
Permutations & Combinationsrfant
 

Destaque (9)

Stack
StackStack
Stack
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
16 combinatroics-2
16 combinatroics-216 combinatroics-2
16 combinatroics-2
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Permutation & Combination
Permutation & CombinationPermutation & Combination
Permutation & Combination
 
Permutations and combinations examples
Permutations and combinations examplesPermutations and combinations examples
Permutations and combinations examples
 
Permutation and combination
Permutation and combinationPermutation and combination
Permutation and combination
 
Permutations & Combinations
Permutations & CombinationsPermutations & Combinations
Permutations & Combinations
 

Semelhante a Virtual base class

Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsPrincess Sam
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdfstudy material
 
Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-iiDeepak Singh
 
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.Abu Saleh
 
MODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerMODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerssuser6f3c8a
 
C# Summer course - Lecture 4
C# Summer course - Lecture 4C# Summer course - Lecture 4
C# Summer course - Lecture 4mohamedsamyali
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and ObjectsPayel Guria
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple InheritanceBhavyaJain137
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...Francesco Casalegno
 
Unit3 java
Unit3 javaUnit3 java
Unit3 javamrecedu
 
Polymorphism and Virtual Functions ppt bioinformatics
Polymorphism and Virtual Functions ppt bioinformaticsPolymorphism and Virtual Functions ppt bioinformatics
Polymorphism and Virtual Functions ppt bioinformaticsPriyanshuMittal31
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphismFALLEE31188
 

Semelhante a Virtual base class (20)

Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functions
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
Inheritance
InheritanceInheritance
Inheritance
 
Chapter26 inheritance-ii
Chapter26 inheritance-iiChapter26 inheritance-ii
Chapter26 inheritance-ii
 
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
 
MODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computerMODULE2_INHERITANCE_SESSION1.ppt computer
MODULE2_INHERITANCE_SESSION1.ppt computer
 
inhertance c++
inhertance c++inhertance c++
inhertance c++
 
C# Summer course - Lecture 4
C# Summer course - Lecture 4C# Summer course - Lecture 4
C# Summer course - Lecture 4
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
 
Lab3
Lab3Lab3
Lab3
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and Objects
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritance
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture4
Lecture4Lecture4
Lecture4
 
Unit3 java
Unit3 javaUnit3 java
Unit3 java
 
Polymorphism and Virtual Functions ppt bioinformatics
Polymorphism and Virtual Functions ppt bioinformaticsPolymorphism and Virtual Functions ppt bioinformatics
Polymorphism and Virtual Functions ppt bioinformatics
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 

Mais de Tech_MX

Theory of estimation
Theory of estimationTheory of estimation
Theory of estimationTech_MX
 
String & its application
String & its applicationString & its application
String & its applicationTech_MX
 
Statistical quality__control_2
Statistical  quality__control_2Statistical  quality__control_2
Statistical quality__control_2Tech_MX
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applicationsTech_MX
 
Set data structure 2
Set data structure 2Set data structure 2
Set data structure 2Tech_MX
 
Set data structure
Set data structure Set data structure
Set data structure Tech_MX
 
Real time Operating System
Real time Operating SystemReal time Operating System
Real time Operating SystemTech_MX
 
Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)Tech_MX
 
Motherboard of a pc
Motherboard of a pcMotherboard of a pc
Motherboard of a pcTech_MX
 
More on Lex
More on LexMore on Lex
More on LexTech_MX
 
MultiMedia dbms
MultiMedia dbmsMultiMedia dbms
MultiMedia dbmsTech_MX
 
Merging files (Data Structure)
Merging files (Data Structure)Merging files (Data Structure)
Merging files (Data Structure)Tech_MX
 
Memory dbms
Memory dbmsMemory dbms
Memory dbmsTech_MX
 
Linear regression
Linear regressionLinear regression
Linear regressionTech_MX
 
Keyboard interrupt
Keyboard interruptKeyboard interrupt
Keyboard interruptTech_MX
 
Introduction to loaders
Introduction to loadersIntroduction to loaders
Introduction to loadersTech_MX
 

Mais de Tech_MX (20)

Uid
UidUid
Uid
 
Theory of estimation
Theory of estimationTheory of estimation
Theory of estimation
 
String & its application
String & its applicationString & its application
String & its application
 
Statistical quality__control_2
Statistical  quality__control_2Statistical  quality__control_2
Statistical quality__control_2
 
Spss
SpssSpss
Spss
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applications
 
Set data structure 2
Set data structure 2Set data structure 2
Set data structure 2
 
Set data structure
Set data structure Set data structure
Set data structure
 
Real time Operating System
Real time Operating SystemReal time Operating System
Real time Operating System
 
Parsing
ParsingParsing
Parsing
 
Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)
 
Motherboard of a pc
Motherboard of a pcMotherboard of a pc
Motherboard of a pc
 
More on Lex
More on LexMore on Lex
More on Lex
 
MultiMedia dbms
MultiMedia dbmsMultiMedia dbms
MultiMedia dbms
 
Merging files (Data Structure)
Merging files (Data Structure)Merging files (Data Structure)
Merging files (Data Structure)
 
Memory dbms
Memory dbmsMemory dbms
Memory dbms
 
Linkers
LinkersLinkers
Linkers
 
Linear regression
Linear regressionLinear regression
Linear regression
 
Keyboard interrupt
Keyboard interruptKeyboard interrupt
Keyboard interrupt
 
Introduction to loaders
Introduction to loadersIntroduction to loaders
Introduction to loaders
 

Último

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 

Último (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 

Virtual base class

  • 2. DIAMOND PROBLEM Base Class Derived 1 Derived 2 Derived 3
  • 3. EXAMPLE class one { public: int a; public: one() { cout<<"onen"; } }; class two : public one { public: int a2; public: two(){ cout<<"twon"; } };
  • 4. class three : public one { public: int a3; public: three(){ cout<<"threen"; } }; class four: public two , public three { private: int a4; public: four(){ a4=6;cout<<"fourn"; } };
  • 5. int main() { four f; return 0; } OUTPUT: One Two copies copies of one present Two in an object of type four. One Three Four
  • 6. int main() { four f; cout<<f.a; //error : request for member ‘a’ is ambiguous return 0; }  Ambiguous because variable „a‟ is present twice in the object of four, one in two class and one in three class.  Because there are two copies of „a‟ present in object „f‟, the compiler doesn‟t not know which one is being referred.
  • 7. HOW TO RESOLVE Two ways:  Manual Selection.  Using virtual base class.
  • 8. MANUAL SELECTION  Manually select the required variable ‘a’ by the using scope resolution operator. int main() { four f; f.two::a=40; OUTPUT: f.three::a=50; 40 50 cout<<f.two::a<<“n”; cout<<f.three::a<<“n”; return 0; }
  • 9. f.two::a=40; and f.three::a=50;  In the above statements, use of scope resolution operators resolves the problem of ambiguity.  But this is not an efficient way because still two copies of „a‟ is available in object „f‟.  How to make only one copy of „a‟ available in the object „f‟ which consumes less memory and easy to access without using scope resolution operator.  This can be done by using virtual base class.
  • 10. VIRTUAL BASE CLASS class one class two : virtual public one { { public: public: int a; int a2; public: public: one() {cout<<"onen"; } two(){ cout<<"twon"; } }; }; class three:virtual public one class four: public two , public three { { public: private: int a3; int a4; public: public: three(){cout<<"threen"; } four(){ a4=6;cout<<"fourn"; } }; };
  • 11. int main() { four f; return 0; } OUTPUT: ONE Only one copy is maintained. TWO No ambiguity. THREE FOUR
  • 12. int main() { four f; f.a=40; // umambigious since only one copy of ‘a’ is present in object ‘f’ cout<<f.a; return 0; }  Now that both two and three have inherited base as virtual, any multiple inheritance involving them will cause only one copy of base to be present.  Therefore, in FOUR, there is only one copy of base.