lecture-7 - CPSC 223 Algorithms Data Abstract Structures...

Info icon This preview shows pages 1–6. Sign up to view the full content.

9/21/10 1 CPSC 223 Algorithms & Data Abstract Structures Lecture 7: Static and Dynamic Binding, Algorithm Analysis Intro Today … Quiz 4 Static vs. Dynamic Binding [Sect. 8.2] Intro to Complexity Analysis [Sect. 9.1] CPSC 223 -­૒-­૒ Fall 2010 2
Image of page 1

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

9/21/10 2 Methods and Inheritance What is method overloading ? Two functions in the same class have the same name But different arguments (types, number of args) E.g.: getStockValue() vs. getStockValue(double fee) What about with inheritance? Derived class methods with the same name as base class methods are hidden (regardless of args) CPSC 223 -­૒-­૒ Fall 2010 3 Method Hiding class Account { public: ... void deductFee(double fee); ... }; class Checking : public Account { public: ... void deductFee(); ... }; CPSC 223 -­૒-­૒ Fall 2010 4 If the client writes: Checking c; c.deductFee(10); What happens? A compile error!!! The method is hidden
Image of page 2
9/21/10 3 Access to Hidden Members Base class methods can still be accessed though … From within a derived class using scope resolution BaseClassName::functionName(...) From within a client object.BaseClassName::functionName(...) CPSC 223 -­૒-­૒ Fall 2010 5 Accessing Hidden Members Suppose checking balances are less the monthly fee … How can we implement CheckingAccount::getBalance() CPSC 223 -­૒-­૒ Fall 2010 6 // base class class Account { public: Account(double bal = 0); double getBalance(); private: double balance; }; double Account::getBalance() { return balance; } // derived class class Checking : public Account { public: Checking(double b = 0, double f = 5); double getBalance(); double getMonthlyFees(); private: double monthlyFees; }; double Checking::getBalance() { return Account::getBalance() – getMonthlyFees(); }
Image of page 3

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

9/21/10 4 Static Binding Static binding is when a method is “bound” to an object based on its static type the static type is the declared type A method statically bound to an object can be a method of the object’s class or be an inherited method of a superclass Static binding is also called compile time binding CPSC 223 -­૒-­૒ Fall 2010 7 Static Binding Whenever we need a base class object, we can use a derived class object in its place … CPSC 223 -­૒-­૒ Fall 2010 8 Account Checking Account::getBalance() balance = 500.00 Checking::getMonthlyFees() Checking::getBalance() monthyFees = 10.00 Checking object Account object (part of the checking account object)
Image of page 4
9/21/10 5 Static Binding When the checking object is assigned to an account object, we lose the derived parts
Image of page 5

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

Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern