170 likes | 287 Views
Announcements. Today:Polymorphism and exceptions in OOLCh 8. In Irvine.Project
E N D
1. Lecture 8: Polymorphism and Exceptions Dragan Mirkovic
Department of Computer Science
University of Houston
2. Announcements Today:
Polymorphism and exceptions in OOL
Ch 8. In Irvine.
Project #3 Dynamic memory allocation
Problem 7.8.6 pp. 259.
Due: 5/2/2005
Final exam: 5/10/2005 2 pm
3. Introduction Essential techniques in OOL:
Abstraction and encapsulation
Inheritance and polymorphism
Flexible class interfaces
Topics
Polymorphism
Virtual functions
Exceptions
Examples
4. Polymorphism Static and dynamic binding
In most programming languages and in most cases the variables are bound to a specific type at compile time
Static binding
Dynamic binding
The type of the variable can change at run time
Using pointers to classes related by inheritance in C++
In C we can use unions to allow dynamic binding of arbitrary types
Dangerous in some cases
5. C++ Polymorphism Definition:
Single name denotes objects of different classes related by inheritance
Ability to manipulate derived objects using the interface defined in the base class
Example:
class Employee {
public:
void CalcPay ();
};
class SalariedEmployee :public Employee{
public:
void CalcPay ();
};
6. Example
Employee *ep;
ep = new SalariedEmployee;
Ep->CalcPay();
Which function is called?
The function in Employee is called
To avoid that we have to declare CalcPay() as a virtual function
7. Virtual Functions Definition:
Nonstatic member function prefaced by the virtual specifier.
Compiler generates the code that will select the appropriate function at runtime
Example:
class Employee {
public:
virtual void CalcPay ();
};
class SalariedEmployee :public Employee{
public:
virtual void CalcPay ();
};
virtual specifier needs to appear in the base class only.
8. Examples Employee *p0 = new Employee;
Employee *p1 = new SalariedEmployee;
p0->CalcPay(); // calls Employee::CalcPay()
p1->CalcPay(); // calls SalariedEmployee::CalcPay()
Any nonstatic member function except a constructor can be virtual
Virtual functions can also be friends of other classes
Some compilers require the class destructor to be virtual if a class contains any virtual functions
9. Combinations of virtual and non-virtual functions Non-virtual function can call virtual function
Example:
class Employee {
public:
void Display() const;
virtual void CalcPay ();
};
class SalariedEmployee
:public Employee{
public:
void Display() const;
virtual void CalcPay ();
};
10. Example void Employee::Display()
{
CalcPay();
};
void SalariedEmployee::Display()
{
CalcPay();
};
Employee *p0 = new SalariedEmployee;
p0->Display();
// calls SalariedEmployee::CalcPay();
It works the other way too
11. Example class Employee {
public:
virtual void Display() const;
void CalcPay ();
};
class SalariedEmployee
:public Employee{
public:
virtual void Display() const;
void CalcPay ();
};
Employee *p0 = new SalariedEmployee;
p0->Display(); // calls SalariedEmployee::CalcPay();
12. Common Interface Virtual functions in the base class provide common interface for all of its derived classes
Example:
class Employee {
public:
long GetDepartment() const;
long GetId() const;
virtual void Display() const;
virtual void Input ();
virtual void CalcPay ();
private:
long id;
long deptNum;
};
13. Example class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b){ width=a; height=b; }
};
class CRectangle: public CPolygon {
public:
int area (void) { return (width * height); }
};
class CTriangle: public CPolygon {
public:
int area (void) { return (width * height / 2); }
};
14. Example (cont.) int main () {
CRectangle rect;
CTriangle trgl;
CPolygon * ppoly1 = ▭
CPolygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << rect.area() << endl;
cout << trgl.area() << endl; return 0;
}
15. Virtual Destructors Destructor preceded by the virtual specifier
Correct destructor must be called
It is recommended that any class containing virtual functions should also have a virtual destructor
Example
BookItem needs a separate destructor to deallocate the storage for title
Destructors are not inherited
16. Summary
17. Homework