Channels ▼
RSS

Open Source

A Better C?


Data Abstraction

Data abstraction is a programming technique in which you define general-purpose and special-purpose types as the basis for applications (see reference 6). These user-defined types are convenient for application programmers since they provide local referencing and data hiding. The result is easier debugging and maintenance and improved program organization.

In C++ , you can define types that you then can use as conveniently as, and in a manner similar to, built-in types. Common examples are arithmetic types such as rational and complex numbers.


class complex {
   double re, im;
public:
   complex(double r, double i)
      { re=r; im=i; }
   complex(double r)
      ( re=r; irn=O }
   // float->complex conversion
   friend complex
      operator+ (complex, complex};
   friend complex
      operator-(complex, complex};
   // binary minus
   friend complex
      operator-(complex};
   // unary minus
   friend complex
      operator* (complex. complex};
   friend complex
      operator/(complex. complex};
};

The declaration of class complex specifies the representation of a complex number and the set of operations on it. The keyword class is C++'s term for "user-defined" type. The declaration of class has two parts.

The initial part specifies the representation of a complex number and is by default private. This representation (consisting of the two double-precision floating-point numbers re and im) is accessible only to the functions defined in the declaration of class complex.

The second part of the declaration specifies how a user can create and manipulate complex numbers. It is called the public part of the declaration because it provides an interface to the general public. It consists of two constructors and the usual arithmetic operations. A constructor is a function that constructs a value of a given type. The first constructor for complex creates a complex number given a coordinate pair; the second creates a complex number given a single floating-point number (using the obvious mapping of the real line into the complex plane). Together they provide the two obvious ways of initializing a complex variable. For example:


complex a = complex(1.2);
// a becomes (1.2,0)
complex b = comp1ex(3.4,5.6);

The arithmetic operations are defined by friend functions: Specifically, these functions are completely ordinary except that they are granted access to the otherwise inaccessible representation of complex numbers by the friend declarations. The notation operator+ is used to name a function defining the addition operator, +. The number of arguments determines whether an operator function implements a binary or a unary operator. For example, operator-(complex,complex) defines subtraction of complex numbers, whereas operator-(complex) defines unary minus.

Such functions can be defined as:


complex operator+(complex al, complex a2)
{ 
   return complex(a1.re+a2.re,a1.im+a2.im);
}

and used like this:


{
main () {
   complex a = 2.3;
   complex b = complex(1/a.7); 
   complex c = a+b+complex(1,4.5);
}

Here, a receives the value (2.3,0) by implicit application of the constructor complex (double); b receives the value (1/2.3,7); and c becomes the value (2.3+1/2.3+1,7+4.5)--that is, about (3.7,11.5).

The constructors and the operator functions let you use complex numbers just as if they were built into the language. In-line functions let the run-time efficiency of a user-defined type come close to an equivalent built-in type.

Hiding the representation is the key to modularity. It allows the representation of a class to be changed without affecting users. For example, you might decide to change the Cartesian representation of complex used above to a polar one. Such a change would affect only the functions listed in the class definition. User code, such as main(), is unaffected. Debugging can also be greatly simplified by proper use of such data hiding.

Programming with classes shifts the emphasis from the design of algorithms to the design of classes (user-defined types). Each class is a direct representation of a concept in the program; each object the program manipulates is of some specific class that defines its behavior. In other words, every object in a program is of some class that defines the set of legal operations on that object. This lets you program in a language with a set of types, or concepts, appropriate to the application. An engineer might use complex numbers, matrices, and fast Fourier transforms, while the telephony-software designer might prefer types such as switch, line, trunk, handset, and digit buffer.

In C++ , this style of programming is supported by a general and flexible set of mechanisms for data hiding, by constructors providing optional guaranteed initialization, by destructors providing optional guaranteed cleanup (termination), and by operator overloading and user-defined coercions providing a convenient and conventional notation for many kinds of applications. All these features are cleanly integrated into the language, and all uses are checked for type violations and ambiguities at compile time to catch errors as early as possible and to avoid unnecessary run-time overheads.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video