Sams Teach Yourself C in 21 Days

(singke) #1
Polymorphism, encapsulation, and inheritance are all a part of this listing. A class called
squareis defined in lines 5 to 12. The details of this class will be discussed on Bonus
Day 3. For now, notice that the declaration contains both functions (lines 7, 8, 10, and
11) and data (line 9). This enables all the features of a square to be encapsulated into one
place. You can see in lines 22 and 28 that there are two different ways of setting up the
squareclass. Inheritance is also illustrated in the listing. The cubeclass inherits from the
squareclass as you can see in line 15. Finally, you can see that reuse is also a feature of
this listing. The cube declaration reuses the square functionality. Additionally, the cube
and square declarations have been written to where they could easily be reused in other
listings. This will all be clearer after Bonus Day 3 when you cover C++ classes and
objects.

The Relationship of C++ to C ......................................................................

C++ is a superset of C. This means that all the features of C are available in C++. All the
features of C++ are not, however, available in C. If you look back at Listing B1.2, you
should be able to determine what most of the code does. Only a few of the constructs
will be new to you.
C++ was created in order to have a programming language that is easier to use with the
object-oriented constructs. Keywords such as classandtemplateare added to make
creating objects and reusable code easier. Additionally, keywords such as tryandcatch
are added to make finding and preventing errors easier. All these changes helped to cre-
ate a language that is much more powerful as well as reusable.

C++ Programs ..............................................................................................

Just like C, C++ is used for more than creating executable programs. The most common
types of programs created with C++ are presented in Table B1.1.

TABLEB1.1 C++ program types
Program Type Description
executables Programs that can be run by an operating system
libraries Routines created to link to other programs being created
dynamic linked libraries Routines created that can reside in memory and be linked to other
programs at runtime
controls Routines created that can be used in the creation of other pro-
grams

640 Bonus Day 1

36 448201x-Bonus1 8/13/02 11:18 AM Page 640

Free download pdf