I.T. Discussion Community!
-Collapse +Expand
To/From Code
-Collapse +Expand Cross Ref Guide
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesDelphi  Print This     

Interface (Delphi and C++ Cross Reference Guide)

By Mike Prestwood

Delphi versus C++: A side by side comparison between Delphi and C++.

OOP Details

More object oriented (OO) stuff.


[Other Languages] 

An element of coding where you define a common set of properties and methods for use with the design of two or more classes.

Both interfaces and abstract classes are types of abstraction. With interfaces, like abstract classes, you cannot provide any implementation. However, unlike abstract classes, interfaces are not based on inheritance. You can apply an Interface to any class in your class tree. In a real sense, interfaces are a technique for designing horizontally in a class hierarchy (as opposed to inheritance where you design vertically). Using interfaces in your class design allows your system to evolve without breaking existing code.

Delphi:  "Interfaces" IInterface, TInterfacedObject

In Delphi, you use interfaces for both com objects and language interfaces and make use of IUnknown, IInterface, and/or TInterfacedObject.

For a pure language interface, add your specified proprieties, procedures, and functions to an interface that descends from IInterface (the base interface) as an interface, no implementation. Then have your implementing class inherit from TInterfacedObject and implement the interface.

For extending the VCL, you descend from the class you wish to extend, then implement an interface from IInterface and add the required functions QueryInterface, _AddRef, and _Release methods (refer to TInterfacedObject for an example).

For a com object, you descend from IUnknown. Descending from IUnknown instead of IInterface informs the Delphi compiler that the interface must be compatible with COM objects -- a Windows feature).

When defining an interface, define it in the type block just like you do for a class but you use the interface keyword instead of the class keyword and in the interfaces section only. Since interfaces, by definition, do not have any implementation details, all you do is specify it in the type block. Then implement in all classes that support the interface.

Syntax Example:
//Language interface:
//Interface section of unit.
IHuman = Interface(IInterface)
  //Specify interface methods and properties here.

TCyborg = class(TInterfacedObject)
TCyborgHuman = class(TCyborg, IHuman)
//Specify each here and implement in
//implementation section.
C++:  "Interfaces" No, but mimic it.

You can mimic an interface by using a class that has only pure-virtual functions and no member variables.

Follow PrestwoodBoards on: 

©1995-2020 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech