0667-0670

Previous Table of Contents Next

Page 667

Listing 26.15. The use of virtual functions.

 class Transaction {     public:         virtual void Insert();         virtual void Update();         virtual void Delete();     protected:         int GetConnection(); }; class AddressHistoryTrans : public Transaction {     public:         int Insert(Address* last);         void Update(); /* Overrides base class */         void Delete(); /* Overrides base class */ }; class AddressTrans : public Transaction {     public:         int Insert(Address* last);         /* int Update(); ILLEGAL--only return type is different */         int Update(Address* last);         int Delete(unsigned long OldID); 

};

In Listing 26.15, the protected member function GetConnection is inherited normally by the derived classes. However, the functions declared as virtual must be redefined in the derived classes. In both derived classes, the Insert function is treated as overloaded because it differs in arguments and return type from the base class's Insert function. However, the redeclaration of Update and Delete in AddressHistoryTrans completely hides the base class implementations of these functions because they match exactly in terms of arguments and return type. Note that the arguments must change in the derived class to overload a base class function. It is illegal to change only the return type of a pure virtual function except when the base class function returns a pointer to the base class and the derived class function returns a pointer to the derived class. In this case, the compiler causes the derived class's function to override the base class's function.

Class objects can also inherit from multiple bases. For example, you can use the previously defined classes as bases to derive a Transaction class for the Car class, as illustrated in Listing 26.16.

Listing 26.16. An illustration of multiple inheritance and overriding virtual base functions.

 class CarTransaction : public Car, public Transaction {     public:                                 continues 

Page 668

Listing 26.16. continued

 virtual void Insert();         virtual void Update();         virtual void Delete();     protected:         void Commit();         void Rollback(); 

};

The class CarTransaction provides only two new member functions: Commit and Rollback. It inherits all the data and methods of the Car class, overrides Delete, inherits GetConnection, and overloads the Insert and Update member functions of the Transaction class. The technique of inheriting from multiple bases and overriding base class implementations in derived classes is often referred to as polymorphism, which combines many of the features of object-oriented programming.
Inheritance is an extremely powerful, yet dangerous, feature of object-oriented programming. Changes made to a base class are proliferated to all descendants that inherit from the base. This is powerful when you are fixing a bug in a base class or adding new data or methods that should apply to all descendants. However, you should always be careful when you make changes to base classes. If a bug is introduced in a base class member function, it affects every descendant that relies on the member function. Also, removing data and member functions from base classes is very difficult. When a data member is removed from a base class, it invalidates references to that data member in every member function of the base class and every member function of descendant classes where it is referenced.

Always remember that when you inherit from a class to derive a new one, all data members are inherited regardless of whether they are used. When you define base classes, think small and use inheritance carefully . As critics of C++ often point out, poor design of class hierarchies and overuse of inheritance results in bloated code.

Although C++ was used in this chapter to illustrate the concepts presented in this section, an increasing number of development tools are embracing the object-oriented model. The implementation of object-oriented features varies greatly among these tools, but the basic concepts are essentially the same. Visual Basic, PowerBuilder, SQLWindows, and more recently, Delphi, have all implemented the object-oriented paradigm with varying degrees of success. Regardless of which development tool you use, you need to understand the basic concepts of the object-oriented model.

Summary

Although Oracle8 is now an object-relational database supporting Java, JDBC, and JSQL (see Chapter 52, "Oracle Web Programming with Java and Perl"), the Oracle package provides the object-oriented capabilities of encapsulation, information hiding, and function overloading.

Page 669

Inheritance is not supported in Oracle8, but because Oracle seems to be moving in an object-oriented direction with the implementation of object caches, IDs, types, methods, and views (see Chapter 7, "Oracle8 Server"), you cannot rule it out as a possibility in future versions of Oracle8.

The newest of the Oracle development tools, including Power Objects and Oracle Objects for OLE, provide true object-oriented capabilities for developing Oracle client applications. However, the capabilities of the object model will not be fully exploited until there are more object-oriented features in the database itself. This will simplify the process of designing database applications and provide consistency by enabling a single object model to exist in both sides of the application.

Page 670

Previous Table of Contents Next


Oracle Unleashed
Oracle Development Unleashed (3rd Edition)
ISBN: 0672315750
EAN: 2147483647
Year: 1997
Pages: 391

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net