SlideShare une entreprise Scribd logo
1  sur  5
Illustration of the use of virtual inheritance.
  //
   // Virtual inheritance
   #include <iostream.h>

   typedef int HANDS;
  enum COLOR { Red, Green, Blue, Yellow, White, Black, Brown } ;
   enum BOOL { FALSE, TRUE };

   class Animal        // common base to both horse and bird
  {
  public:
     Animal(int);
     virtual ~Animal() { cout << "Animal destructor...n"; }
     virtual int GetAge() const { return itsAge; }
     virtual void SetAge(int age) { itsAge = age; }
  private:
     int itsAge;
  };

  Animal::Animal(int age):
  itsAge(age)
  {
     cout << "Animal constructor...n";
  }

  class Horse : virtual public Animal
  {
  public:
     Horse(COLOR color, HANDS height, int age);
     virtual ~Horse() { cout << "Horse destructor...n"; }
     virtual void Whinny()const { cout << "Whinny!... "; }
     virtual HANDS GetHeight() const { return itsHeight; }
     virtual COLOR GetColor() const { return itsColor; }
  protected:
     HANDS itsHeight;
     COLOR itsColor;
  };

  Horse::Horse(COLOR color, HANDS height, int age):
     Animal(age),
     itsColor(color),itsHeight(height)
  {
     cout << "Horse constructor...n";
  }

  class Bird : virtual public Animal
  {
  public:
     Bird(COLOR color, BOOL migrates, int age);
virtual ~Bird() {cout << "Bird destructor...n"; }
   virtual void Chirp()const { cout << "Chirp... "; }
   virtual void Fly()const
      { cout << "I can fly! I can fly! I can fly! "; }
   virtual COLOR GetColor()const { return itsColor; }
   virtual BOOL GetMigration() const { return itsMigration; }
protected:
   COLOR itsColor;
   BOOL itsMigration;
};

Bird::Bird(COLOR color, BOOL migrates, int age):
   Animal(age),
   itsColor(color), itsMigration(migrates)
{
   cout << "Bird constructor...n";
}

class Pegasus : public Horse, public Bird
{
public:
   void Chirp()const { Whinny(); }
   Pegasus(COLOR, HANDS, BOOL, long, int);
   ~Pegasus() {cout << "Pegasus destructor...n";}
   virtual long GetNumberBelievers() const
      { return itsNumberBelievers; }
   virtual COLOR GetColor()const { return Horse::itsColor; }
private:
   long itsNumberBelievers;
};

Pegasus::Pegasus(
   COLOR aColor,
   HANDS height,
   BOOL migrates,
   long NumBelieve,
   int age):
Horse(aColor, height,age),
Bird(aColor, migrates,age),
Animal(age*2),
itsNumberBelievers(NumBelieve)
{
  cout << "Pegasus constructor...n";
}

int main()
{
   Pegasus *pPeg = new Pegasus(Red, 5, TRUE, 10, 2);
   int age = pPeg->GetAge();
   cout << "This pegasus is " << age << " years old.n";
delete pPeg;
     return 0;
 }

/ virtual members
#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;
}
#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 (void) =0;
  };

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

class CTriangle: public CPolygon {
  public:
     int area (void)
       { 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 << ppoly1->area() << endl;
  cout << ppoly2->area() << endl;
  return 0;
}
// pure virtual members can be called
// from the abstract 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; }
     virtual int area (void) =0;
     void printarea (void)
       { cout << this->area() << endl; }
  };

class CRectangle: public CPolygon {
  public:
     int area (void)
       { return (width * height); }
  };
class CTriangle: public CPolygon {
  public:
     int area (void)
       { 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);
  ppoly1->printarea();
  ppoly2->printarea();
  return 0;
}

Contenu connexe

Tendances

Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to PerlSway Wang
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd StudyChris Ohk
 
Introduction to go
Introduction to goIntroduction to go
Introduction to goJaehue Jang
 
