Introduction
Introduction
C++ is a statically typed, compiled, general-purpose, case-sensitive, irregular programming language that supports procedural programming, object-oriented programming and generic programming.
C++ is considered to be an intermediate language that combines the features of both high-level and low-level languages.
C++ was first designed and developed by Bjarne Stroustrup in 1979 at Bell Laboratories in Murray Hill, N.J. C++ further extends and refines the C language, originally named C with classes, and later renamed C++ in 1983.
C++ is a superset of C. In fact, any legal C program is a legal C++ program.
Note: Programming languages that use static types perform type checking at compile time, not at runtime.
Standardisation
Release Date | Common Name | Notes |
---|---|---|
2017 | C++17 | The fifth C++ standard |
2017 | coroutines TS | Concurrent library extensions |
2017 | ranges TS | Providing a scoping mechanism |
2017 | library fundamentals TS | Standard library extensions |
2016 | concurrency TS | Extensions for concurrent computation |
2015 | concepts TS | Concept libraries for optimizing compile-time information |
2015 | TM TS | Transactional memory operations |
2015 | parallelism TS | Extensions for parallel computing |
2015 | filesystem TS | File systems |
2014 | C++14 | The fourth C++ standard |
2011 | - | Decimal floating point extensions |
2011 | C++11 | The third C++ standard |
2010 | - | Mathematical function extensions |
2007 | C++TR1 | C++ Technical Report: Library Extensions |
2006 | - | C++ Performance Technical Report |
2003 | C++03 | The Second C++ Standard |
1998 | C++98 | The First C++ Standard |
Development history
- The history of the C++ programming language dates back to 1979, when Bjarne Stroustrup did some development for a PhD thesis. Of all the languages available to Stroustrup, one was known as Simula, which, as the name suggests, was probably a language designed primarily for simulation. the Simula 67 language, a variant used by Stroustrup, was considered the main language to support the object-oriented programming paradigm. stroustrup found this paradigm to be useful for packaging development Stroustrup found this paradigm useful for packaging development. However, the Simula language was too slow for practical and real-world use. Shortly afterwards, Bjarne Stroustrup wanted to enhance C by supporting object-oriented paradigms, and he delved into Smalltalk's OO implementation to get an idea of what to do with it. But he was not willing to give up performance for this, so he started work on "C with Classes", hoping that C++ code should run with similar (or better) performance than C code.
- In 1983, the name of the language was changed from "C with Classes" to C++, and the ++ operator in C, an operator for incrementing variables, gives you an insight into how Stroustrup saw the language. Many new features were added during this period, most notably virtual functions, function overloading, references with & signs, the const keyword and single line comments with two forward slashes.
- In 1985, Stroustrup published a book entitled *"The C++ Programming Language". In the same year, C++ was implemented as a commercial product. The language had not yet been formally standardised, making the book a very important reference. The language was updated again in 1989 to include protected and static members, as well as inheritance from multiple classes.
- In 1990, the C++ Reference Manual with Comments was released. That same year, Borland's Turbo C++ compiler would be released as a commercial product. turbo C++ added a number of other libraries that would have a considerable impact on the development of C++. Although the last stable release of Turbo C++ was in 2006, the compiler is still widely used.
- In 1998, the C++ Standards Committee published the first international standard for C++ ISO / IEC 14882:1998, informally known as C++ 98. It is said that the C++ Reference Manual with Comments had a great influence on the development of the standard. Also included was the Standard Template Library, which began conceptual development in 1979. in 2003 the committee responded to a number of issues reported in the 1998 standard and revised it accordingly. The changed language is called C++ 03.
- In 2005, the C++ Standards Committee published a technical report (called TR1) detailing the various features they planned to add to the latest C++ standard. The new standard is informally known as C++ 0x, as it is expected to be released sometime before the end of the first decade. Ironically, the new standard will not be released until mid-2011. Until then, several technical reports have been published and some compilers have started to add experimental support for the new features.
- Mid-2011 saw the completion of the new C++ standard, known as C++ 11. The Boost library project has had a significant impact on the new standard, with some of the new modules coming directly from the corresponding Boost library. Some of the new features include regular expression support, a comprehensive randomisation library, a new C++ time library, atomic support, a standard threading library, a new for loop syntax that provides functionality similar to foreach loops in some other languages, the auto keyword, new container classes, better support for union and array initialisation lists and variable parameter templates.
- 2014, C++ 14 (also known as C++ 1y) was released as a minor extension to C++11, featuring bug fixes and minor improvements, with a draft international standard ballot process completed in mid-August 2014, enhanced lambda functions, constexpr and type derivation features.
- 2017, release of the C17 standard, C17 offers many things. Enhancements to the core language and libraries.
- 2020, release of the C++20 standard, which introduces a number of weighty features, the more important of which are
- Concepts: Concepts change the way we think about and program templates. They are semantic classes of template parameters. They allow you to express your intentions directly in the type system. If something goes wrong, you will receive a clear error message.
- Ranges library: The new ranges library makes it possible to execute algorithms directly on containers, compose them in pipeline notation and apply them to infinite data streams.
- Coroutines: Asynchronous programming in C++ has gone mainstream thanks to concatenation. Coroutines are the basis for collaborative tasks, event loops, infinite data streams or pipelines.
- Modules: Modules overcome the limitations of header files. The separation of headers and source files became as obsolete as preprocessors. Finally, we have a faster build time and an easier way to build packages.
- Concurrency: Atomic Smart Pointers, Joining & Cancellable Threads, The C20 Synchronization Library, enhanced C++ concurrent programming capabilities.
Object-Oriented
C++ fully supports object-oriented programming, including the four main features of object-oriented development.
- Encapsulation
- abstraction
- Inheritance
- polymorphism
Standard library
The standard C++ consists of three important parts.
- The core language, which provides all the building blocks, including variables, data types, constants, etc.
- The C++ standard library, which provides a large number of functions for manipulating files, strings, etc.
- The Standard Template Library (STL), which provides a large number of methods for manipulating data structures, etc.
Instances
// Output the word Hello World
#include <iostream>
using namespace std;
// main() is where the program starts to execute
int main(){
// Output Hello World
cout << "Hello World";
return 0;
}
The C++ language defines a number of header files that contain information that is necessary or useful in a program. The above program contains the header file
line using namespace std; tells the compiler to use the std namespace. Namespaces are a relatively new concept in C++.
// main() is where the program starts executing is a one-line comment. A single line comment starts with // and ends at the end of the line.
int main() is the main function from which the program starts executing.
cout << "Hello World"; will display the message "Hello World" on the screen.
return 0; Terminates the main( ) function and returns the value 0 to the calling process.
g++ compiler
The program g++ is a special version of gcc with the default language set to C++, which automatically uses the C++ standard library instead of the C standard library when linking. It is possible to compile and link C++ programs with gcc by following the naming convention of the source code and specifying the names of the corresponding libraries, as shown in the following example.
gcc main.cpp -lstdc++ -o main
The following is the code for a simple C++ program saved in the file helloworld.cpp.
#include <iostream>using namespace std;int main(){
cout << "Hello, world!" << endl;
return 0;}
The simplest way to compile.
g++ helloworld.cpp
Since the filename of the executable is not specified on the command line, the compiler uses the default a.out. The program can be run as follows
. /a.outHello, world!
Usually we use the -o option to specify the filename of the executable, and the following example produces a helloworld executable.
g++ helloworld.cpp -o helloworld
Execute helloworld:
. /helloworldHello, world!
In the case of multiple C++ code files, such as runoob1.cpp, runoob2.cpp, the compile command is as follows
g++ runoob1.cpp cpp, runoob2.cpp -o runoob
Generate a runoob executable.
g++ Some systems use C++98 by default, we can specify C++11 to compile the main.cpp file as follows
g++ -g -Wall -std=c++11 main.cpp
Common command options
Options | Explanation |
---|---|
-ansi | Only the ANSI standard C syntax is supported. This option will disable certain features of GNU C, such as the asm or typeof keywords. |
-c | Compile and generate target files only. |
-DMACRO | Define the MACRO macro with the string "1". |
-DMACRO=DEFN | Defines the MACRO macro with the string "DEFN". |
-E | Runs the C precompiler only. |
-g | Generate debugging information that can be used by the GNU debugger. |
-IDIRECTORY | Specify additional header search paths for DIRECTORY. |
-LDIRECTORY | Specifies an additional library search path, DIRECTORY. |
-lLIBRARY | Searches for the specified library LIBRARY at connection time. |
-m486 | Code optimization for 486. |
-o | FILE Generate the specified output file. Used when generating executable files. |
-O0 | No optimisation. |
-O | or -O1 Optimize the generated code. |
-O2 | Further optimisation. |
-O3 | Further optimizations than -O2, including inline functions. |
-shared | Generate shared target files. Usually used when creating shared libraries. |
-static | Disable shared connections. |
-UMACRO | Undefine the MACRO macro. |
-w | Do not generate any warning messages. |
-Wall | Generate all warning messages. |