Docsity

Inheritance and Polymorphism in Object-Oriented Programming: A Case Study on Bank Accounts, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
CSCI-1200 Computer Science II — Spring 2006
Lecture 24 — C++ Inheritance and Polymorphism
Review from Lecture 23
•cs2set operations: insert, destroy, printing, erase, tree height.
•Increment and decrement operations on iterators can be accomplished through use of parent pointers.
•Our tree implementation is limited because even though the average-case behavior is O(logn) for insert, erase,
and find operations, the worst-case can be as bad as O(n).
Today’s Class
•Inheritance is a relationship among classes. Examples: bank accounts, polygons, stack & list
•Basic mechanisms of inheritance
•Types of inheritance
•Is-A, Has-A, As-A relationships among classes.
•Polymorphism
24.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.
24.2 Accounts Hierarchy
•Account is the base class of the hierarchy.
•SavingsAccount is a derived class from Account.SavingsAccount has inherited member variables & functions
and ordinarily-defined member variables & functions.
•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.
24.3 Exercise: Draw the Accounts Class Hierarchy
pf3
pf4
pf5

Partial preview of the text

Download Inheritance and Polymorphism in Object-Oriented Programming: A Case Study on Bank Accounts and more Study notes Data Structures and Algorithms in PDF only on Docsity!

CSCI-1200 Computer Science II — Spring 2006

Lecture 24 — C++ Inheritance and Polymorphism

Review from Lecture 23

  • cs2set operations: insert, destroy, printing, erase, tree height.
  • Increment and decrement operations on iterators can be accomplished through use of parent pointers.
  • Our tree implementation is limited because even though the average-case behavior is O(log n) for insert, erase, and find operations, the worst-case can be as bad as O(n).

Today’s Class

  • Inheritance is a relationship among classes. Examples: bank accounts, polygons, stack & list
  • Basic mechanisms of inheritance
  • Types of inheritance
  • Is-A, Has-A, As-A relationships among classes.
  • Polymorphism

24.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.

24.2 Accounts Hierarchy

  • Account is the base class of the hierarchy.
  • SavingsAccount is a derived class from Account. SavingsAccount has inherited member variables & functions and ordinarily-defined member variables & functions.
  • 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.

24.3 Exercise: Draw the Accounts Class Hierarchy

#include 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 double interest = balance * rate; balance += interest; return interest; } double withdraw(double amt) { // if overdraft ==> return 0, else return amount if (amt > balance) { return 0.0; } else { balance -= amt; return amt; } }

protected: double rate; // periodic interest rate };

// ------------------------------------------------------------------------------- class CheckingAccount : public Account { public: CheckingAccount(double bal = 0.0, double lim = 500.0, double chg = 0.5) : Account(bal) { limit = lim; charge = chg; }

double cash_check(double amt) { assert (amt > 0); if (balance < limit && (amt + charge <= balance)) { balance -= amt + charge; return amt + charge; } else if (balance >= limit && amt <= balance) { balance -= amt; return amt; } else { return 0.0; } }

protected: double limit; // lower limit for free checking double charge; // per check charge };

24.7 Stack Inheriting from List

  • For another example of inheritance, let’s re-implement the stack class as a derived class of std::list:

template class stack : private std::list { public: stack() {} stack(stack const& other) : std::list(other) {} virtual ~stack() {} void push(T const& value) { this->push_back(value); } void pop() { this->pop_back(); } T const& top() const { return this->back(); } int size() { return std::list::size(); } bool empty() { return std::list::empty(); } };

  • Private inheritance hides the std::list member functions from the outside world. However, these member functions are still available to the member functions of the stack class.
  • Note: no member variables are defined — the only member variables needed are in the list class.
  • When the stack member function uses the same name as the base class (list) member function, the name of the base class followed by :: must be provided to indicate that the base class member function is to be used.
  • The copy constructor just uses the copy constructor of the base class, without any special designation because the stack object is a list object as well.

24.8 Is-A, Has-A, As-A Relationships Among Classes

  • When trying to determine the relationship between (hypothetical) classes C1 and C2, try to think of a logical relationship between them that can be written: - C1 is a C2, - C1 has a C2, or - C1 is implemented as a C
  • If writing “C1 is-a C2” is best, for example: “a savings account is an account”, then C1 should be a derived class (a subclass) of C2.
  • If writing “C1 has a C2” is best, for example: “a cylinder has a circle as its base”, then class C1 should have a member variable of type C2.
  • In the case of “C1 is implemented as a C2”, for example: “the stack is implemented as a list”, then C1 should be derived from C2, but with private inheritance. This is by far the least common case!

24.9 Exercise: 2D Geometric Primitives

Create a class hierarchy of geometric objects, such as: triangle, isosceles triangle, right triangle, quadrilateral, square, rhombus, kite, trapezoid, circle, ellipse, etc. How should this hierarchy be arranged? What member variables and member functions should be in each class?

24.10 Note: Multiple Inheritance

  • When sketching a class hierarchy for geometric objects, your may have wanted to specify relationships that were more complex... in particular some objects may wish to inherit from more than one base class.
  • This is called multiple inheritance and can make many implementation details significantly more hairy. Different programming languages offer different variations of multiple inheritance.

24.11 Introduction to Polymorphism

  • Let’s consider a small class hierarchy version of polygonal objects:

class Polygon { public: Polygon() {} virtual ~Polygon() {} int NumVerts() { return verts.size(); } virtual double Area() = 0; virtual bool IsSquare() { return false; } protected: vector verts; };

class Triangle : public Polygon { public: Triangle(Point pts[3]) { for (int i = 0; i < 3; i++) verts.push_back(pts[i]); } double Area(); };

class Quadrilateral : public Polygon { public: Quadrilateral(Point pts[4]) { for (int i = 0; i < 4; i++) verts.push_back(pts[i]); } double Area(); double LongerDiagonal(); bool IsSquare() { return (SidesEqual() && AnglesEqual()); } private: bool SidesEqual(); bool AnglesEqual(); };

  • Functions that are common, at least have a common interface, are in Polygon.
  • Some of these functions are marked virtual, which means that when they are redefined by a derived class, this new definition will be used, even for pointers to base class objects.
  • Some of these virtual functions, those whose declarations are followed by = 0 are pure virtual, which means they must be redefined in a derived class. - Any class that has pure virtual functions is called “abstract”. - Objects of abstract types may not be created — only pointers to these objects may be created.
  • Functions that are specific to a particular object type are declared in the derived class prototype.

24.12 A Polymorphic List of Polygon Objects

  • Now instead of two separate lists of polygon objects, we can create one “polymorphic” list:

std::list<Polygon*> polygons;

  • Objects are constructed using new and inserted into the list:

Polygon *p_ptr = new Triangle( .... ); polygons.push_back(p_ptr); p_ptr = new Quadrilateral( ... ); polygons.push_back(p_ptr); Triangle *t_ptr = new Triangle( .... ); polygons.push_back(t_ptr);

Note: We’ve used the same pointer variable (p ptr) to point to objects of two different types.