Page 459 - Computer science 868 Class 12
P. 459

39          ob.sumdigit();
                   40          ob.print();

                   41       }
                   42      }

                 The output of the preceding program is as follows:






















                   Program 4     A base class Distance is defined as follows:
                                 Class name               :   Distance
                                 Data Members
                                 double km                :   To store distance travelled in kilometer
                                 Member Methods
                                 Distance (double k)      :   Parameterised constructor to initialise km to k
                                 void show()              :   Prints distance in kilometer
                                 The derived class Fare is defined to calculate fare as per condition given below.
                                 Distance travelled in km   :   Fare
                                 Up to 3 Km               :   ` 20
                                 Above 3 Km               :   ` 5 for every three  kilometer or fraction there of
                                 The details of class Fare is given below.
                                 Class name               :   Fare
                                 Data Members
                                 double f                 :   To store fare to be paid for the journey
                                 Member Methods
                                 Fare(double k)           :     Parameterised constructor to initialise data members of the
                                                              base and derived classes
                                 void  calFare( )         :     Calculates the amount to be paid by the passengers as per
                                                              rate given above
                                 void show()              :   Prints distance in kilometer and fare to be paid
                                 Define the above classes. Also, write the main method to create object of the derived class
                                 and the methods to implement the above.

                   1       class Distance
                   2       { protected double km;

                   3         Distance(double k) // base class constructor

                   4         { km=k;}



                                                                                                                       457
                                                                                  Inheritance, Interfaces and Polymorphism  457
   454   455   456   457   458   459   460   461   462   463   464