Page 453 - Computer science 868 Class 12
P. 453
Base
• Hierarchical Inheritance: This type of inheritance, a base class has many sub
classes inheriting it’s properties as shown in the diagram on the left.
Derived Derived
Base1 Base2 Base3
• Hybrid Inheritance: It is a combination of two or more inheritances
like multiple and hierarchical, multiple and multilevel as shown
alongside. Derived1
12.2 SOLVED PROGRAMS ON INHERITANCE Derived2
Note: In ISC examination, you are asked to write only the derived class definition.
Program 1 A base class Factorial has been defined to find the factorial of any number. The class
description is given below.
Class name : Factorial
Member Methods
int calculate (int x) : Calculates and returns the factorial of the number x
The derived class checks whether a given number entered by the user is a Krishnamurthy
number or not. Krishnamurthy number is one which is equal to the sum of factorial of its
digits. The details of the class is given below.
Class name : Krishnamurthy
Member Methods
int n : Stores number
void read(int nn) : Initialises n to nn
int sumoffact() : Calls calculate(int) to calculate the sum of factorial of the digits
void display() : Calls sumoffact( ) and prints if the number is Krishnamurthy
number or not
Also write the main method to create object of the derived class and call appropriate methods
to implement the above.
1 class Factorial
2 { int calculate(int x)
3 { int f=1,i;
4 for(i=1;i<=x;i++)
5 { f=f*i;}
6 return f;
7 }
8 }
451
Inheritance, Interfaces and Polymorphism 451

