0% found this document useful (0 votes)
137 views

Inheritance

C++ inheritance allows one class to inherit attributes and methods from another class. There are four forms of inheritance in C++: single, multilevel, hierarchical, and multiple. To inherit from a class, use the colon ":" symbol followed by the base class name when defining the derived class.

Uploaded by

mohammad haqqi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views

Inheritance

C++ inheritance allows one class to inherit attributes and methods from another class. There are four forms of inheritance in C++: single, multilevel, hierarchical, and multiple. To inherit from a class, use the colon ":" symbol followed by the base class name when defining the derived class.

Uploaded by

mohammad haqqi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

‫‪C++ Inheritance‬‬

‫• ﻣﻔﮭوم اﻟوراﺛﺔ ﻓﻲ ‪C++‬‬


‫• أﺷﻛﺎل اﻟوراﺛﺔ ﻓﻲ ‪C++‬‬
‫• ﻛﯾﻔﯾﺔ ﺟﻌل اﻟﻛﻼس ﯾرث ﻣن ﻛﻼس آﺧر ﻓﻲ ‪C++‬‬
‫• ﻛﻠﻣﺎت اﻟوﺻول ﻓﻲ ‪C++‬‬
‫• أﻣﺛﻠﺔ ﺗطﺑﯾﻘﯾﺔ ﻋﻠﻰ اﻟوراﺛﺔ ﻓﻲ ‪C++‬‬
‫• ﻣﺷﻛﻠﺔ اﻟوراﺛﺔ اﻟﻣﺗﻌددة ﻓﻲ ‪C++‬‬
‫ﻣﻔﮭوم اﻟوراﺛﺔ ﻓﻲ ‪C++‬‬
‫ﻓﻲ اﻟﺑداﯾﺔ‪ ,‬ﻛﻠﻣﺔ وراﺛﺔ ﺗﻌﻧﻲ ﺗﺿﻣﯾن ﻣﺣﺗوى ﻛﻼس ﻓﻲ ﻛﻼس آﺧر‪.‬‬
‫ﻓﻲ ‪C++,‬اﻟﻛﻼس ﯾﻣﻛﻧﮫ أن ﯾرث ﻣن ﻛﻼس آﺧر ﺣﺗﻰ ﯾﺣﺻل ﻋﻠﻰ اﻟدوال و اﻟﻣﺗﻐﯾرات اﻟﻣوﺟودة ﻓﯾﮫ‪.‬‬
‫إذا ً ﻓﻛرة اﻟوراﺛﺔ ﺑﺳﯾطﺔ ﻟﻛن ﻓﺎﺋدﺗﮭﺎ ﻗوﯾﺔ ﺟدا ً‪ .‬ﻓﻣﺛﻼً إذا ﻛﻧت ﺗرﯾد إﻧﺷﺎء ﻛﻼس ﺟدﯾد و ﻻﺣظت أﻧﮫ ﯾوﺟد ﻛﻼس ﺟﺎھز ﯾﺣﺗوي ﻋﻠﻰ ﻛودات ﻗد ﺗﻔﯾدك‬
‫ﯾﻣﻛﻧك اﺳﺗﻐﻼﻟﮭﺎ ﺑدل ﻛﺗﺎﺑﺗﮭﺎ ﻣن اﻟﺻﻔر‪ ,‬أي ﯾﻣﻛﻧك ﺟﻌل اﻟﻛﻼس اﻟذي ﻗﻣت ﺑﺗﻌرﯾﻔﮫ ﯾرث ھذا اﻟﻛﻼس‪ ,‬و ﺑﻌدھﺎ ﯾﻣﻛﻧك إﺳﺗﺧدام ﺟﻣﯾﻊ اﻟﻣﺗﻐﯾرات و‬
‫اﻟدوال اﻟﺗﻲ ورﺛﮭﺎ اﻟﻛﻼس اﻟﺟدﯾد ﻣن اﻟﻛﻼس اﻟﺟﺎھز‪.‬‬

‫‪Inheritance‬‬
‫‪In C++, it is possible to inherit attributes and methods from one class to another. We‬‬
‫‪group the "inheritance concept" into two categories:‬‬
‫‪•derived class (child) - the class that inherits from another class‬‬
‫‪•base class (parent) - the class being inherited from‬‬
‫أﺷﻛﺎل اﻟوراﺛﺔ ﻓﻲ ‪C++‬‬
‫ﻓﻲ ‪C++‬ﯾوﺟد ‪ 4‬أﺷﻛﺎل ﻟﻠوراﺛﺔ ﻛﻣﺎ ﻓﻲ اﻟﺟدول اﻟﺗﺎﻟﻲ‪.‬‬

‫إذا َ أﺷﻛﺎل اﻟوراﺛﺔ ﻓﻲ ‪C++‬ھﻲ ﻛﺎﻟﺗﺎﻟﻲ‪:‬‬

‫•وراﺛﺔ ﻓردﯾﺔ‪ :‬ﺗﻌﻧﻲ ﻛﻼس ﯾرث ﻣن ﻛﻼس واﺣد ﻓﻘط‪.‬‬

‫•وراﺛﺔ ﻣﺗﺗﺎﻟﯾﺔ‪ :‬ﺗﻌﻧﻲ ﻛﻼس ﯾرث ﻣن ﻛﻼس واﺣد و ھذا اﻟﻛﻼس‬


‫ﻛﺎن ﻓﻲ اﻷﺻل ﯾرث ﻣن ﻛﻼس آﺧر‪.‬‬

‫•وراﺛﺔ ھرﻣﯾﺔ‪ :‬ﺗﻌﻧﻲ أن اﻟﻛﻼس ﻣوروث ﻣن ﻗﺑل أﻛﺛر ﻣن ﻛﻼس‪.‬‬

‫•وراﺛﺔ ﻣﺗﻌددة‪ :‬ﺗﻌﻧﻲ أن اﻟﻛﻼس ﯾرث ﻣن أﻛﺛر ﻣن ﻛﻼس‪.‬‬


‫ﻛﯾﻔﯾﺔ ﺟﻌل اﻟﻛﻼس ﯾرث ﻣن ﻛﻼس آﺧر ﻓﻲ ‪C++‬‬
‫ﻟﺟﻌل اﻟﻛﻼس ﯾرث ﻣن ﻛﻼس آﺧر ﻧﺗﺑﻊ اﻷﺳﻠوب اﻟﺗﺎﻟﻲ‪.‬‬
To inherit from a class, use the : symbol.
In the example below,
the B class (child) inherits the attributes
and methods from the A class (parent):
Multilevel Inheritance
A class can also be derived from one class, which is already derived from another class.
In the following
example, C is derived
from class B (which is
derived from A).
Multiple Inheritance
A class can also be derived from more than one base class, using a comma-separated list:

You might also like