C Plus Plus
C++ (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 (ISO/IEC 14882-1998).
| Table of contents |
|
2 Object-oriented features of C++ 3 History of C++ 4 4 and y5 C++ Examples 6 See also 7 References 8 External links |
Technical Overview
The 1998 C++ Standard consists of two parts: the Core Language and the Standard Library; the latter includes the Standard Template Library and C's 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.
The C++ language is mainly a superset of C. Features introduced in C++ include declarations as statements, function-like casts,
C++ also performs more type checking than C in several cases.
// comments were originally part of C's predecessor, BCPL, and were reintroduced in C++.
Several features of C++ were later adopted by C, including
The C++ standard library mostly forms a superset of the C standard library. A large part of the C++ library comprises the Standard Template Library (STL). The STL provides such useful tools as iterators (which resemble high-level pointers) and containers (which resemble arrays that can automatically grow to include new elements). As in C, the features of the library are accessed by using the
C++ introduces object-oriented features to C. It offers classes which cover the three essential object-oriented necessities: encapsulation, polymorphism, and inheritance.
C++ fulfills the requirement of encapsulation by allowing all members of a class to be declared as either private, public, or protected. A public member of the class will be accessible to every other class. A protected member will only be accessible to the class in which it is in and the classes that inherit said class. A private member will only be accessible within that class. (Also, those functions declared as friends in the class will be able to access private members.) Obviously, it is possible to bypass encapsulation completely and declare all members of the class public, but this defeats the purpose of encapsulation which is that there are functions which will be able to control the modification of the data.
In general, this is what is generally thought of as "Good Practice." One should make all data in a class private and the functions public only if they will be accessed outside the class.
Polymorphism is the property of code that objects are treated differently based on their type. For example, a C++ program may contain this line of code:
In C, polymorphism of a sort can be achieved using the
Function overloading allows programs to declare multiple functions with the same name. The functions are distinguished by the number and types of their formal parameters. For example, a program might contain the following three function declarations:
Operator overloading is a form of function overloading. It is one of C++'s most controversial features. Many consider it to be widely misused. An operator is one of the symbols defined in the C++ language, such as
C++ templates make heavy use of static polymorphism, including overloaded operators.
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.
As stated earlier, the Inheritance used in C++ is one that allows multiple inheritance and so therefore has sparked much debate. Many newer languages, such as Java do not allow multiple inheritence. However, it is still a powerful feature in C++. This allows a base class to "be" as many classes as it is. For example, if you create a "Flying Cat" class it can both inherit a Cat and Flying Mammal. (Other languages, like Java, accomplish this by allowing interfaces instead of this multiple inheritance.) Also, in C++, a class must mention whether it will inherit the public members of the class or the protected members.
Stroustrup began work on C with Classes in 1979. The idea of creating a new language originated from Stroustrup's experience programming for his Ph.D. thesis. Stroustrup found that Simula had features that were very helpful for large software development but was too slow for practical uses, while BCPL was fast but too low level and unsuitable for large software development. When Stroustrup started working in Bell Labs, he had the problem of analyzing the UNIX kernel with respect to distributed computing. Remembering his Ph.D. experience, Stroustrup set out to enhance the C language with Simula-like features. C was chosen because it is general-purpose, fast and portable. At first, class (with data encapsulation), derived class, strong type checking, inlining, and default argument were features added to 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 1982, 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 yet. 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. 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).
C++ continues to evolve to meet future requirements. While compiler vendors still struggle to support all of C++'s features, the situation improved significantly from 1998 to 2003. 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. C++ still lacks a standard for name decoration, making object code produced by different compilers incompatible.
Some C programmers have noted that if the statements
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 traditionally done by returning a nonzero value. This is not quite correct from a language-lawyer standpoint, however.
Features introduced in C++
new/delete, bool, reference types, const, inline functions, default arguments, function overloading, namespaces, classes (including all class-related features such as inheritance, member functions, virtual functions, abstract classes, and constructors), bitfields, operator overloading, templates, the :: operator, exception handling, and run-time type identification.const, inline, declarations in for loops, and C++-style comments (using the // symbol). However, C99 also introduced
features that do not exist in C++, such as vararg macros and better handling of arrays as parameters.C++ Library
#include directive to include a standard header. C++ provides sixty-nine standard headers, of which nineteen are deprecated. A project known as STLPort has created a single set of public domain source code source for the STL. The binary images are available online for most platforms.Ownership of C++
Nobody owns C++. Stroustrup and AT&T receive no royalties for the usage of C++.Object-oriented features of C++
C++ Encapsulation
Polymorphism
sendPrintJob(device, printJob);
The print job could be an HTML document, the source code of a program, a photograph, or any number of other things. The device could be a printer, a fax machine, or something else. In each case, completely different code paths must execute based on the type of objects at hand. This is polymorphism at work.switch statement or function pointers. 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. void pageUser(int userid);
void pageUser(string username);
int pageUser(int userid, string message);
Three different pageUser() functions are declared. When the compiler afterwards encounters a call to pageUser(), it determines which function to call based on the number and type of the arguments provided. (The compiler considers only the parameters, not the return type.) Because the compiler determines which function to call at compile time, this is called static polymorphism. (The word static is used here in the sense of "not moving". It denotes that the determination is made based solely on static analysis of the source code: by reading it, not by running it. By the time the program executes, the decision has been made.)+, !=, <, or &. Much as function overloading allows the programmer to define different versions of a function for use with different argument types, operator overloading lets the programmer define different versions of an operator for use with different operand types. For example, if the class Integer contains a declaration like this: Integer operator++();
then the program can use the ++ operator with objects of type Integer. For example, the code Integer a = 2;
++a;
behaves exactly like this: Integer a = 2;
a.operator++();
A programmer using the Integer class would expect this to increment the value of the variable a to 3. However, the programmer who created the Integer class can define the Integer::operator++() member function to do whatever he wants. Because operators are commonly used implicitly, it is considered bad style to declare an operator except when its meaning is obvious, unambiguous, and non-tricky.PrintJob base class might contain a member function virtual int getPageCount(double pageWidth, double pageHeight). Each different type of print job may then override the method with a function that can calculate the appropriate number of pages for that type of job. In contrast with function overloading, the parameters for a given member function are always exactly the same number and type. Only the type of the object being called varies.C++ Inheritance
History of C++
Future Development
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. While most C code consists of valid C++, C does not form a subset of C++.x=3; and y=x++; are executed, then x and 4
y3; x is incremented after its value is assigned to y. However, if the second statement is y=++x;, then y=4 and x=4. Following such reasoning, a more proper name for C++ might actually be ++C. However, c++ and ++c both increment c, and, on its own line, the form c++ is more common than ++c. The pedantic may note that the introduction of C++ did not change the C language itself and the most accurate name might then be "C+1".C++ Examples
Note however, that the current ANSI C++ standard recommends that we specifically mention "using namespace std;" before referencing any standard objects. Also, it is recommended for good programming practice that header files specific to C++ be mentioned without the ".h" extension viz. "#include Example 1
This is an example of a program which does nothing. It begins executing and immediately terminates. It consists of one thing: a main() function. main() is the designated start of a C++ program.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++.Example 2
This program also does nothing, but is less verbose.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().Example 3
This is an example of a Hello world program, which displays a message and then terminates.#include
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}
Check out more C++ examples.See also
References
External links
Programming languages
Ada | Algol | APL | BASIC | C | C++ | C# | Cg | COBOL | Common Lisp | ColdFusion | Delphi | Eiffel | Forth | FORTRAN | Haskell | Java | JavaScript | Jython | Lisp | Logo | Mesa | ML | Modula-2 | Oberon | Objective-C | Objective Caml | Pascal | Perl | PHP | PL/I | PostScript | Powerbuilder | Prolog | Python | QBASIC | REXX | Ruby | Scheme | Smalltalk | Tcl/Tk | Visual Basic