![]() |
![]() |
|
![]() |
![]() |
Encyclopedia :
C :
CP :
CPL :
C Plus Plus |
|
|
C Plus PlusC++ (pronounced "see plus plus") is a general-purpose computer programming language. It is a statically typed free-form multi-paradigm language supporting procedural programming, data abstraction, object-oriented programming, and generic programming. During the 1990s, C++ became one of the most popular commercial programming languages. Bell Labs' Bjarne Stroustrup developed C++ (originally named "C with Classeses") during the 1980s as an enhancement to the C programming language. Enhancements started with the addition of classes, followed by, among many features, virtual functions, operator overloading, multiple inheritance, templates, and exception handling. The C++ programming language standard was ratified in 1998 as ISO/IEC 14882:1998, the current version of which is the 2003 version, ISO/IEC 14882:2003. In C and C++, the expression x++ increases the value of x by 1. The name "C++" is a play on this, suggesting an improvement upon C. Technical overviewThe 1998 C++ Standard consists of two parts: the Core Language and the Standard Library; the latter includes most of the Standard Template Library and a slightly modified version of the C standard library. Many C++ libraries exist which are not part of the Standard, such as Boost. Also, non-Standard libraries written in C can generally be used by C++ programs. It is important to realize that, officially, there is no longer a single language called "C++"; the term denotes a family of related languages, each usually a slightly incompatible superset of a previously defined one. Early members of this family used to be identified by version number, later members are distinguished by the year in which that specific language has been standardized. Features introduced in C++ Features introduced in C++ include declarations as statements, function-like casts, C++ also performs more type checking than C in several cases. Comments starting with two slashes ("//") were originally part of C's predecessor, BCPL, and were reintroduced in C++. Several features of C++ were later adopted by C, including A very common source of confusion is a subtle terminology issue: because of its derivation from C, in C++ the term object means memory area, just like in C, and not class instance, which is what it means in most other object oriented languages. For example in both C and C++ the line int i;
defines an object of type int, that is the memory area where the value of the variable i will be stored on assignment. C++ library The C++ standard library incorporates the C standard library with some small modifications to make it work better with the C++ language. Another large part of the C++ library is based on the Standard Template Library (STL). This provides such useful tools as containers (for example vectors and lists) and iterators (generalized pointers) to provide these containers with array-like access. Furthermore (multi)maps (associative arrays) and (multi)sets are provided, all of which export compatible interfaces. Therefore it is possible, using templates, to write generic algorithms that work with any container or on any sequence defined by iterators. As in C, the features of the library are accessed by using the The STL was originally a third-party library from HP and later SGI, before its incorporation into the C++ standard. The standard does not refer to it as "STL", as it is merely a part of the standard library, but many people still use that term to distinguish it from the rest of the library (input/output streams A project known as STLPort, based on the SGI STL, maintains an Object-oriented features of C++ C++ introduces some object-oriented features to C. It offers classes which provide the four features commonly present in OO (and some non OO) languages: abstraction, encapsulation, polymorphism, and inheritance. EncapsulationC++ implements encapsulation by allowing all members of a class to be declared as either public, private, or protected. A public member of the class will be accessible to any function. A private member will only be accessible to functions that are members of that class and to functions and classes explicitly granted access permission by the class ("friends"). A protected member will be accessible to members of classes that inherit from the class in addition to the class itself and any friends. The OO principle is that all and only the functions that can access the internal representation of a type should be encapsulated within the type definition. C++ supports this (via member functions and friend functions), but does not enforce it: the programmer can declare parts or all of the representation of a type to be public, and is also allowed to make public entities that are not part of the representation of the type. Because of this C++ supports not just OO programming but other weaker decomposition paradigms, like modular programming. It is generally considered good practice to make all data private, or at least protected, and to make public only those functions that are part of a minimal interface for users of the class that hides implementation details. PolymorphismPolymorphism is a widely used and abused term that is not well defined. In the case of C++ it is often used in connection with member function names, where the function name corresponds to several implementations, and which implementation gets invoked depends either on the type of the arguments (static polymorphism) or on the type of the class instance value (dynamic polymorphism) used on which the virtual member function is invoked. For example, a C++ program may contain something like this: /* Static polymorphism */
extern void SendJobToDevice(PrintJobText *,DeviceLaser *);
extern void SendJobToDevice(PrintJobText *,DeviceJet *);
extern void SendJobToDevice(PrintJobHTML *,DeviceLaser *);
extern void SendJobToDevice(PrintJobHTML *,DeviceJet *);
....
sendJobToDevice(printJob,device);
/* Dynamic polymorphism */
PrintJobText *printJob; Device *device;
....
device->print(printJob);
In C, (dynamic) polymorphism of a sort can be achieved using the C++ provides two more sophisticated features for polymorphism: function overloading and virtual member functions. Both features allow a program to define several different implementations of a function for use with different types of objects. Function overloading allows programs to declare multiple functions with the same name. The functions are distinguished by the number and types of their formal parameterss. For example, a program might contain the following three function declarations: void pageUser(int userid);
void pageUser(int userid, string message);
void pageUser(string username);
Three different Operator overloading is a form of function overloading. It is one of C++'s most controversial features. Many consider operator overloading to be widely misused, while others think it is a great tool for increasing expressiveness. An operator is one of the symbols defined in the C++ language, such as Integer& operator++();
then the program can use the Integer a = 2; ++a; behaves exactly like this: Integer a = 2;
a.operator++();
In most cases, this would then increment the value of the variable a to 3. However, the programmer who created the Integer class can define the C++ templates make heavy use of static polymorphism, including overloaded operators. Virtual member functions provide a different type of polymorphism. In this case, different objects that share a common base class may all support an operation in different ways. For example, a virtual int getPageCount(double pageWidth, double pageHeight)
Each different type of print job, such as When a virtual member function of an object is called, the compiler sometimes doesn't know the type of the object at compile time and therefore can't determine which function to call. The decision is therefore put off until runtime. The compiler generates code to examine the object's type at runtime and determine which function to call. Because this determination is made on the fly, this is called dynamic polymorphism. The run-time determination and execution of a function is called dynamic dispatch. In C++, this is commonly done using virtual tables. InheritanceInheritance from a base class may be declared as public, protected, or private. This access specifier determines whether unrelated and derived classes can access the inherited public and protected members of the base class. Only public inheritance corresponds to what is usually meant by "inheritance". The other two forms are much less frequently used. If the access specifier is omitted, inheritance is assumed to be private for a class base and public for a struct base. Base classes may be declared as virtual; this is called virtual inheritance. Virtual inheritance ensures that only one instance of a base class exists in the inheritance graph, avoiding some of the ambiguity problems of multiple inheritance. Multiple inheritance is another controversial C++ feature. Multiple inheritance allows a class to derive from more than one base class; this can result in a complicated graph of inheritance relationships. For example, a "Flying Cat" class can inherit from both "Cat" and "Flying Mammal". Some other languages, such as Java, accomplish something similar by allowing inheritance of multiple interfaces while restricting the number of base classes to one (interfaces, unlike classes, provide no implementation). Design of C++In The Design and Evolution of C++ ISBN 0-201-54330-3, Bjarne Stroustrup describes some rules that he uses for the design of C++. Knowing the rules helps to understand why C++ is the way it is. The following is a summary of the rules. Much more details can be found in The Design and Evolution of C++.
As Stroustrup designed C with Classes (later C++), he also wrote Cfront, a compiler that generates C source codes from C with Classes source codes. The first commercial release occurred in October 1985. In 1983, the name of the language was changed from C with Classes to C++. New features that were added to the language included virtual functions, function name and operator overloading, references, constants, user-controlled free-store memory control, improved type checking, and new comment style (//). In 1985, the first edition of The C++ Programming Language was released, providing an important reference to the language, as there was not yet an official standard. In 1989, Release 2.0 of C++ was released. New features included multiple inheritance, abstract classes, static member functions, const member functions, and protected members. In 1990, The Annotated C++ Reference Manual was released and provided the basis for the future standard. Late addition of features included templates, exceptions, namespaces, new casts, and a Boolean type. As the C++ language evolved, a standard library also evolved with it. The first addition to the C++ standard library was the stream I/O library which provided facilities to replace the traditional C functions such as printf and scanf. Later, among the most significant additions to the standard library, was the Standard Template Library. After years of work, a joint ANSI-ISO committee standardized C++ in 1998 (ISO/IEC 14882:1998). For some years after the official release of the standard in 1998, the committee processed defect reports, and published a corrected version of the C++ standard in 2003. No one owns the C++ language; it is royalty-free. The standard document itself is, however, not available for free. Future developmentC++ continues to evolve to meet future requirements. One group in particular works to make the most of C++ in its current form and advise the C++ standards committee which features work well and which need improving: Boost.org. Current work indicates that C++ will capitalize on its multi-paradigm nature more and more. The work at Boost.org, for example, is greatly expanding C++'s functional and metaprogramming capabilities. The C++ standard does not cover implementation of name decoration, exception handling, and other implementation-specific features, making object code produced by different compilers incompatible; there are, however, 3rd-party standards for particular machines or OSs which attempt to standardise compilers on those platforms, for example [1]. C++ compilers still struggle to support the entire C++ standard, especially in the area of templates — a part of the language that was more-or-less entirely conceived by the standards committee. One particular point of contention is the export keyword, intended to allow template definitions to be separated from their declarations. The first compiler to implement export was Comeau C++, in early 2003 (5 years after the release of the standard); in 2004, beta compiler of Borland C++ Builder X was also released with export. Both of these compilers are based on the EDG C++ frontend. It should also be noted that many C++ books provide example code for implementing the keyword export (Ivor Horton's Beginning ANSI C++, pg. 827) which will not compile, but there is no reference to the problem with the keyword export mentioned. Other compilers such as Microsoft Visual C++ and GCC do not support it at all. Herb Sutter, secretary of the C++ standards committee, has recommended that export be removed from future versions of the C++ standard Other template issues include constructions such as partial template specialisation, which was poorly supported for several years after the C++ standard was released. History of the name "C++"This name is credited to Rick Mascitti (mid-1983) and was first used in December 1983. Earlier, during the research period, the developing language had been referred to as "C with Classeses". The final name stems from C's "++" operator (which increments the value of a variable) and a common naming convention of using "+" to indicate an enhanced computer program, for example: "Wikipedia+". According to Stroustrup: "the name signifies the evolutionary nature of the changes from C". C+ had earlier named an unrelated program. Some C programmers have noted that if the statements C++ is not a superset of CWhile most C code is also valid C++, C++ does not form a superset of C: there exists valid C code that is not valid C++. This is in contrast to Objective-C, another extension of C to support object-oriented programming, which is a superset of C. Furthermore, code that is both valid C and valid C++ may produce different results depending upon whether it is compiled as C or C++. For example, the following program prints "C" when compiled with a C compiler but prints "C++" when compiled with a C++ compiler. This is because in C the type of a character literal (such as 'a') is int, whereas in C++ it is char. #include See this section of the C article for more details. int main()
{
return 0;
}
The C++ Standard requires that main() returns type int. A program which uses any other return type for main() is not Standard C++. The Standard does not say what the return value of main() actually means. Traditionally, it is interpreted as the return value of the program itself. The Standard guarantees that returning zero from main() indicates successful termination. Indicating unsuccessful termination from a C++ program is done by returning the EXIT_FAILURE constant, which is defined in the cstddef standard header. int main()
{
}
In C++, falling off of the end of main() is equivalent to return 0;. This is not true for any function other than main(). However, it is generally considered good practice to append return 0; at the end of main(). #include |
|
|
This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License. |
|
| © 2008 Chamas Enterprises Inc. |