22_inheritance - CSCI-1200 Data Structures Fall 2010...

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

View Full Document Right Arrow Icon
CSCI-1200 Data Structures — Fall 2010 Lecture 22 — C++ Inheritance and Polymorphism Review from Lecture 21 A hash table is implemented with a array at the top level. Each key is mapped to a slot in the array by a hash function , a simple function of one argument (the key) which returns an index (a bucket or slot in the array). CallerID Performance: Vectors vs. Binary Search Trees vs. Hash Tables Hash Table Collision Resolution Using a Hash Table to Implement a set . Function objects, Iterators, Fundamental operations: find, insert and erase. Today’s Class Basic mechanisms of inheritance Types of inheritance Is-A, Has-A, As-A relationships among classes. Polymorphism 22.1 Motivating Example: Bank Accounts Consider different types of bank accounts: Savings accounts Checking accounts Time withdrawal accounts (like savings accounts, except that only the interest can be withdrawn) If you were designing C++ classes to represent each of these, what member functions might be repeated among the different classes? What member functions would be unique to a given class? To avoid repeating common member functions and member variables, we will create a class hierarchy , where the common members are placed in a base class and specialized members are placed in derived classes . 22.2 Accounts Hierarchy Account is the base class of the hierarchy. SavingsAccount is a derived class from Account . SavingsAccount The member variable balance in base class Account is protected , which means: balance is NOT publicly accessible outside the class, but it is accessible in the derived classes. if balance was declared as private , then SavingsAccount member functions could not access it. When using objects of type SavingsAccount , the inherited and derived members are treated exactly the same and are not distinguishable. CheckingAccount is also a derived class from base class Account . TimeAccount is derived from SavingsAccount . SavingsAccount is its base class and Account is its indirect base class. 22.3 Exercise: Draw the Accounts Class Hierarchy
Background image of page 1

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

View Full Document Right Arrow Icon
#include <iostream> using namespace std; // Note we’ve inlined all the functions (even though some are > 1 line of code) // ------------------------------------------------------------------------------- class Account { public: Account(double bal = 0.0) : balance(bal) {} void deposit(double amt) { balance += amt; } double get_balance() const { return balance; } protected: double balance; // account balance }; // ------------------------------------------------------------------------------- class SavingsAccount : public Account { public: SavingsAccount(double bal = 0.0, double pct = 5.0) : Account(bal), rate(pct/100.0) {} double compound() { // computes and deposits interest
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 6

22_inheritance - CSCI-1200 Data Structures Fall 2010...

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

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