Page 457 - Computer science 868 Class 12
P. 457

The output of the preceding program is as follows:



















                   Program 3     A base class called Number is defined which finds the number of digits in a number. The
                                 details of the class is given below.
                                 Class name               :   Number
                                 Data Members
                                 num                      :   Integer to store number
                                 len                      :   Length of the number, i.e., total number of digits
                                 Member Methods
                                 Number(int a)            :   Initialises num = a and len to 0
                                 void countdigits()       :   Counts the number of digits in number num
                                 void print()             :   Prints the number and number of digits
                                 A derived class Armstrong is defined to check if a given number is an Armstrong number or
                                 not. A number is said to be Armstrong if sum of its digits raised to the power of length of the
                                 number is equal to the number itself. For example,
                                 371 = 3  + 7  + 1 3
                                        3
                                            3
                                             4
                                         4
                                 1634 = 1  + 6  + 3  + 4 4
                                                 4
                                              5
                                                  5
                                 54748 = 5  + 4  + 7  + 4  + 8 5
                                                      5
                                          5
                                 Thus, 371, 1634 and 54748 are all examples of Armstrong numbers.
                                 The details of the class is given below.
                                 Class name               :   Armstrong
                                 Data Members
                                 sum                      :    Stores sum of the digits raised to the power of the length of the
                                                              number
                                 Member Methods
                                 Armstrong(….)            :     Parameterised constructor to initialise the variables of the
                                                              base and derived classes
                                 void sumdigit()          :     Calculates the sum of the digits raised to the power of the
                                                              length of the number
                                 void print()             :     Prints the details of the number by calling print() of the base
                                                              class. Also checks and prints whether the number num is an
                                                              Armstrong number or not
                                 Define the above classes. Also write the main method to create object of the derived class
                                 and call the methods to implement the above.
                   1       class Number

                   2       { protected int num,len;
                   3         Number(int a) //base class constructor




                                                                                                                       455
                                                                                  Inheritance, Interfaces and Polymorphism  455
   452   453   454   455   456   457   458   459   460   461   462