OOPortal OOPortal


OO Encapsulation  «Prev  Next»

Adding user-defined Types to C++

Type extensibility is the ability to add user-defined types to C++ so that new types are as easy to use as native types.
An Abstract Data Type (ADT) is an extension to the native types available in C++. It consists of a set of values and a collection of operations that can act on those values. An ADT is a description of the ideal public behavior of the type. Example of a simple ADT string. The user of a string knows that operations, such as concatenate or print, result in certain public behavior. A concrete implementation of the ADT also has implementation limits, for example, strings might be limited in size. These limits affect public behavior.
Also, internal or private details of the implementation do not directly affect the user's understanding. For example, a string is frequently implemented as an array. The internal base address of this array and its name should be of no direct consequence to the user.
If we want to create a string type that differs from the string type already available in C++, we can easily do so. For example, we could create a variation on the string type that has a certain length limit and has the ability to print itself out backwards and capitalize every other letter.

A data type is called an abstract data type if the programmers who use the type do not have access to the details of how the values and operations are implemented. The predefined types such as int are abstract data types (ADTs). You do not know how the operations such as + and * are implemented for the type int. Even if you did know, you would not use this information in any C++ program. Programmer-defined types, such as the structure types and class types are not automatically ADTs. Unless they are defined and used with care, programmer-defined types can be used in unintuitive ways that make a program difficult to understand and difficult to modify. The best way to avoid these problems is to makesure all the data types that you define are ADTs. The way that you do this in C++ is to use classes, but not every class is an ADT. To make the class an ADT you must define the class in a certain way. Unless they are defined and used with care.