Herencia en C++

 

La capacidad de una clase para derivar propiedades y características de otra clase se denomina herencia . La herencia es una de las características más importantes de la Programación Orientada a Objetos. 

La herencia es una característica o un proceso en el que se crean nuevas clases a partir de las clases existentes. La nueva clase creada se denomina «clase derivada» o «clase secundaria» y la clase existente se conoce como «clase base» o «clase principal». Ahora se dice que la clase derivada se hereda de la clase base.

C++

// Example: define member function without argument within the class
 
#include<iostream>
using namespace std;
 
class Person
{
    int id;
    char name[100];
   
    public:
        void set_p()
        {
            cout<<"Enter the Id:";
            cin>>id;
            fflush(stdin);
            cout<<"Enter the Name:";
            cin.get(name,100);
        }
   
        void display_p()
        {
            cout<<endl<<id<<"\t"<<name;
        }
};
 
class Student: private Person
{
    char course[50];
    int fee;
     
    public:
    void set_s()
        {
            set_p();
            cout<<"Enter the Course Name:";
            fflush(stdin);
            cin.getline(course,50);
            cout<<"Enter the Course Fee:";
            cin>>fee;
        }
         
        void display_s()
        {
            display_p();
            cout<<"t"<<course<<"\t"<<fee;
        }
};
 
main()
{
    Student s;
    s.set_s();
    s.display_s();
    return 0;
}

C++

// Example: define member function without argument outside the class
 
#include<iostream>
using namespace std;
 
class Person
{
    int id;
    char name[100];
   
    public:
        void set_p();
        void display_p();
};
 
void Person::set_p()
{
    cout<<"Enter the Id:";
    cin>>id;
    fflush(stdin);
    cout<<"Enter the Name:";
    cin.get(name,100);
}
 
void Person::display_p()
{
    cout<<endl<<id<<"\t"<<name;
}
 
class Student: private Person
{
    char course[50];
    int fee;
     
    public:
        void set_s();
        void display_s();
};
 
void Student::set_s()
{
    set_p();
    cout<<"Enter the Course Name:";
    fflush(stdin);
    cin.getline(course,50);
    cout<<"Enter the Course Fee:";
    cin>>fee;
}
 
void Student::display_s()
{
    display_p();
    cout<<"t"<<course<<"\t"<<fee;
}
 
main()
{
    Student s;
    s.set_s();
    s.display_s();
    return 0;
}

C++

// Example: define member function with argument outside the class
 
#include<iostream>
#include<string.h>
using namespace std;
 
class Person
{
    int id;
    char name[100];
   
    public:
        void set_p(int,char[]);
        void display_p();
};
 
void Person::set_p(int id,char n[])
{
    this->id=id;
    strcpy(this->name,n);       
}
 
void Person::display_p()
{
    cout<<endl<<id<<"\t"<<name;
}
 
class Student: private Person
{
    char course[50];
    int fee;
    public:
    void set_s(int,char[],char[],int);
    void display_s();
};
 
void Student::set_s(int id,char n[],char c[],int f)
{
    set_p(id,n);
    strcpy(course,c);
    fee=f;
}
 
 
void Student::display_s()
{
    display_p();
    cout<<"t"<<course<<"\t"<<fee;
}
 
main()
{
    Student s;
    s.set_s(1001,"Ram","B.Tech",2000);
    s.display_s();
    return 0;
}

CPP

// C++ program to demonstrate implementation
// of Inheritance
 
#include <bits/stdc++.h>
using namespace std;
 
// Base class
class Parent {
public:
    int id_p;
};
 
// Sub class inheriting from Base Class(Parent)
class Child : public Parent {
public:
    int id_c;
};
 
// main function
int main()
{
    Child obj1;
 
    // An object of class child has all data members
    // and member functions of class parent
    obj1.id_c = 7;
    obj1.id_p = 91;
    cout << "Child id is: " << obj1.id_c << '\n';
    cout << "Parent id is: " << obj1.id_p << '\n';
 
    return 0;
}

CPP

// C++ Implementation to show that a derived class
// doesn’t inherit access to private data members.
// However, it does inherit a full parent object.
class A {
public:
    int x;
 
protected:
    int y;
 
private:
    int z;
};
 
class B : public A {
    // x is public
    // y is protected
    // z is not accessible from B
};
 
class C : protected A {
    // x is protected
    // y is protected
    // z is not accessible from C
};
 
