Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism

Overriding

C++ Program

Question: 1. Explain the concept of function overriding. Differentiate between function overloading and function overriding.

Overriding

• Redefining a function in a derived class is called function overriding.

• Function overloading means within the class we can declare same function name, but arguments and return types are different and function overriding means the function name is same but the task carried out with it is different.

C++ Program

#include<iostream>

using namespace std;

class A

{

private:

int a,b;

public:

void get_msg()

{

   a=10;

   b=20;

}

void print_ msg()const

{

   int c;

   c=a+b;//performing addition

   cout<<"\n C(10+20)= "<<c;

   cout<<"\n I'm print_msg() in class A";

}

};

class B: public A

{

private:

int a,b;

public:

void set_ msg()

{

   a=100;

   b=10:

}

void print_msg()const

{

   int c;

   c=a‒b;//performing subtratcion in this function

   cout<<"\n\n C(100‒10)= "<<c;

   cout<<"\n I'm print_msg() in class B ";

}

};

void main()

{

   A obj_base;

   B obj_derived;

   clrscr();

   obj_base.get_msg();

   obj_base.print_msg();//same function name

   obj_derived.set_msg();

   obj_derived.print msg();//but different tasks

}

Output

C(10+20)=30

I'm print _msg() in class A

C(100–10) = 90

I'm print msg() in class B

• In above program we have written two functions print_msg() by the same name but performing different operation. The print_msg() function in class A (i.e. base class) is performing addition of two numbers and the print_msg() function in class B is performing the subtraction of two numbers. Both the functions have same return type, same name and no parameters but their role is changing. This mechanism of changing the task of some function in derived class is called function overriding.

 

Difference between Function overloading and Function overriding


Method overloading

1. The method overloading occurs at compile time.

2. In case of method overloading different number of parameters can be passed to the function.

3. The overloaded functions may have different return types.

4. Method overloading is performed within a class.

 Method overriding

1. The method overriding occurs at the run time or execution time.

2. In function overriding the number of parameters that are passed to the function are the same.

3. In method overriding all the methods will have the same return type.

4. Method overriding is normally performed between two classes that have inheritance relationship.

 

Example: 1

Develop an object oriented program in C++ to create a payroll system for an organization where one base class consist of employee name, code, designation and another base class consist of a account no and date of joining. The derived class consists of the data members such as basic pay, DA, HRA, CCA and deductions PF, LIC, IT. (Program must use the concept of virtual base class)

Solution :

/**************************************************************************

Program to Payroll System for organization

**************************************************************************/

#include<iostream>

using namespace std;

class Employee

{

   public:

   char name[10];

   int code;

   char designation[15];

};

class Accounts:virtual public Employee

{

   int accno;

   char doj[10];

};

class Pay:virtual public Employee

{

   public:

   double BasicPay;

   double DA;

   double HRA;

   double CCA;

   double PF LIC,IT;

};

class Derived:public Accounts,public Pay

{

public:

void get _details()

{

   cout<<"\n Enter the name of Employee: ";

   cin>>name;

   cout<<"\n Enter the Employee Code: ";

   cin>>code;

   cout<<"\n Enter the designation of Employee: ";

   cin>>designation;

   cout<<"\n Enter the Basic Payment: ";

   cin>>BasicPay;

   cout<<"\n Enter amount of DA: ";

   cin>>DA;

   cout<<"\n Enter the amount of HRA: ";

   cin>>HRA;

   cout<<"\n Enter the amount of CCA: ";

   cin>>CCA;

   cout<<"\n Enter the amount of PF: ";

   cin>>PF;

   cout<<"\n Enter the amount of LIC: ";

   cin>>LIC;

   cout<<"\n Enter the amount of IT: ";

   cin>>IT;

}

double NetPayment()

{

   double amount,deductions;

   deductions=PF+LIC+IT;

   amount=(BasicPay+DA+HRA+CCA)‒deductions;

   return amount;

}

};

void main()

{

   Derived obj;

   obj.get_details();

   cout<<obj.NetPayment();

}

Output

Enter the name of Employee: ABC

Enter the Employee Code: 100

Enter the designation of Employee: Manager

Enter the Basic Payment: 12000

Enter amount of DA: 3000

Enter the amount of HRA: 2000

Enter the amount of CCA: 700

Enter the amount of PF: 1200

Enter the amount of LIC: 1000

Enter the amount of IT: 3000

12500

 

Review Question

1. Explain the concept of function overriding. Differentiate between function overloading and function overriding.

 

Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism : Tag: Data Structure, C++, C Programing : C++ Program - Overriding


Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism



Under Subject


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Electron Devices

EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Circuits and Network Analysis

EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Engineering Drawing - Laboratory

ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Data Structures using CPlusPlus - Laboratory

CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Devices and Circuits Laboratory

EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation