C++ overview
Designed by [Bjarne Stroustrup], a Danish computer scientist, starting in 1979 at Bell Labs.
-
is a better C, is a direct descendant of C89
- supports data abstraction (C with Classes)
- adds keyword
class
, also keeps struct
for compatible
- A union is a class defined with the class-key
union
; it holds at most one data member at a time
- supports access control (compare with
struct
)
- Members of a class defined with the keyword
class
are private by default.
- Members of a class defined with the keywords
struct
or union
are public by default.
* constructors and destructors
* initialization
* Rule of three
- supports Object-oriented programming without extensions
- supports function overloading
calc integer sum: 1 + 2 = 3
calc float sum: 1.1 + 2.2 = 3.3
- supports generic programming through the use of templates
Template calc int sum: 1 + 2 = 3
Template calc double sum: 1.1 + 2.2 = 3.3
Template calc int sum: 1 + 2 = 3
- supports overloading of operators
-
Overloaded “&&” and “ |
|
” operators lose their short-circuit evaluation property. |
- extends the C89 standard library with C++ standard library
-
For compatibility with the C standard library, provides the 26 C headers:
-
C++ style head file, e.g.: #include <cstdio>
instead of #include <stdio.h>
#include <cstdio>
imports the symbol names in std::
namespace and possibly in Global namespace.
#include <stdio.h>
imports the symbol names in Global namespace and possibly in std::
namespace.
Sample for C++ style header:
- explicit cast operator (
static_cast, dynamic_cast, const_cast and reinterpret_cast
)
- keyword
bool
, comparison operators returns bool, while C returns int.
- sizeof
- introduce
reference
, also keeps the pointer
- similar to a pointer
- NO need to use a prefix
*
to access the value
- CANNOT be made to refer to a different object after its initialization
- CANNOT be NULL
x = 10 y = 20
pointer swap:
x = 20 y = 10
x = 10 y = 20
reference swap:
x = 20 y = 10
x = 10 y = 20
- introduce new keyword
namespace