Introduction to C++

C++ is a general-purpose programming language. It is basically an extension of the C language, or it is called “C with Classes”. Both C and C++ are used for application development.

The main difference between them that C is a procedural programming language, whereas C++ is a combination of both procedural and object-oriented programming languages.

Variables

A variable is considered a box (storage area) to hold data. Accordingly, we have to give each variable a unique name (identifier) to indicate the storage area.

For example, int age = 18;

‘age’ is a variable of type int-integer

Tokens

A token is the smallest meaningful element of a program to the compiler. They are classified as follows:  

  1. Keywords
  2. Identifiers
  3. Constants
  4. Strings
  5. Special Symbols
  6. Operators

1. Keywords

In C, there are 32 keywords or pre-defined words.

auto             double           int               struct
break           else                long            switch
case             enum             register       typedef
char             extern            return         union
const           float               short           unsigned
continue     for                 signed         void
default        goto               sizeof          volatile
do                if                    static          while 

But in C++, there are 31 additional keywords.

2. Identifiers

Identifiers are used for naming variables, functions and arrays.

Rules for naming identifiers:

  • They must begin with a letter or underscore(_).
  • They must consist of only letters, digits, or underscore. No other special character is allowed.
  • It should not be a keyword.
  • They must not contain white space.
  • It can be up to 31 characters long as only the first 31 characters are significant.

3. Constants

In C++, we can create variables whose value cannot be changed. We use the keyword const to create constants.

Example:

const int LIGHT_SPEED = 299792458;
LIGHT_SPEED = 2500 // Error! LIGHT_SPEED is a constant.

Here, when we try to change the value of LIGHT_SPEED, we get an error.

4. Strings

String can be defined as an array of characters ending with a null character(‘\0’). We enclose strings in double quotes.

Eg: char string[20] = {‘H’, ’e’, ‘l’, ‘l’, ‘o’, ‘W’, ‘o’, ‘r’, ‘l’, ’d’, ‘\0’};

5. Special Symbols

There are certain symbols which have fixed meaning and cannot be used for other purposes. Eg: Brackets, paranthesis, semi colon, assignment operator, etc.

6. Operators

Operators are symbols that can set off an action. These operators work on operands.

They are of 2 types:

  1. Unary Operators: They act on single operands.
  2. Binary Operators: They require 2 operands to act on. eg: arithmetic operator, logical operator, etc.
We will be happy to hear your thoughts

Leave a reply

Edusera
Logo
Open chat
1
Scan the code
Hello!👋
Can we help you?