Page 135 - DCAP404 _Object Oriented Programming
P. 135

Object-oriented Programming




                    Notes                 interest  (int  p,  int  n,  int  r  =  10);
                                          void  cal  (void);
                                   };
                                          interest::interest  (int  p,  int  n,  int  r  =  10)
                                   {  principal  =  p;  year  =  n;  rate  =  r;
                                   };
                                   void  interest::cal  (void)

                                   {
                                          cout<<  “Principal”  <<principal;
                                          cout  <<  “\  Rate”  <<rate;
                                          cout<<  “\  Year”  <<year;
                                          amount  =  (float)  (p*n*r)/100;
                                          cout<<  “\Amount”  <<amount;
                                   };

                                   main  (  )
                                   {
                                          interest  i1(1000,2);
                                          interest  i2(1000,  2,15);
                                          clrscr(  );
                                          il.cal();
                                          i2.cal();
                                   }





                                     Notes  The two objects created and initialized in the main( ) function.

                                   interest  i1(1000,2);
                                   interest  i2(1000,2,  15);
                                   The data members principal and year of object i1 are initialized to 1000 and 2 respectively at the
                                   time when object i1 is created. The data member rate takes the default value 10 whereas when
                                   the object 12 is created, principal, year and rate are initialized to 1000,2 and 15 respectively.
                                   It is necessary to distinguish between the default
                                   constructor::construct();
                                   and default argument constructor

                                   construct::construct(int  =  0)
                                   The default argument constructor can be called with one or no arguments. When it is invoked
                                   with no arguments it becomes a default constructor. But when both these forms are used in a
                                   class, it causes ambiguity for a declaration like construct C1;
                                   The ambiguity is whether to invoke construct: : construct ( ) or construct: : construct (int=O)



          128                               LOVELY PROFESSIONAL UNIVERSITY
   130   131   132   133   134   135   136   137   138   139   140