446 // Program #12: Bank Account - Bug
447 /////////////////////////////////////////
448
449 #include <iostream.h>
450
451 class Account
452 {
453 float mBalance; //balance of money
454
455 public:
456
457 void Init(void)
458 { mBalance=0; }
459
460
461 void Deposit(float amount)
462 {
463 mBalance = mBalance + amount;
464 }
465
466 void PrintBalance()
467 {
468 cout<<"Balance: $" << mBalance << endl;
469 }
470
471 void WithDrawal(float amount)
472 {
473 mBalance = mBalance - amount;
474 }
475
476 };
477
478
479
480 int main(void)
481 {
482 Account x;
483
484 // forgot to init the Account
485
486 x.Deposit(100.00);
487 x.Deposit(200.00);
488 x.WithDrawal(50.00);
489
490 x.PrintBalance();
491
492 return 0;
493 }
494
495
496
497
498 ////////////////////////////////////////
499 // Program #13: Bank Account - Constructor
500 /////////////////////////////////////////
501
502 #include <iostream.h>
503
504 class Account
505 {
506 float mBalance;
507
508 public:
509
510
511 Account() // Constructor
512 {
513 Init(); // Init the account
514 }
515
516
517 void Init(void)
518 { mBalance=0; }
519
520
521 void Deposit(float amount)
522 {
523 mBalance = mBalance + amount;
524 }
525
526 void PrintBalance()
527 {
528 cout<<"Balance: $" << mBalance << endl;
529 }
530
531 void WithDrawal(float amount)
532 {
533 mBalance = mBalance - amount;
534 }
535
536 };
537
538
539
540 int main(void)
541 {
542 Account x; // constructor initializes the account
543
544 x.Deposit(100.00);
545 x.Deposit(200.00);
546 x.WithDrawal(50.00);
547
548 x.PrintBalance();
549
550 return 0;
551 }
552
553
554
555 ////////////////////////////////////////
556 // Program #14: Bank Account - More Constructors
557 /////////////////////////////////////////
558
559 #include <iostream.h>
560
561 class Account
562 {
563 float mBalance;
564
565 public:
566
567
568 Account() // Default Constructor
569 {
570 mBalance=0;
571 }
572
573
574 Account(float init) // a float constructor
575 {
576 mBalance=init;
577 }
578
579 void Deposit(float amount)
580 {
581 mBalance = mBalance + amount;
582 }
583
584 void PrintBalance()
585 {
586 cout<<"Balance: $" << mBalance << endl;
587 }
588
589 void WithDrawal(float amount)
590 {
591 mBalance = mBalance - amount;
592 }
593
594 };
595
596
597
598
599
600 int main(void)
601 {
602 Account x; // x has 0 balance -> ie default Constructor
603 Account y(1000.0); // y has initial balance of 1000.00
604
605
606 x.PrintBalance();
607 y.PrintBalance();
608
609 return 0;
610 }
611
612
♥ ¸¸.•*¨*•♫♪♪♫•*¨*•.¸¸♥
http://comsci.liu.edu/~murali/cpp/ReviewSet2.txt
No comments:
Post a Comment