class D : private A // 'private' is default for classes
{
    // x is private
    // y is private
    // z is not accessible from D
};

CPP

// C++ program to explain
// Single inheritance
#include<iostream>
using namespace std;
 
// base class
class Vehicle {
  public:
    Vehicle()
    {
      cout << "This is a Vehicle\n";
    }
};
 
// sub class derived from a single base classes
class Car : public Vehicle {
 
};
 
// main function
int main()
{  
    // Creating object of sub class will
    // invoke the constructor of base classes
    Car obj;
    return 0;
}

C++

// Example:
 
#include<iostream>
using namespace std;
 
class A
{
    protected:
    int a;
   
    public:
        void set_A()
        {
            cout<<"Enter the Value of A=";
            cin>>a;
             
        }
        void disp_A()
        {
            cout<<endl<<"Value of A="<<a;
        }
};
 
 
class B: public A
{
    int b,p;
     
    public:
        void set_B()
        {
            set_A();
            cout<<"Enter the Value of B=";
            cin>>b;
        }
         
        void disp_B()
        {
            disp_A();
            cout<<endl<<"Value of B="<<b;
        }
         
        void cal_product()
        {
            p=a*b;
            cout<<endl<<"Product of "<<a<<" * "<<b<<" = "<<p;
        }
         
};
 
main()
{
     
    B _b;
    _b.set_B();
    _b.cal_product();
     
    return 0;
     
}

C++

// Example:
 
#include<iostream>
using namespace std;
 
class A
{
    protected:
    int a;
   
    public:
        void set_A(int x)
        {
             a=x;           
        }
   
        void disp_A()
        {
            cout<<endl<<"Value of A="<<a;
        }
};
 
class B: public A
{
    int b,p;
     
    public:
        void set_B(int x,int y)
        {
            set_A(x);
            b=y;
        }
         
        void disp_B()
        {
            disp_A();
            cout<<endl<<"Value of B="<<b;
        }
         
        void cal_product()
        {
            p=a*b;
            cout<<endl<<"Product of "<<a<<" * "<<b<<" = "<<p;
        }
         
};
 
main()
{
    B _b;
    _b.set_B(4,5);
    _b.cal_product();
     
    return 0;
}

CPP

// C++ program to explain
// multiple inheritance
#include <iostream>
using namespace std;
 
// first base class
class Vehicle {
public:
    Vehicle() { cout << "This is a Vehicle\n"; }
};
 
// second base class
class FourWheeler {
public:
    FourWheeler()
    {
        cout << "This is a 4 wheeler Vehicle\n";
    }
};
 
// sub class derived from two base classes
class Car : public Vehicle, public FourWheeler {
};
 
// main function
int main()
{
    // Creating object of sub class will
    // invoke the constructor of base classes.
    Car obj;
    return 0;
}

C++

// Example:
 
#include<iostream>
using namespace std;
 
class A
{
              protected:
              int a;
   
              public:
                  void set_A()
                  {
                        cout<<"Enter the Value of A=";
                        cin>>a;
          
                  }
 
                  void disp_A()
                  {
                        cout<<endl<<"Value of A="<<a;
                  }
};
 
class B: public A
{
           protected:
                int b;
             
           public:
                   void set_B()
                {
                      cout<<"Enter the Value of B=";
                       cin>>b;
                   }
 
         
                void disp_B()
                  {
                     cout<<endl<<"Value of B="<<b;
                  }
};
 
class C: public B
{
      int c,p;
       
      public:
          void set_C()
          {
                 cout<<"Enter the Value of C=";
                 cin>>c;
           }
         
           void disp_C()
           {
                 cout<<endl<<"Value of C="<<c;
           }
     
            void cal_product()
             {
                   p=a*b*c;
                  cout<<endl<<"Product of "<<a<<" * "<<b<<" * "<<c<<" = "<<p;
              }
};
 
main()
{
     
    C _c;
    _c.set_A();
    _c.set_B();
    _c.set_C();
    _c.disp_A();
    _c.disp_B();
    _c.disp_C();
    _c.cal_product();
     
    return 0;
     
}

CPP

// C++ program to implement
// Multilevel Inheritance
#include <iostream>
using namespace std;
 
// base class
class Vehicle {
public:
    Vehicle() { cout << "This is a Vehicle\n"; }
};
 