Introduction to Swift programming language.
Introduction to Swift programming language.Introduction to Swift programming language.
Introduction to Swift programming language.Icalia Labs
 
F# delight
F# delightF# delight
F# delightpriort
 
Ruby on rails tips
Ruby  on rails tipsRuby  on rails tips
Ruby on rails tipsBinBin He
 
Spotify at PyCon Finland 2011
Spotify at PyCon Finland 2011Spotify at PyCon Finland 2011
Spotify at PyCon Finland 2011Tommie Gannert
 
Let's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APILet's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APIMario Fusco
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2goMoriyoshi Koizumi
 
Unmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeUnmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeDmitri Nesteruk
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th StudyChris Ohk
 
Hacking Go Compiler Internals / GoCon 2014 Autumn
Hacking Go Compiler Internals / GoCon 2014 AutumnHacking Go Compiler Internals / GoCon 2014 Autumn
Hacking Go Compiler Internals / GoCon 2014 AutumnMoriyoshi Koizumi
 
Android 101 - Building a simple app with Kotlin in 90 minutes
Android 101 - Building a simple app with Kotlin in 90 minutesAndroid 101 - Building a simple app with Kotlin in 90 minutes
Android 101 - Building a simple app with Kotlin in 90 minutesKai Koenig
 
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologiesit-people
 
D vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoyaD vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoyaN Masahiro
 

Tendances (20)

Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 
Introduction to go
Introduction to goIntroduction to go
Introduction to go
 
Introduction to Swift programming language.
Introduction to Swift programming language.Introduction to Swift programming language.
Introduction to Swift programming language.
 
F# delight
F# delightF# delight
F# delight
 
String
StringString
String
 
C++ via C#
C++ via C#C++ via C#
C++ via C#
 
Ruby on rails tips
Ruby  on rails tipsRuby  on rails tips
Ruby on rails tips
 
Spotify at PyCon Finland 2011
Spotify at PyCon Finland 2011Spotify at PyCon Finland 2011
Spotify at PyCon Finland 2011
 
Let's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APILet's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java API
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
 
FPBrno 2018-05-22: Benchmarking in elixir
FPBrno 2018-05-22: Benchmarking in elixirFPBrno 2018-05-22: Benchmarking in elixir
FPBrno 2018-05-22: Benchmarking in elixir
 
Unmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeUnmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/Invoke
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
Hacking Go Compiler Internals / GoCon 2014 Autumn
Hacking Go Compiler Internals / GoCon 2014 AutumnHacking Go Compiler Internals / GoCon 2014 Autumn
Hacking Go Compiler Internals / GoCon 2014 Autumn
 
Android 101 - Building a simple app with Kotlin in 90 minutes
Android 101 - Building a simple app with Kotlin in 90 minutesAndroid 101 - Building a simple app with Kotlin in 90 minutes
Android 101 - Building a simple app with Kotlin in 90 minutes
 
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
 
D vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoyaD vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoya
 
Jan 2012 HUG: RHadoop
Jan 2012 HUG: RHadoopJan 2012 HUG: RHadoop
Jan 2012 HUG: RHadoop
 
Led with raspberry pi
Led with raspberry piLed with raspberry pi
Led with raspberry pi
 

En vedette

0128 1604 10mseminar[1]
0128 1604 10mseminar[1]0128 1604 10mseminar[1]
0128 1604 10mseminar[1]Shun Nokubo
 
Portfolio Karel Peelman 06/2011
Portfolio Karel Peelman 06/2011Portfolio Karel Peelman 06/2011
Portfolio Karel Peelman 06/2011karelpeelman
 
Natural way to_strengthen_immune_system_final[1]
Natural way to_strengthen_immune_system_final[1]Natural way to_strengthen_immune_system_final[1]
Natural way to_strengthen_immune_system_final[1]advacu
 
Natural way to_strengthen_immune_system_final[1]
Natural way to_strengthen_immune_system_final[1]Natural way to_strengthen_immune_system_final[1]
Natural way to_strengthen_immune_system_final[1]advacu
 
