C++ Neural Networks and Fuzzy Logic C++ Neural Networks and Fuzzy Logic
by Valluru B. Rao
M&T Books, IDG Books Worldwide, Inc.
ISBN: 1558515526   Pub Date: 06/01/95
  

Previous Table of Contents Next


Chapter 2
C++ and Object Orientation

Introduction to C++

C++ is an object-oriented programming language built on the base of the C language. This chapter gives you a very brief introduction to C++, touching on many important aspects of C++, so you would be able to follow our presentations of the C++ implementations of neural network models and write your own C++ programs.

The C++ language is a superset of the C language. You could write C++ programs like C programs (a few of the programs in this book are like that), or you could take advantage of the object-oriented features of C++ to write object-oriented programs (like the backpropagation simulator of Chapter 7). What makes a programming language or programming methodology object oriented? Well, there are several indisputable pillars of object orientation. These features stand out more than any other as far as object orientation goes. They are encapsulation, data hiding, overloading, polymorphism, and the grand-daddy of them all: inheritance. Each of the pillars of object-orientation will be discussed in the coming sections, but before we tackle these, we need to answer the question, What does all this object-oriented stuff buy me ? By using the object-oriented features of C++, in conjunction with Object-Oriented Analysis and Design(OOAD), which is a methodology that fully utilizes object orientation, you can have well-packaged, reusable, extensible, and reliable programs and program segments. It’s beyond the scope of this book to discuss OOAD, but it’s recommended you read Booch or Rumbaugh to get more details on OOAD and how and why to change your programming style forever! See the reference section in the back of this book for more information on these readings. Now let’s get back to discussing the great object-oriented features of C++.

Encapsulation

In C++ you have the facility to encapsulate data and the operations that manipulate that data, in an appropriate object. This enables the use of these collections of data and function, called objects , in programs other than the program for which they were originally created. With objects, just as with the traditional concept of subroutines, you make functional blocks of code. You still have language-supported abstractions such as scope and separate compilation available. This is a rudimentary form of encapsulation. Objects carry encapsulation a step further. With objects, you define not only the way a function operates, or its implementation, but also the way an object can be accessed, or its interface. You can specify access differently for different entities. For example, you could make function do_operation() contained inside Object A accessible to Object B but not to Object C. This access qualification can also be used for data members inside an object. The encapsulation of data and the intended operations on them prevents the data from being subjected to operations not meant for them. This is what really makes objects reusable and portable! The operations are usually given in the form of functions operating upon the data items. Such functions are also called methods in some object-oriented programming languages. The data items and the functions that manipulate them are combined into a structure called a class. A class is an abstract data type. When you make an instance of a class, you have an object. This is no different than when you instantiate an integer type to create variables i and j. For example, you design a class called ElectronicBook, with a data element called ArrayofPages. When you instantiate your class you make objects of type ElectronicBook. Suppose that you create two of these called EB_Geography and EB_History. Every object that is instantiated has its own data member inside it, referred to by ArrayOfPages.

Data Hiding

Related to the idea of encapsulation is the concept of data hiding. Encapsulation hides the data from other classes and functions in other classes. Going back to the ElectronicBook class, you could define functions like GetNextPage, GetPreviousPage, and GetCurrentPage as the only means of accessing information in the ArrayofPages data member, by functions that access the ElectronicBook object. Although there may be a hundred and one other attributes and data elements in the class ElectronicBook, these are all hidden from view. This makes programs more reliable, since publishing a specific interface to an object prevents inadvertent access to data in ways that were not designed or accounted for. In C++, the access to an object, and its encapsulated data and functions is treated very carefully, by the use of keywords private, protected, and public. One has the opportunity to make access specifications for data objects and functions as being private, or protected, or public while defining a class. Only when the declaration is made as public do other functions and objects have access to the object and its components without question. On the other hand, if the declaration happens to be as private, there is no possibility of such access. When the declaration given is as protected, then the access to data and functions in a class by others is not as free as when it is public, nor as restricted as when it is private. You can declare one class as derived from another class, which will be discussed shortly. So-called derived classes and the declaring class do get the access to the components of the object that are declared protected. One class that is not a derived class of a second class can get access to data items and functions of the second class if it is declared as a friend class in the second. The three types of declarations of access specification can be different for different components of an object. For example, some of the data items could be declared public, some private, and the others protected. The same situation can occur with the functions in an object. When no explicit declaration is made, the default specification is as private.


Previous Table of Contents Next

Copyright © IDG Books Worldwide, Inc.