C++

Appeared in:
1985
Influenced by:
Influenced:
Paradigm:
Typing discipline:
File extensions:
.cpp .c++ .cc .h .hpp
Versions and implementations (Collapse all | Expand all):
Programming language

C++ (“C plus plus”) is a middle-level general-purpose programming language that supports multiple programming styles.

Language development started in 1979 under the name “C with classes”. It was renamed to “C++” later to emphasis both its origin from C and its superiority compared to it.

C++ was first released for commercial purposes in 1985, along with reference book called “The C++ Programming Language” which became its unofficial standard. C++ 2.0 came in 1989, followed by “The Annotated C++ Reference Manual” book. The official standard of the language, ISO/IEC 14882:1998, was published in 1998, and its corrected version, ISO/IEC 14882:1998, in 2003. Another important document is “Library Technical Report 1” (TR1 for short), published in 2005, which describes several extensions to the standard library. It is not a part of official standard, but is supported by most of currently maintained C++ compilers along with 1998 standard.

The official standard for the next version of the language, C++0x, is currently under development.

C++ was developed to enhance C with features which should facilitate large-scale software development while still keeping speed, portability and versatility of C.

In 1990th C++ has become, and still remains, one of the most popular programming languages ever seen. As a middle-level language, it is used both for system and application software development.

Language features:

  • C++ supports object-oriented programming through class construct. C++ classes provide all four features of OOP: abstraction, encapsulation, inheritance (including multiple) and polymorphism.
  • C++ supports generic programming via function and class templating.
  • C++ Standard Library includes Standard Template Library (STL) which provides lots of generic containters and algorithms and C standard library with some modifications.
  • virtual functions,
  • inline functions,
  • operator overloading,
  • function overloading,
  • exception handling etc.
  • C++ was designed to be as C-compatible as possible, so most codes written in C will compile as C++ codes.

Elements of syntax:

Inline comments //
Non-nestable comments /* ... */
Case-sensitivity yes
Variable identifier regexp [_a-zA-Z][_a-zA-Z0-9]*
Function identifier regexp [_a-zA-Z][_a-zA-Z0-9]*
Variable assignment varname = value
Variable declaration type varname
Variable declaration with assignment type varname = value
Grouping expressions ( ... )
Block { ... }
Physical (shallow) equality a == b
Physical (shallow) inequality a != b
Deep equality *a == *b (a and b are pointers)
Deep inequality *a != *b (a and b are pointers)
Comparison < > <= >=
Function definition returntype functionName (argtype1 argname1, ..., argtypeN argnameN)
Function call functionName(arg1, ..., argN)
Function call with no parameters functionName()
Sequence ;
If - then if (condition) trueBlock
If - then - else if (condition) trueBlock else falseBlock
Loop forever while (true) loopBody
While condition do while (condition) loopBody
Do until condition do loopBody while (!condition)
For each value in a numeric range, 1 increment for (int i = first; i <= last ; i++) loopBody
For each value in a numeric range, 1 decrement for (int i = last; i >= first; i--) loopBody

Examples:

Hello, World!:

Example for versions Borland C++ Builder 6, Microsoft Visual C++ 6, Microsoft Visual C++ 9 (2008), g++ 3.4.5
#include <iostream>

int main(void)
{
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Factorial:

Example for versions Borland C++ Builder 6, Microsoft Visual C++ 6, Microsoft Visual C++ 9 (2008), g++ 3.4.5

This example uses recursive factorial definition.

#include "stdio.h"

__int64 factorial(__int64 n)
{
	return ( n==0 ? 1 : n*factorial(n-1) );
}

int main(int argc, char* argv[])
{
	for (int n=0; n<=16; n++)
		printf( "%d! = %I64d\n", n, factorial(n) );
	return 0;
}

Factorial:

Example for versions Borland C++ Builder 6, g++ 3.4.5

This example uses recursive factorial definition.

#include <iostream>

unsigned long long factorial(unsigned long long n)
{
	if (n == 0) 
		return 1;
	else 
		return n * factorial (n - 1);
}

int main(void)
{
    for (int n = 0; n <= 16; n++) 
        std::cout << n << "! = " << factorial(n) << std::endl;
    return 0;
}

Fibonacci numbers:

Example for versions Borland C++ Builder 6, Microsoft Visual C++ 6, Microsoft Visual C++ 9 (2008), g++ 3.4.5

This example uses recursive definition of Fibonacci numbers.

#include <iostream>

int fibonacci(int n)
{
    return ( n<=2 ? 1 : fibonacci(n-1) + fibonacci(n-2) );
}

int main(void)
{
    for (int n=1; n<=16; n++)
        std::cout << fibonacci(n) << ", ";
    std::cout << "..." << std::endl;
    return 0;
}

Quadratic equation:

Example for versions Borland C++ Builder 6

This example uses a template class complex<>, provided in STL. All calculations are done in complex numbers, because this allows not to worry about the sign of discriminant and representation of the roots.

Operator >> of complex is overloaded so that it can recognize several formats of the number, so the input constants are read not as integers but as complex numbers without imaginary part. This implementation allows to solve quadratic equations not only with integer coefficients but also with floating-point and even complex ones.

Operator << is also overloaded to print the number x as (x.real(),x.imag()), so to print roots without imaginary part as a single double, function print was created.

#include <iostream>
#include <complex>
#include <math>

using namespace std;

void print(int ind, complex<double> x)
{   cout << "x" << ind << " = ";
    if (fabs(x.imag()) < 1E-6)
        cout << x.real() << endl;
    else cout << x << endl;
}

void main()
{   complex<double> A, B, C, D, x1, x2;
    cout << "A = ";
    cin >> A;
    if (abs(A)<1E-3)
    {   cout << "Not a quadratic equation" << endl;
        return;
    }
    cout << "B = ";
    cin >> B;
    cout << "C = ";
    cin >> C;

    A *= 2;
    D = B*B-A*C*2.0;
    if (abs(D)<1E-3)
        cout << "x = " << (-B/A).real();
    else
    {   print(1, (-B+sqrt(D))/A);
        print(2, (-B-sqrt(D))/A);
    }
}

Hello, World!:

Example for versions Turbo C++ 1.01, g++ 3.4.5, gcc 3.4.5, gcc 3.4.5 (Objective-C), gcc 4.2.4
#include <stdio.h>

int main()
{
    printf("Hello, World!\n");
    return 0;
}