Write a C++ Program to Overriding member functions using Inheritance. Here’s a Simple C++ Program to Overriding member functions using Inheritance in C++ Programming Language.
The member functions can also be used in a derived class, with the same name as those in the base class.
One might want to do this so that calls in the program work the same way for objects of both base and derived classes.
The following program will illustrate this concept:
Below is the source code for C++ Program to Overriding member functions using Inheritance which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C++ Program to Overriding member functions using Inheritance */
#include<iostream>
using namespace std;
const int len = 20 ;
class Employee
{
private:
char F_name[len];
int I_number ;
int age ;
float salary ;
public:
void Enter_data(void)
{
cout << "\n Enter the first name = " ; cin >> F_name ;
cout << "\n Enter the identity number = " ; cin >> I_number ;
cout << "\n Enter the age = " ; cin >> age ;
cout << "\n Enter the salary = " ; cin >> salary ;
}
void Display_data(void)
{
cout << "\n Name = " << F_name ;
cout << "\n Identity Number = " << I_number ;
cout << "\n Age = " << age ;
cout << "\n Salary = " << salary ;
}
}; // End of the base class
class Engineer : public Employee
{
private:
char design[len] ; // S_Engineer, J_Engineer, Ex_Engineer etc
public:
void Enter_data( )
{
Employee :: Enter_data( ) ; // Overriding of the member function
cout << "\n Enter the designation of the Engineer: " ; cin >> design ;
}
void Display_data(void)
{
cout << "\n *******Displaying the particulars of the Engineer**** \n" ;
Employee :: Display_data( ) ; // Overriding of the member function
cout << "\n Designition = " << design ;
}
}; // End of the derived class
int main(void)
{
Engineer er ;
er.Enter_data( ) ;
er.Display_data( );
return 0;
}
OUTPUT : :
/* C++ Program to Overriding member functions using Inheritance */
Enter the first name = CodezClub
Enter the identity number = 123
Enter the age = 20
Enter the salary = 50000
Enter the designation of the Engineer: S_Engineer
*******Displaying the particulars of the Engineer****
Name = CodezClub
Identity Number = 123
Age = 20
Salary = 50000
Designition = S_Engineer
Process returned 0
Above is the source code and output for C++ Program to Overriding member functions using Inheritance which is successfully compiled and run on Windows System to produce desired output.
Overriding member functions : :
Below is the source code for C++ Program to Overriding member functions using Inheritance which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
OUTPUT : :
Above is the source code and output for C++ Program to Overriding member functions using Inheritance which is successfully compiled and run on Windows System to produce desired output.
need an explanation for this answer? contact us directly to get an explanation for this answer