Natural weight management
Natural weight managementNatural weight management
Natural weight managementadvacu
 
Wintegration - Strategieworkshop für Großgruppen
Wintegration - Strategieworkshop für GroßgruppenWintegration - Strategieworkshop für Großgruppen
Wintegration - Strategieworkshop für GroßgruppenMarko Willnecker
 

En vedette (7)

0128 1604 10mseminar[1]
0128 1604 10mseminar[1]0128 1604 10mseminar[1]
0128 1604 10mseminar[1]
 
Portfolio Karel Peelman 06/2011
Portfolio Karel Peelman 06/2011Portfolio Karel Peelman 06/2011
Portfolio Karel Peelman 06/2011
 
Natural way to_strengthen_immune_system_final[1]
Natural way to_strengthen_immune_system_final[1]Natural way to_strengthen_immune_system_final[1]
Natural way to_strengthen_immune_system_final[1]
 
1129 5m seminar
1129 5m seminar1129 5m seminar
1129 5m seminar
 
Natural way to_strengthen_immune_system_final[1]
Natural way to_strengthen_immune_system_final[1]Natural way to_strengthen_immune_system_final[1]
Natural way to_strengthen_immune_system_final[1]
 
Natural weight management
Natural weight managementNatural weight management
Natural weight management
 
Wintegration - Strategieworkshop für Großgruppen
Wintegration - Strategieworkshop für GroßgruppenWintegration - Strategieworkshop für Großgruppen
Wintegration - Strategieworkshop für Großgruppen
 

Similaire à Virtual inheritance

Inheritance compiler support
Inheritance compiler supportInheritance compiler support
Inheritance compiler supportSyed Zaid Irshad
 
Web2Day 2017 - Concilier DomainDriveDesign et API REST
Web2Day 2017 - Concilier DomainDriveDesign et API RESTWeb2Day 2017 - Concilier DomainDriveDesign et API REST
Web2Day 2017 - Concilier DomainDriveDesign et API RESTNicolas Faugout
 
Game unleashedjavascript
Game unleashedjavascriptGame unleashedjavascript
Game unleashedjavascriptReece Carlson
 
import javautilLinkedList import javautilQueue import .pdf
import javautilLinkedList import javautilQueue import .pdfimport javautilLinkedList import javautilQueue import .pdf
import javautilLinkedList import javautilQueue import .pdfADITIEYEWEAR
 
Better Software: introduction to good code
Better Software: introduction to good codeBetter Software: introduction to good code
Better Software: introduction to good codeGiordano Scalzo
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Ismar Silveira
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual finalAhalyaR
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxssuser3cbb4c
 
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsAlfonso Peletier
 
project report in C++ programming and SQL
project report in C++ programming and SQLproject report in C++ programming and SQL
project report in C++ programming and SQLvikram mahendra
 
Java 5 New Feature
Java 5 New FeatureJava 5 New Feature
Java 5 New Featurexcoda
 

Similaire à Virtual inheritance (20)

Opp compile
Opp compileOpp compile
Opp compile
 
Inheritance compiler support
Inheritance compiler supportInheritance compiler support
Inheritance compiler support
 
Sbaw091006
Sbaw091006Sbaw091006
Sbaw091006
 
Web2Day 2017 - Concilier DomainDriveDesign et API REST
Web2Day 2017 - Concilier DomainDriveDesign et API RESTWeb2Day 2017 - Concilier DomainDriveDesign et API REST
Web2Day 2017 - Concilier DomainDriveDesign et API REST
 
Game unleashedjavascript
Game unleashedjavascriptGame unleashedjavascript
Game unleashedjavascript
 
import javautilLinkedList import javautilQueue import .pdf
import javautilLinkedList import javautilQueue import .pdfimport javautilLinkedList import javautilQueue import .pdf
import javautilLinkedList import javautilQueue import .pdf
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Better Software: introduction to good code
Better Software: introduction to good codeBetter Software: introduction to good code
Better Software: introduction to good code
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
C++ programs
C++ programsC++ programs
C++ programs
 
