Introduction to C++ - C++ Programming

1. What is C++?

  • C++ (pronounced "see plus plus") is a programming language began as an expanded version of C.
  • The C++ were first invented by Bjarne Stroustrup in 1979 at Bell Laboratories in Murray Hill, New Jersey.
  • Bjarne Stroustrup initially called the new language "C with Classes." However, in 1983 the name was changed to C++.
  • C++ is a middle-level programming language.
  • C++ is a statically typed, compiled, general purpose, case -sensitive, free-form programming language that supports procedural, object-oriented, and generic programming.

2. Programming paradigms

A programming paradigms is a model of programming language based on distinct concepts that shapes the way programmers design, organize and write programs.

  • A programming language is said to use static typing when type checking is performed during compile -time as opposed to run-time. So, Static typing usually finds type errors at compile time, increasing the reliability of compiled programs.
  • C++ fully supports object-oriented programming, including the four pillars of object-oriented development: Encapsulation, Data hiding, Inheritance, Polymorphism.
  • Generic programming or Polymorphism is a programming style that allow one value to take on different types as long as certain contracts such as subtypes signature are kept.

3. ANSI/ISO STANDARD C++

In the early 1990s, a joint committee of the American National Standards Institute (ANSI) and the International Standard Organization (ISO) standardized the syntax used in C++. The standardized rules are known as the ANSI/ISO Standard. Most newer compilers use the new standard, most older compilers use the older standard, and some compilers allow you to use whichever you prefer. The features of the language are almost entirely the same using any C++ compiler, but to access certain features, notably input and output operations, you must begin your C++ programs with slightly different statements.

Differences b/w ANSI/ISO C++ and Standard C++

ANSI/ISO C++ Standard C++
The `include` preprocessor directives do not typically use file extensions, for example `#include` The `include` preprocessor directives typically use file extensions, for example `#include`
Uses the statement `using namespace std;` Does not use the `using namespace std;` statement
The `main()` function header is `int main()` The `main()` function header might be any of the following: `int main()`, `int main(void)`, `void main()`, `void main(void)`
It is conventional to end the `main()` function with `return 0`; The `main()` function does not have a `return` statement if the function type is `void`.

4. Common C++ Keywords

Keywords are the words whose meaning has already been explained to the C++ compiler. The keywords are also called ‘Reserved words’.

and continue if public try
and_eq default inline register typedef
asm delete int reinterpret_cast typeid
asm delete int reinterpret_cast typeid
auto do typename long return
bitand double mutable short uchar_t
bitor dynamiccast namespace signed union
bool else new sizeof unsigned
break enum not state_cast using
case explicit not_eq static virtual
catch extern operator struct void
char false Or switch volatile
class float or_eq template wchar_t
compl for overload this while
const friend throw private xor
constcast goto protected true xor_eq

5. Comments in C++

C++ supports both line comments and block comments. A line comment begins with two slashes (//) and continues to the end of the line on which it is placed. It might take up an entire line, or it might begin after some executable C++ code and take up the rest of the line. A block comment begins with a single slash and an asterisk (/*) and ends with an asterisk and a slash (*/); it might be contained on a single line or continued across many lines.

// this is a comment on one line
/* this comment is on a different line */
/* this comment runs across
three lines of code just to show
that it can be done ! */

6. C++ Basic Syntax

Let us look at a simple code that would print the words Hello World.

#include<iostream>
using namespace std;

int main()
{
    cout <<"Hello World";// prints Hello World
    return 0;
}

Let us look various parts of the above program:

  • The C++ language defines several headers, which contain information that is either necessary or useful to your program. For this program, the header <iostream> is needed.
  • The line using namespace std; tells the compiler to use the std namespace.
  • The line int main() is the main function where program execution begins.
  • The next line cout << "Hello World."; causes the message "Hello World" to be displayed on the screen.
  • The next line return 0; terminates main() function and causes it to return the value 0 to the calling process.

7. Control characters (Escape sequences)

Certain non printing characters as well as the backslash () and the apostrophe('), can be expressed in terms of escape sequence.

  • \a - Bell
  • \n - New line
  • \r - Carriage return
  • \b - Backspace
  • \f - Formfeed
  • \t - Horizontal tab
  • \" - Quotation mark
  • \v - Vertical tab
  • \' - Apostrophe
  • \\ - Backslash
  • \? - Question mark
  • \0 - Null

8. Trigraphs

A few characters have an alternative representation, called a trigraph sequence. A trigraph is a three -character sequence that represents a single character and the sequence always starts with two question marks.

Trigraph Replacement
??= #
??/ \
??' ^
??( [
??) ]
??! |
??< {
??> }
??- ~

Discussion

Read Community Guidelines
You've successfully subscribed to Developer Insider
Great! Next, complete checkout for full access to Developer Insider
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.