The development of a CreditCard class an introduction to object oriented design in C January 20 2006 Rather than explain the many aspects involved in a fully fledged yet complex example we will build up to such an example in a piecewise fashion Thus we start with a relatively simple program and add complexity as we go Version 1 In Figure 1 we give a self contained file which defines a CreditCard class and then shows a sample main routine which utilizes that class Already there are many aspects of this class definition which deserve comment Lines 12 14 are used to bring in some definitions from standard libraries to support basic input output functionality and a defined string class Though we might think that these basic needs would be part of the core language they are not Instead they are part of a very large extended library that is relatively standard across all installations of the language The official declaration of the CreditCard class begins at line 16 and continues up to the closing brace at line 45 Lines 18 21 formally declare the data members of this class including explicit type declarations e g string int double The next section is used to define the member functions Please note that there was no explicit requirement to declare the data members separately from the member functions they can be interspersed as we d like though certainly grouping them helps the legibility In our first version of the code we define only three member functions CreditCard chargeIt and makePayment Notice that the signatures of each method i e lines 25 33 42 follow a very specific general format explicitly declaring the type of any parameter as well as the type of return value For example the chargeIt method takes one parameter which is a double precision floating point number and in the end returns a boolean value A few special cases to note The makePayment body does not explicitly return anything In this case the return type is declared using a special keyword void which designates this routine as one which does not have any return value we informally describe this as a void return type The declaration of the CreditCard method is special in its own right This method serves as a constructor for the class Though it is common to list this first this is not actually a requirement The way that the compiler recognizes this as a constructor is because the method name is the same as the class name A second special feature of this signature versus 1 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 include i o s t r e a m include st ring using namespace s t d p r o v i d e s i n p u t o u t p u t c l a s s e s p r o v i d e s st ring make t h e i n c l u d e d d e f i n i t i o n s a c c e s s i b l e c l a s s C r ed itC ar d st ring number st ring name int limit double balance d a t a members c r e d i t c ard number c ard owner s name credit limit c r e d i t c ard b a l a n c e member f u n c t i o n s c o n s t r u c t o r C r ed itC ar d st ring no st ring nm int lim double b a l 0 number no name nm balance bal l i m i t lim u p d a t e methods bool c h a r g e I t double p r i c e i f p r i c e balance l i m i t return f a l s e else b a l a n c e p r i c e return true make a c h a r g e o v e r l i m i t t h e c h a r g e g o e s t h r o u g h void makePayment double payment b a l a n c e payment end o f Cre di tCard d e f i n i t i o n make a payment int main C r ed itC ar d d i s c o v e r 6011 1234 5678 9999 Mich ael 5 0 0 0 cou t c u r r e n t b a l a n c e i s d i s c o v e r b a l a n c e n discover chargeIt 400 00 cou t c u r r e n t b a l a n c e i s d i s c o v e r b a l a n c e n d i s c o v e r makePayment 1 0 0 0 0 cou t c u r r e n t b a l a n c e i s d i s c o v e r b a l a n c e n Figure 1 Excerpt from Version 1 of the CreditCard program CreditCard cpp 2 the standard methods is that there is nothing listed at all in the place where a return type specifier would normally be placed not even void is used In essence the constructor is generally used to initialize the state of a newly created instance There is no explicit return value in this context One other important point is to notice how the data members of an instance are accessed from within the body of the methods For example note the use of balance in the chargeIt method Recall that all identifiers must be formally declared yet this is not declared at least locally in the context of that method body If it cannot find such a local declaration it then looks to see if a data member exists with that name which it does in this case Since variables are not always explicitly declared in Python the way to distinguish between local variables and data members is by designating use of data members prefaced with an explicit self reference There actually is an implicit reference to the instance of an object in C as well designated by the implicit keyword this However you are not required to use this when accessing members of the instance Hopefully most of this example is self explanatory If we wish to run this code we must first compile it into an executable Since the entire program is in a single source file named CreditCard cpp we can execute the …
View Full Document