08ClassRelationships - Chapter 8 Class Relationships Up to...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
 CS 240  Chapter 8 – Class  1Page 1 Chapter 8 Class Relationships Up to this point, the C++ classes that we’ve used have been little more  than elaborate structs. We’re now going to examine some of the features of C++ classes that  expand the language’s object-oriented capabilities: ü  Inheritance ü  Virtual Functions ü  Class Templates ü  Function Templates ü  Overloaded Operators
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
 CS 240  Chapter 8 – Class  2Page 2 class X: public A X’s public members X’s protected members X’s private members A’s public members A’s protected members class Y: protected A Y’s public members Y’s protected members Y’s private members A’s public members A’s protected members class Z: private A Z’s public members Z’s private members Z’s protected members A’s public members A’s protected members Inheritance When a new class is merely an  extension of an existing class, the  class may be defined as a  “derived” class, which “inherits”  from the existing class. class A A’s public members A’s protected members A’s private members
Background image of page 2
 CS 240  Chapter 8 – Class  3Page 3 //////////////////////////////////////////////// // Class definition file: Account.h // // // // In this header file, a class is defined to // // hold information about a bank Account. // // Notice that the only real piece of data // // inside an object of this class is a single // // floating-point number that represents the // // Account's current balance. Again, the // // idea of forming a class like this is to // // hide the information so that it can only // // be accessed through precisely specified // // means, to centralize those means so that // // modifying them at a later date can be sim- // // plified, and to make it easy to formulate // // slight variations of the Account class // // (e.g., a checking Account that accumulates // // interest, a savings Account, etc.). // //////////////////////////////////////////////// #ifndef ACCT_H #include <iostream> using namespace std; class Account{ public : // Class constructors Account(); Account( float initialBalance); Account( const Account &a); // Function members float deposit( float amount); float withdraw( float amount); float getBalance(); Account& operator = ( const Account &a); protected : // Data member float balance; }; #define ACCT_H #endif /////////////////////////////////////////////// // Class implementation file: Account.cpp // // // // The implementations of the member func- // // tions of the Account class are contained // // in this program file. // /////////////////////////////////////////////// #include "Account.h"   #include <iostream> using namespace std; // Default Constructor: Handles the creation // // of an Account with an unspecified initial // // balance. // Account::Account() { balance = 0.00F; } Inheritance Example #1: Bank Accounts
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
 CS 240  Chapter 8 – Class  4Page 4 // Initializing Constructor: Handles the // // creation of an Account with a specified // // initial balance. // Account::Account( float initialBalance) { balance = initialBalance; } // Copy Constructor: Handles the creation // // of an Account that is a duplicate of // // the parameterized Account. // Account::Account( const Account &a) { balance = a.balance; } // Deposit Member Function: Adds a depo- // // sited amt to the balance of an Account. // float Account::deposit( float amount) { balance = balance + amount; return
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 55

08ClassRelationships - Chapter 8 Class Relationships Up to...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online