Lab 07 Overriding in Inheritance: 1. Objectives
Lab 07 Overriding in Inheritance: 1. Objectives
Overriding in Inheritance
1. Objectives :
To familiarize the students with the important concept of overriding in inheritance in Object Oriented
Programming .
2. Outcome:
After this lab the students should be able to perform the overriding of member functions. They should
learn how to access the overridden function in the base class from the derived class.
3. Introduction:
3.1. Overriding:
Function overriding is a feature that allows us to have a same function in child class which is already
present in the parent class. A child class inherits the data members and member functions of parent
class, but when you want to override functionality in the child class then you can use function
overriding.
You have learned that a derived class can inherit both public and protected members (both variable and
functions) from a base class. However, the derived class can also redefine the inherited member
function. If the derived class defines a member function with has the same signature (number and type
of parameters) as the base class, then the derived class is overriding the base class's member function.
To access the overridden function of the base class from the derived class, scope resolution
operator:: is used. For example, If you want to access getData() function of the base class, you
can use the following statement in the derived class.
4. Examples:
4.1. An example overriding push() function in a stack class..
4.2. An example overriding the sign of measurement in a Distance class.
5. In-Lab Tasks:
5.1. Write a program that declares two classes. The parent class is called Simple that has two
data members num1 and num2to store two numbers. It also has four member functions.
The add() function adds two numbers and displays the result.
The sub() function subtracts two numbers and displays the result.
The mul() function multiplies two numbers and displays the result.
The div() function divides two numbers and displays the result.
The child class is called Complex that overrides all four functions. Each function in the child
class checks the value of data members. It calls the corresponding member function in the parent
class if the values are greater than 0. Otherwise it displays error message.
5.2. Write a program having a base class Student with data members rollno, name and Class
define a member functions getdata() to input values and another function putdata() to display all
values.A class Test is derived from class Student with data members T1marks, T2marks,
T3marks, Sessional1, Sessional2, Assignment and Final. Also make an overriding function
getdata() to enter marks for all variables except Final and also make an overriding function
putdata() to display result. Make a function Finalresult() to calculate value for final variable
using other marks. Then display the student result along with student data.
6. Post-Lab Tasks:
6.1. An electricity board charges the following rates to domestic users to discourage large
consumption of energy.
For the first 100 units − 50 P per unit
Beyond 100 units − 60 P per unit
If the total cost is more than Rs.250.00 then an additional surcharge of 15% is added on the
difference. Define a class Electricity in which the function Bill computes the cost. Define a
derived class More_Electricity and override Bill to add the surcharge.
6.2. Create a class 2D having the x and y coordinates of an object. Derive a class 3D form
from 2D as a base class with an additional z coordinate, override x and y coordinates of 2D class
using setdata() function. Get the coordinates for two specific points and compute the distance
between them.