logo

Eredità C++

In C++, l'ereditarietà è un processo in cui un oggetto acquisisce automaticamente tutte le proprietà e i comportamenti dell'oggetto genitore. In questo modo, puoi riutilizzare, estendere o modificare gli attributi e i comportamenti definiti in altre classi.

In C++, la classe che eredita i membri di un'altra classe è chiamata classe derivata e la classe i cui membri sono ereditati è chiamata classe base. La classe derivata è la classe specializzata per la classe base.


Vantaggio dell'ereditarietà C++

Riutilizzabilità del codice: Ora puoi riutilizzare i membri della tua classe genitore. Pertanto, non è necessario definire nuovamente il membro. Quindi è richiesto meno codice nella classe.

convertire la stringa in json in Java

Tipi di eredità

Il C++ supporta cinque tipi di ereditarietà:

  • Eredità unica
  • Eredità multipla
  • Eredità gerarchica
  • Eredità multilivello
  • Eredità ibrida
Eredità C++

Classi derivate

Una classe derivata è definita come la classe derivata dalla classe base.

gestore attività di Linux

La sintassi della classe derivata:

|_+_|

Nel caso precedente, la funzione della classe derivata sovrascrive il metodo della classe base. Pertanto, la chiamata alla funzione display() chiamerà semplicemente la funzione definita nella classe derivata. Se vogliamo invocare la funzione della classe base, possiamo usare l'operatore di risoluzione della classe.

 int main() { B b; b.display(); // Calling the display() function of B class. b.B :: display(); // Calling the display() function defined in B class. } 

Ereditarietà ibrida C++

L'ereditarietà ibrida è una combinazione di più tipi di ereditarietà.

Eredità C++

Vediamo un semplice esempio:

 #include using namespace std; class A { protected: int a; public: void get_a() { std::cout &lt;&lt; &apos;Enter the value of &apos;a&apos; : &apos; &lt;&gt;a; } }; class B : public A { protected: int b; public: void get_b() { std::cout &lt;&lt; &apos;Enter the value of &apos;b&apos; : &apos; &lt;&gt;b; } }; class C { protected: int c; public: void get_c() { std::cout &lt;&lt; &apos;Enter the value of c is : &apos; &lt;&gt;c; } }; class D : public B, public C { protected: int d; public: void mul() { get_a(); get_b(); get_c(); std::cout &lt;&lt; &apos;Multiplication of a,b,c is : &apos; &lt; <a*b*c<< std::endl; } }; int main() { d d; d.mul(); return 0; < pre> <p>Output:</p> <pre> Enter the value of &apos;a&apos; : 10 Enter the value of &apos;b&apos; : 20 Enter the value of c is : 30 Multiplication of a,b,c is : 6000 </pre> <h2>C++ Hierarchical Inheritance</h2> <p>Hierarchical inheritance is defined as the process of deriving more than one class from a base class.</p> <img src="//techcodeview.com/img/c-tutorial/62/c-inheritance-7.webp" alt="C++ Inheritance"> <p> <strong>Syntax of Hierarchical inheritance:</strong> </p> <pre> class A { // body of the class A. } class B : public A { // body of class B. } class C : public A { // body of class C. } class D : public A { // body of class D. } </pre> <p>Let&apos;s see a simple example:</p> <pre> #include using namespace std; class Shape // Declaration of base class. { public: int a; int b; void get_data(int n,int m) { a= n; b = m; } }; class Rectangle : public Shape // inheriting Shape class { public: int rect_area() { int result = a*b; return result; } }; class Triangle : public Shape // inheriting Shape class { public: int triangle_area() { float result = 0.5*a*b; return result; } }; int main() { Rectangle r; Triangle t; int length,breadth,base,height; std::cout &lt;&lt; &apos;Enter the length and breadth of a rectangle: &apos; &lt;&gt;length&gt;&gt;breadth; r.get_data(length,breadth); int m = r.rect_area(); std::cout &lt;&lt; &apos;Area of the rectangle is : &apos; &lt;<m<< std::endl; std::cout << \\'enter the base and height of triangle: \\' <>base&gt;&gt;height; t.get_data(base,height); float n = t.triangle_area(); std::cout &lt;<\\'area of the triangle is : \\' << n<<std::endl; return 0; } < pre> <p>Output:</p> <pre> Enter the length and breadth of a rectangle: 23 20 Area of the rectangle is : 460 Enter the base and height of the triangle: 2 5 Area of the triangle is : 5 </pre></\\'area></m<<></pre></a*b*c<<>

Ereditarietà gerarchica C++

L'ereditarietà gerarchica è definita come il processo di derivazione di più di una classe da una classe base.

Eredità C++

Sintassi dell'ereditarietà gerarchica:

 class A { // body of the class A. } class B : public A { // body of class B. } class C : public A { // body of class C. } class D : public A { // body of class D. } 

Vediamo un semplice esempio:

Shreya Ghoshal, primo marito
 #include using namespace std; class Shape // Declaration of base class. { public: int a; int b; void get_data(int n,int m) { a= n; b = m; } }; class Rectangle : public Shape // inheriting Shape class { public: int rect_area() { int result = a*b; return result; } }; class Triangle : public Shape // inheriting Shape class { public: int triangle_area() { float result = 0.5*a*b; return result; } }; int main() { Rectangle r; Triangle t; int length,breadth,base,height; std::cout &lt;&lt; &apos;Enter the length and breadth of a rectangle: &apos; &lt;&gt;length&gt;&gt;breadth; r.get_data(length,breadth); int m = r.rect_area(); std::cout &lt;&lt; &apos;Area of the rectangle is : &apos; &lt;<m<< std::endl; std::cout << \\'enter the base and height of triangle: \\' <>base&gt;&gt;height; t.get_data(base,height); float n = t.triangle_area(); std::cout &lt;<\\'area of the triangle is : \\' << n<<std::endl; return 0; } < pre> <p>Output:</p> <pre> Enter the length and breadth of a rectangle: 23 20 Area of the rectangle is : 460 Enter the base and height of the triangle: 2 5 Area of the triangle is : 5 </pre></\\'area></m<<>