C++ Programming Guide

Chapter 1 Introduction

The Sun C++ compiler, CC, described in this book (and the companion book, C++ User's Guide) is available under the Solaris 2.5.1, 2.6, and Solaris 7 operating environments on the hardware platforms in SPARC and x86. Sun C++ 5.0 implements the language and libraries described in the C++ International Standard.

The C++ Language

C++ was first described in The C++ Programming Language by Bjarne Stroustrup, and later more formally described in The Annotated C++ Reference Manual, by Margaret Ellis and Bjarne Stroustrup. An international standard for C++ is now available.

C++ is designed as a superset of the C programming language. While retaining efficient low-level programming, C++ adds:

The support for object-oriented programming allows good design of modular and extensible interfaces among program modules. The standard library, including an extensible set of data types and algorithms, speeds the development of common applications.

Data Abstraction

C++ directly supports the use of programmer-defined data types that function much like the predefined data types already in the language. Such abstract data types can be defined to model the problem being solved.

Object-Oriented Features

The class, the fundamental unit of data abstraction in C++, contains data and defines operations on the data.

A class can build on one or more classes; this property is called inheritance, or derivation. The inherited class (or parent class) is called a base class in C++. It is known as a super class in other programming languages. The child class is called a derived class in C++. It is called a subclass in other programming languages. A derived class has all the data (and usually all the operations) of its base classes. It might add new data or replace operations from the base classes

A class hierarchy can be designed to replace a base class with a derived class. For example, a Window class could have, as a derived class, a ScrollingWindow class that has all the properties of the Window class, but also allows scrolling of its contents. The ScrollingWindow class can then be used wherever the Window class is expected. This substitution property is known as polymorphism (meaning "many forms").

A program is said to be object-oriented when it is designed with abstract data types that use inheritance and exhibit polymorphism.

Type Checking

A compiler, or interpreter, performs type checking when it ensures that operations are applied to data of the correct type. C++ has stronger type checking than C, though not as strong as that provided by Pascal, which always prohibits attempts to use data of the wrong type. The C++ compiler produces errors in some cases, but in others, it converts data to the correct type.

In addition to having the C++ compiler perform these automatic conversions, you can explicitly convert between types using type casts.

A related area involves overloaded function names. In C++, you can give any number of functions the same name. The compiler decides which function should be called by checking the types of the parameters to the function call. If the correct function is not clear at compile time, the compiler issues an "ambiguity" error.

Classes and Data Abstraction

If you are a C programmer, think of a class as an extension of the struct type. A struct contains predefined data types, for example, char or int, and might also contain other struct types. C++ allows a struct type to have not only data types to store data, but also operations to manipulate the data. The C++ keyword class is analogous to struct in C. As a matter of style, many programmers use struct to mean a C-compatible struct type, and class to mean a struct type that has C++ features not available in C.

C++ provides classes as a means for data abstraction. You decide what types (classes) you want for your program data and then decide what operations each type needs. In other words, a C++ class is a user-defined data type.

For example, if you define a class BigNum, which implements arithmetic for very large integers, you can define the + operator so that it has a meaning when used with objects in the class BigNum. If, in the following expression, n1 and n2 are objects of the type BigNum, then the expression has a value determined by your definition of + for BigNum.


n1 + n2

In the absence of an operator +() that you define, the + operation would not be allowed on a class type. The + operator is predefined only for the built-in numeric types such as int, long, or float. Operators with such extra definitions are called overloaded operators.

The data storage elements in a C++ class are called data members. The operations in a C++ class include both functions and overloaded, built-in operators (special kinds of functions). A class's functions can be member functions (declared as part of the class), or nonmember functions (declared outside the class). Member functions exist to operate on members of the class. Nonmember functions must be declared friend functions if they need to access private or protected members of the class directly.

You can specify the level of access for a class member using the public, private, and protected member access specifiers. Public members are available to all functions in the program. Private members are available only to member functions and friend functions of the class. Protected members are available only to members and friends of the base class and members and friends of derived classes. You can apply the same access specifiers to base classes, limiting access to all members of the affected base class.

Compatibility With C

C++ was designed to be highly compatible with C. C programmers can learn C++ at their own pace and incorporate features of the new language when it seems appropriate. C++ supplements what is good and useful about C. Most important, C++ retains C's efficient interface to the hardware of the computer, including types and operators that correspond directly to components of computing equipment.

C++ does have some important differences. An ordinary C program might not be accepted by the C++ compiler without some modifications. See the C++ Migration Guide for information about what you must know to move from programming in C to programming in C++.

The differences between C and C++ are most evident in the way you can design interfaces between program modules, but C++ retains all of C's facilities for designing such interfaces. You can, for example, link C++ modules to C modules, so you can use C libraries with C++ programs.

C++ differs from C in a number of other details. In C++: