GNU Free Documentation License . .

C++

( «++»)
: ,
C++
:

: -, , ,

:

:

1983

():

:

.c++ .cpp .cxx .cc .h++ .hpp .hxx .hh .h

:

:

GNU C++, Microsoft Visual C++, Intel C++ compiler, Clang, Comeau C/C++, Embarcadero (Borland) C++ Builder, Watcom C++ compiler, Digital Mars C++, Oracle Solaris Studio C++ compiler, Turbo C++

:

ISO/IEC 14882 C++

C++  .

, , , , , (), , - , , , , [1][2].   C- .[2] «C++» C, ++ .

,[3][4] C++ . , , , , , (, ). C++  , . GNU, Microsoft, Intel Embarcadero (Borland). C++ , Java C#.

C++ C. , C, C++,   , C++ C.

[]

[5]
BCPL 1966
( UNIX) 1969
( ) 1973
1979
C84 1984
Cfront ( E) 1984
Cfront ( 1.0) 1985
/ 1988
() 1991
ANSI C++ / ISO-C++ 1996
ISO/IEC 14882:1998 1998
ISO/IEC 14882:2003 2003
C++/CLI 2005
TR1 2005
C++11 2011

1980- , Bell Laboratories C . . 1998 C++: ISO/IEC 14882:1998 «Standard for the C++ Programming Language»; 2003     ISO/IEC 14882:2003.[6]

, «C », 1980 .[7] . , Simula , , . BCPL , . Bell Labs ( ). . , C ( BCPL) , . C, UNIX, Bell, , . . , ( - ) ( C). C ( ), , , inline- .

C ( C++), cfront  , C C. , , , .

