logo

Herencia C++

En C++, la herencia es un proceso en el que un objeto adquiere automáticamente todas las propiedades y comportamientos de su objeto principal. De tal manera, puede reutilizar, ampliar o modificar los atributos y comportamientos que están definidos en otra clase.

En C++, la clase que hereda los miembros de otra clase se llama clase derivada y la clase cuyos miembros se heredan se llama clase base. La clase derivada es la clase especializada de la clase base.


Ventaja de la herencia de C++

Reutilización del código: Ahora puedes reutilizar los miembros de tu clase principal. Por tanto, no es necesario volver a definir el miembro. Entonces se requiere menos código en la clase.

convertir cadena a jsonobject java

Tipos de herencia

C++ admite cinco tipos de herencia:

  • herencia única
  • herencia múltiple
  • Herencia jerárquica
  • Herencia multinivel
  • Herencia híbrida
Herencia C++

Clases derivadas

Una clase derivada se define como la clase derivada de la clase base.

funciones de arduino

La sintaxis de la clase derivada:

|_+_|

En el caso anterior, la función de la clase derivada anula el método de la clase base. Por lo tanto, la llamada a la función display() simplemente llamará a la función definida en la clase derivada. Si queremos invocar la función de clase base, podemos usar el operador de resolución de clase.

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

Herencia híbrida de C++

La herencia híbrida es una combinación de más de un tipo de herencia.

Herencia C++

Veamos un ejemplo sencillo:

 #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<<>

Herencia jerárquica de C++

La herencia jerárquica se define como el proceso de derivar más de una clase a partir de una clase base.

Herencia C++

Sintaxis de herencia jerárquica:

 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. } 

Veamos un ejemplo sencillo:

cambiar el nombre del directorio linux
 #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<<>