Introduzione a C#
Introduzione a C#Introduzione a C#
Introduzione a C#
 
DotNet Conference: code smells
DotNet Conference: code smellsDotNet Conference: code smells
DotNet Conference: code smells
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
 
oodp elab.pdf
oodp elab.pdfoodp elab.pdf
oodp elab.pdf
 
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture4
Lecture4Lecture4
Lecture4
 
project report in C++ programming and SQL
project report in C++ programming and SQLproject report in C++ programming and SQL
project report in C++ programming and SQL
 
Java 5 New Feature
Java 5 New FeatureJava 5 New Feature
Java 5 New Feature
 

Virtual inheritance

  • 1. Illustration of the use of virtual inheritance. // // Virtual inheritance #include <iostream.h> typedef int HANDS; enum COLOR { Red, Green, Blue, Yellow, White, Black, Brown } ; enum BOOL { FALSE, TRUE }; class Animal // common base to both horse and bird { public: Animal(int); virtual ~Animal() { cout << "Animal destructor...n"; } virtual int GetAge() const { return itsAge; } virtual void SetAge(int age) { itsAge = age; } private: int itsAge; }; Animal::Animal(int age): itsAge(age) { cout << "Animal constructor...n"; } class Horse : virtual public Animal { public: Horse(COLOR color, HANDS height, int age); virtual ~Horse() { cout << "Horse destructor...n"; } virtual void Whinny()const { cout << "Whinny!... "; } virtual HANDS GetHeight() const { return itsHeight; } virtual COLOR GetColor() const { return itsColor; } protected: HANDS itsHeight; COLOR itsColor; }; Horse::Horse(COLOR color, HANDS height, int age): Animal(age), itsColor(color),itsHeight(height) { cout << "Horse constructor...n"; } class Bird : virtual public Animal { public: Bird(COLOR color, BOOL migrates, int age);
  • 2. virtual ~Bird() {cout << "Bird destructor...n"; } virtual void Chirp()const { cout << "Chirp... "; } virtual void Fly()const { cout << "I can fly! I can fly! I can fly! "; } virtual COLOR GetColor()const { return itsColor; } virtual BOOL GetMigration() const { return itsMigration; } protected: COLOR itsColor; BOOL itsMigration; }; Bird::Bird(COLOR color, BOOL migrates, int age): Animal(age), itsColor(color), itsMigration(migrates) { cout << "Bird constructor...n"; } class Pegasus : public Horse, public Bird { public: void Chirp()const { Whinny(); } Pegasus(COLOR, HANDS, BOOL, long, int); ~Pegasus() {cout << "Pegasus destructor...n";} virtual long GetNumberBelievers() const { return itsNumberBelievers; } virtual COLOR GetColor()const { return Horse::itsColor; } private: long itsNumberBelievers; }; Pegasus::Pegasus( COLOR aColor, HANDS height, BOOL migrates, long NumBelieve, int age): Horse(aColor, height,age), Bird(aColor, migrates,age), Animal(age*2), itsNumberBelievers(NumBelieve) { cout << "Pegasus constructor...n"; } int main() { Pegasus *pPeg = new Pegasus(Red, 5, TRUE, 10, 2); int age = pPeg->GetAge(); cout << "This pegasus is " << age << " years old.n";
  • 3. delete pPeg; return 0; } / virtual members #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; }
  • 4. #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 (void) =0; }; class CRectangle: public CPolygon { public: int area (void) { return (width * height); } }; class CTriangle: public CPolygon { public: int area (void) { 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 << ppoly1->area() << endl; cout << ppoly2->area() << endl; return 0; } // pure virtual members can be called // from the abstract 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; } virtual int area (void) =0; void printarea (void) { cout << this->area() << endl; } }; class CRectangle: public CPolygon { public: int area (void) { return (width * height); } };
  • 5. class CTriangle: public CPolygon { public: int area (void) { 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); ppoly1->printarea(); ppoly2->printarea(); return 0; }