1983 C C++. , , , , , , , (//). 1985 . 1985 « C++», , - . 1989 C++ 2.0. , , -, - .

1990 « C++», . , , , .

C++ . C++ /, C printf scanf. .

C++, . ( ) .

[]

1998 ISO/IEC 14882:1998 ( C++98),[8] C++ (ISO/IEC JTC1/SC22/WG21 working group).

2003 ISO/IEC 14882:2003, .

2005 «Library Technical Report 1» ( TR1). , , , , C++. TR1 C++.

2009 , C++99, C++0x,   C++11, , TR1.

[]

, , C ++ ( ). C+ , C , , . B, « C C».[7]

[] C++

« C++» , C++.[9] , C++ , . :

  • , C.
  • , , , - .
  • , .
  • C, C.
  • C C++: , , .
  • , .
  • , .
  • .

[]

C++ 2003 : .

, C++, . C++ C.

C++, , , . , , . - cfront. -. - , C++ .

[] -

, - (), , , .

[]

++ , C

/* 
 ,   
  
*/

,

//      

, // , , \ ( ??/), .

[]

C++ :

  • : char, wchar_t.
  • : signed char, short int, int, long int ( long long int, C++11).
  • : unsigned char, unsigned short int, unsigned int, unsigned long int( unsigned long long int, C++11).
  • : float, double, long double.
  • : bool, true false.

bool. if, while bool.[10]

. , void f(int &x) {x=3;} 3. , . , {double &b=a[3]; b=sin(b);} a[3]=sin(a[3]);. , : ; ; , , . .   , , , ;   , , .

[]

  • inline inline-. , , inline . inline- , , , . inline-, , , , inline. , inline- , ( inline- , ), , inline, . :

inline double Sqr(double x) {return x*x;}.

  • volatile , , . , volatile, , (, ) .
  • , , (union) (enum), , :
struct Time {
    int hh, mm, ss;
};
Time t1, t2;
  • (namespace). ,
namespace Foo
{
   const int x=5;
   typedef int** T;
   void f(int y) {return y*x};
   double g(T);
   ...
}

T, x, f, g Foo::T, Foo::x, Foo::f Foo::g . - ,

using namespace Foo;

using Foo::T;

, , , .

namespace
{
    ...
}

, , .

  • . , void f(int x, int y=5, int z=10), f(1), f(1,5) f(1,5,10) .
  • , C, , , . , , , int printf(const char* fmt, ...).
  • , typedef, , . , :
struct S
{
    typedef int** T;
    T x;
};
S::T y;
  • , ( ; ). , :
void Print(int x);
void Print(double x);
void Print(int x, int y);
  • . , :
struct Date {int day, month, year;};
void operator ++(struct Date& date);

() . new, new[], delete delete[], (, int); , C++ (, **); , , (, a+b*c , , a, b c). [] ( ) () ( ).

  • (template). , template<class T> T Min(T x, T y) {return x<y?x:y;} Min . , . , template<class T> struct Array{int len; T* val;}; , Array<float> x;
  • malloc free operator new, operator new[], operator delete operator delete[], new, new[], delete delete[]. T  , , X  A  n , X,
    • new T ( operator new), , , , * (, * p = new T).
    • new X[n] new A ( operator new[]), n X, , , X* (, X* p = new X[n]).
    • delete p  ( ), p, ( operator delete), new-.
    • delete [] p  , p, ( operator delete[]), new-.

delete , NULL, . non-POD new- ; delete- (. ).

[] -

C++ C - . , : , .

C++ (class) , class, struct union, (structure) , struct, (union) , union.

[]

, (. Alloc . (inline))

[] -

- ( ) const

class Array
{
...
    inline double operator[] (int n) const;

( , mutable). , .

[]

C++ , - . .

, -, . .

, ( ):

/ private- protected- public-
private- private private
protected- protected protected
public- protected public

  , , , , , .

[]

- , :

class Figure
{
    ...
    void Draw() const;
    ...
};
 
class Square : public Figure
{
    ...
    void Draw() const;
    ...
};
 
class Circle : public Figure
{
    ...
    void Draw() const;
    ...
};

  Figure::Draw(), Square::Draw() Circle::Draw()  . :

Circle *c = new Circle(0,0,5);
Figure *f = c; //  ok: Figure     Circle
c->Draw();
f->Draw(); //    ,   f    ,   c

, Circle, c Circle::Draw(), f  Figure::Draw(), f  Figure. .

C++ , . - .

class Figure
{
    ...
    virtual void Draw() const;
    ...
};
 
class Square : public Figure
{
    ...
    void Draw() const;
    ...
};
 
class Circle : public Figure
{
    ...
    void Draw() const;
    ...
};
 
Figure * figures[10];
figures[0] = new Square(1, 2, 10);
figures[1] = new Circle(3, 5, 8);
...
for (int i = 0; i < 10; i++)
    figures[i]->Draw();

Square::Draw() Circle::Draw() .

-, = 0:

class Figure
{
    ...
    virtual void Draw() const = 0;
);

, , .

, -. . . , ++ -.

[]

C++ . (struct) C, , (class) C++ , - (member functions). ++ : (, public), (protected) (, , private). C++ , ,   .

private protected public

, .

, ( , !):

class Array
{
    public:
        Array() : 
            len(0),
            val(NULL)
        {}
 
        Array(int _len) :
            len(_len)
        {
            val = new double[_len];
        }
 
        Array(const Array & a);
 
        ~Array()
        {
            Free();
        }
 
        inline const double & Elem(int i) const
        {
            return val[i];
        }
 
        inline void ChangeElem(int i, double x)
        {
            val[i] = x;
        }
 
    protected:
        void Alloc(int _len)
        {
            if (len == 0)
                Free();
 
            len = _len;
            val = new double[len];
        }
 
        void Free()
        {
            delete [] val;
            len = 0;
        }
 
        int len;
        double * val;
};

Array 2 -, 2 , 3 . inline , , .

[]

-  , - . friend. :

class Matrix {
    ...
    friend Matrix Multiply(Matrix m1, Matrix m2);
    ...
};
 
Matrix Multiply(Matrix m1, Matrix m2) {
    ...
}

Multiply - Matrix.

-. A  B, ( ) - B. :

class Matrix {
    ...
    friend class Vector;
    ...
};

C++ «   ».

C++ ( ). C++0x . VC++, GNU C++ Comeau C++ , C++0x.

[]

  , .

( ) ,   . , .

(, Array::Array),   , (, Array::~Array). . . ( ), , ( ) .

, , , - (, Array::Array(const Array&)) ( ), ,  , , :

Array a(5); //  Array::Array(int)
Array b;    //  Array::Array()
Array c(a); //  Array::Array(const Array&)
Array d=a;  //  Array::Array(const Array&)
b=c;        //    =
            //     (   ),      , 
            //         -.
            //         

, . , . , .

[]

- :

class Array {
...
    inline double& operator[] (int n) { return val[n]; }

Array a(10);
...
double b = a[5];

[]

++ , ++, , , C [   617 ], C++[   617 ]. C++ C 1990 , C. C++ ( #include) . C++ 50 (, <cstddef>  , <new>   . .).

void* operator new(std::size_t) throw(std::bad_alloc);
void* operator new[](std::size_t) throw(std::bad_alloc);
void operator delete(void*) throw();
void operator delete[](void*) throw();

- .

[]

STL C++ ,   HP, SGI. «STL», , , ( / (iostream), C ).

STLport[11], SGI STL, STL, IOstream . .

[] ++ C

[] C

C++ C :

  • - ;
  • ;
  • ;
  • ;
  • ;
  • ;
  • ;
  • ;
  • ;
  • .

C++ , , new delete, bool, , , , , , , ( , , -, , ), , , ::, , . C++ , C.

C++ (//), C  BCPL.

C++ C, , const inline, for C++ (//). C , C++, va_arg -.

[] C++ C

, C C++, C++ C . C , C++. Objective C, C , C.

. , C++ main() , C . , C++ ; , , .

, , , , . , «», C, «C++»  C++. - , C (, 'a') int, C++  char, .

#include <stdio.h>
 
int main()
{
    printf("%s\n", (sizeof('a') == sizeof(char)) ? "C++" : "C");
    return 0;
}

[]

ISO/IEC 14882:2003(E) 2003 . C++03. C++11.

C++ , . , C++ C++   Boost. ,   .

C++ , , , , . .

[] export

export, .

, export , Comeau C++ 2003 ( 5 C++98). 2004 - Borland C++ Builder X .

- EDG. , Microsoft Visual C++ GCC (GCC 3.4.4), . , C++, export , . export: Microsoft Visual C++ 7.0, GCC 3.4.4, Microsoft Visual Studio 2010 .

, , , C++.

[] C++

      C++  Ubuntu GNU/Linux

[] № 1

, . . : main(), C++.

int main()
{
    return 0;
}

C++ , main() int. , main(), C++.

, main(). . , 0 main() , .

C++ .

[] № 2

, .

int main(){}

C++ ( C), main(), return 0;. main().

[] № 3

Hello World, , , .

#include <iostream> //    std::cout  std::endl     <<
 
int main()
{
    std::cout << "Hello, world!" << std::endl;
    return 0;
}

[] № 4

C++ . (STL).

#include <iostream>   //   std::cout
#include <vector>     //  std::vector<>
#include <map>        //  std::map<>  std::pair<>
#include <algorithm>  //  std::for_each()
#include <string>     //  std::string
 
using namespace std;  //    "std"
 
void display_item_count(pair < string const, vector<string> > const& person) {
   // person -    : person.first -   ,
   // person.second -     ( )
   cout << person.first << " is carrying " << person.second.size() << " items" << endl;
}
 
int main()
{
   //           
   map< string, vector<string> > items;
 
   //           
   items["Anya"].push_back("scarf");
   items["Dmitry"].push_back("tickets");
   items["Anya"].push_back("puppy");
 
   //     
   for_each(items.begin(), items.end(), display_item_count);
}

, , :

#include <vector>
 
int main()
{
    using std::vector;
 
    vector<int> my_vector;
}

, ( ). , , .[   819 ]

[] № 5

boost . . STL- .

#include <iostream>
#include <numeric>
#include <boost/iterator/counting_iterator.hpp>
#include <boost/iterator/transform_iterator.hpp>
 
int odd(int i)
{
  return 2 * i + 1;
}
 
int square(int i)
{
  return i * i;
}
 
typedef boost::counting_iterator <int> counter;
typedef boost::transform_iterator <int (*)(int), counter> transformer;
 
transformer odds(int n)
{
  return transformer(counter(n), odd);
}
 
transformer squares(int n)
{
  return transformer(counter(n), square);
}
 
int main()
{
  using namespace std;
 
  cout << "Enter vector length: ";
  int n; cin >> n;
 
  cout << inner_product( odds(0), odds(n), squares(0), 0 ) << endl;
}

«» . , STL , . , , ++, inner_product(), , . inner_product : « n».

[]

, , , , C++ , , , .

[]

C++  , , . :

  • , , , , (, ).
  • . , (, ), . , . .
  • , , . ( ) (, ,  . .), , (, ).
  • - .
  • (const) (mutable) . , , , . , , . - ( , ). mutable .
  • , , .
  • , . , Boost.Bind .
  • - . , Boost.Spirit, EBNF- C++.
  • - . Loki, SmartPtr , , 300 « » .
  • : . (, std::numeric_limits <T>). , C++ .
  • . , . , ,   .
  • , .
  • C, C- ( C C++; , C, C++ - , , , C, , ++).

[]

C++ . , :

  • , C, .
  • , .
  • , .
  • .

[]

  • =, ==. , , , , . :
    if (x=0) {  }
    
    . , , x , x , if. «», . , .
  • (=), (++), (--) . , , . C [   617 ], . , C C++  , «» .
  • [   617 ]. , .
  • ++ , C, ++. . , dynamic_cast . , grep. C .

[]

  • C++ break switch . Java[12]. , . , C# break, goto case N [13].
  • (#define) , . C++ , , , . C- [14].

[]

[]

[]

, ++ :

  • C , , C.
  • ++ . .
  • , ++ , , ++. ++ , , . , .

[] C++ Java

. :  (.) Comparison of Java and C++

C++ C, . , C++ , , C[?]. , , C++[   617 ].

C++ -, Java. C++, Java , - , , , . Microsoft C#, C++ , Java. Nemerle, C# . C++ Java C#  D, .

Java C++ - C, , , . (, Java, C# Nemerle, ).

 
C++ C, . Java C C++,   , . Java ++. , Java , , .
 
Java- , , C++ (, , C++ ). : Java , . Java , ( -) . Java , - , (   ).   .
 
C++ « » (RAII), (, std::vector std::ifstream). , , ( ,  . .), . Java , , , . , , RAII , . Java , , Java . , , , Java .
 
Java -, , , , . C++ . ,  . . , .
 
C++ . Java . , . , C++ ( , , ), . , Java .
 
++, Java - , . Java , static [23]. Java [24].
 
C++ RTTI . Java . C++, CTTI.
 
C++ , , , , , , [25]. , , , . . , C++ (, , ) , . Java , , .

, . , Java Java, C++ . C++, , , (, ).

[   617 ]. [   617 ] , Java C++ , . [   617 ], .

[] .

[]

  1. Herbert Schildt C++ The Complete Reference Third Edition.  Osborne McGraw-Hill.  ISBN 978-0078824760
  2. 1 2 . . 2.1. C++? // C++. . .  . 57.
  3. Programming Language Popularity (2009). 27 2012. 16 2009.
  4. TIOBE Programming Community Index (2009). 27 2012. 6 2009.
  5. , Pure C++: Hello, C++/CLI  (.)
  6. C++  Standards
  7. 1 2 . . 1.4. // C++. . .  . 46.
  8. Stroustrup, Bjarne C++ Glossary. 27 2012. 8 2007.
  9. . C++ = The Design and Evolution of C++.  .: , 2007.  445 .  ISBN 5-469-01217-4
  10. ISO/IEC 14882:1998, 6.4, 4: «The value of a condition that is an initialized declaration in a statement other than a switch statement is the value of the declared variable implicitly converted to bool The value of a condition that is an expression is the value of the expression, implicitly converted to bool for statements other than switch; if that conversion is ill-formed, the program is ill-formed».
  11. STLport: Welcome!
  12. The Java Tutorials: The switch Statement
  13. MSDN: The switch statement in C#
  14. , . // : C++ = Programming: Principles and Practice Using C++.  .: «. . », 2001.  . 1065, 1066, 1133.  1248 .  ISBN 978-5-8459-1621-1
  15. TIOBE Programming Community Index for January 2010
  16. Boehm-Demers-Weiser garbage collector for C and C++
  17. Dave Gottner. Templates Without Code Bloat // Dr. Dobb's Journal. 1995.
  18. Adrian Stone. Minimizing Code Bloat: Redundant Template Instantiation. Game Angst (22 2009). 27 2012. 19 2010.
  19. Herb Sutter. C++ Conformance Roundup // Dr. Dobb's Journal. 2001.
  20. Are there any compilers that implement all of this?. comp.std.c++ frequently asked questions / The C++ language. Comeau Computing (.) (10 2008). 27 2012. 19 2010.
  21. vanDooren. C++ keyword of the day: export. Blogs@MSMVPs (24 2008).  «The export keyword is a bit like the Higgs boson of C++. Theoretically it exists, it is described by the standard, and noone has seen it in the wild. There is 1 C++ compiler front-end in the world which actually supports it»  27 2012. 19 2010.
  22. Scott Meyers. Code Bloat due to Templates. comp.lang.c++.moderated. Usenet (16 2002). 19 2010.
  23. Class Arrays, JavaTM 2 Platform Std. Ed. v1.4.2
  24. The Java Tutorials. A Closer Look at the «Hello World!» Application
  25. From "C++ Template Metaprogramming, " by David Abrahams and Aleksey Gurtovoy. Copyright (c) 2005 by Pearson

[]

[]

, C++
,