// first sub_class derived from class vehicle
class fourWheeler : public Vehicle {
public:
    fourWheeler()
    {
        cout << "Objects with 4 wheels are vehicles\n";
    }
};
// sub class derived from the derived base class fourWheeler
class Car : public fourWheeler {
public:
    Car() { cout << "Car has 4 Wheels\n"; }
};
 
// main function
int main()
{
    // Creating object of sub class will
    // invoke the constructor of base classes.
    Car obj;
    return 0;
}

CPP

// C++ program to implement
// Hierarchical Inheritance
#include <iostream>
using namespace std;
 
// base class
class Vehicle {
public:
    Vehicle() { cout << "This is a Vehicle\n"; }
};
 
// first sub class
class Car : public Vehicle {
};
 
// second sub class
class Bus : public Vehicle {
};
 
// main function
int main()
{
    // Creating object of sub class will
    // invoke the constructor of base class.
    Car obj1;
    Bus obj2;
    return 0;
}

CPP

// C++ program for Hybrid Inheritance
 
#include <iostream>
using namespace std;
 
// base class
class Vehicle {
public:
    Vehicle() { cout << "This is a Vehicle\n"; }
};
 
// base class
class Fare {
public:
    Fare() { cout << "Fare of Vehicle\n"; }
};
 
// first sub class
class Car : public Vehicle {
};
 
// second sub class
class Bus : public Vehicle, public Fare {
};
 
// main function
int main()
{
    // Creating object of sub class will
    // invoke the constructor of base class.
    Bus obj2;
    return 0;
}

C++

// Example:
 
#include <iostream> 
using namespace std; 
 
class A 
{ 
    protected: 
    int a; 
    public: 
    void get_a() 
    { 
       cout << "Enter the value of 'a' : "; 
       cin>>a; 
    } 
}; 
   
class B : public A  
{ 
    protected: 
    int b; 
    public: 
    void get_b() 
    { 
       cout << "Enter the value of 'b' : ";
       cin>>b; 
    } 
}; 
class C  
{ 
    protected: 
    int c; 
    public: 
    void get_c() 
    { 
        cout << "Enter the value of c is : "; 
        cin>>c; 
    } 
}; 
   
class D : public B, public C 
{ 
    protected: 
    int d; 
    public: 
    void mul() 
    { 
         get_a(); 
         get_b(); 
         get_c(); 
         cout << "Multiplication of a,b,c is : " <<a*b*c; 
    } 
}; 
 
 
int main() 
{ 
    D d; 
    d.mul(); 
    return 0; 
}

CPP

// C++ program demonstrating ambiguity in Multipath
// Inheritance
 
#include <iostream>
using namespace std;
 
class ClassA {
public:
    int a;
};
 
class ClassB : public ClassA {
public:
    int b;
};
 
class ClassC : public ClassA {
public:
    int c;
};
 
class ClassD : public ClassB, public ClassC {
public:
    int d;
};
 
int main()
{
    ClassD obj;
 
    // obj.a = 10;                  // Statement 1, Error
    // obj.a = 100;                 // Statement 2, Error
 
    obj.ClassB::a = 10; // Statement 3
    obj.ClassC::a = 100; // Statement 4
 
    obj.b = 20;
    obj.c = 30;
    obj.d = 40;
 
    cout << " a from ClassB  : " << obj.ClassB::a;
    cout << "\n a from ClassC  : " << obj.ClassC::a;
 
    cout << "\n b : " << obj.b;
    cout << "\n c : " << obj.c;
    cout << "\n d : " << obj.d << '\n';
}

CPP

obj.ClassB::a = 10;       // Statement 3
obj.ClassC::a = 100;      // Statement 4

CPP

#include<iostream>
 
class ClassA
{
  public:
    int a;
};
 
class ClassB : virtual public ClassA
{
  public:
    int b;
};
 
class ClassC : virtual public ClassA
{
  public:
    int c;
};
 
class ClassD : public ClassB, public ClassC
{
  public:
    int d;
};
 
int main()
{
    ClassD obj;
 
    obj.a = 10;       // Statement 3
    obj.a = 100;      // Statement 4
 
    obj.b = 20;
    obj.c = 30;
    obj.d = 40;
 
    cout << "\n a : " << obj.a;
    cout << "\n b : " << obj.b;
    cout << "\n c : " << obj.c;
    cout << "\n d : " << obj.d << '\n';
}

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *