Language Elements

Object Oriented Concepts

Data Types

Key Words

Arrays

Pointers

Data Structures

Multithreading

Errors and Exception Handling

Interview Questions


C++ Language Elements

Functions

A C++ program must contain exactly one main function, which is the entry point where program execution begins. Other functions can be called from the main function or from other functions. The main function is usually declared with the keyword int before it, as it typically returns an integer value to the operating system. It cannot be overloaded, nor can it be declared static or inline (where the function's code is directly inserted at the call site instead of performing a regular function call). Parameters may or may not be passed to the main function.

Two common main function declarations are:
1) int main() – without parameters.
2) int main(int a, int b) – with two integer parameters.

A C++ function is defined with a return type. Common return types include void (no return value), int (integer return type), double, and others. The body of a function is enclosed in curly braces { }. Functions can be overloaded, meaning multiple functions can share the same name if their parameter lists differ.

Example: a simple C++ function to find the area of a square:

/* This function calculates the area of a square.
   The return type is void, so it is only called without assigning its result to a variable. */

void AreaSquare() // AreaSquare is the function name with void return type.
{
    double S; // S is one side of the square, declared as double precision.
    cout << "\nEnter the value of S: "; // Prompt for input. \n starts a new line.
    cin >> S; // Input value for side S.
    cout << "\nA = " << S * S; // Calculate and display "A", the area of the square.
}

Data Types

C++ supports primitive data types: int (integer), void (no return type), float (single precision), char (character type, usually smaller than int), double (double precision), bool (true or false).

Derived data types include: Function, Array, Pointer, and Reference.

User-defined data types include: Class, Structure, Union, and Enumeration.

Variables store values for various data types in memory. They must begin with a letter or underscore and may contain letters, digits, and underscores.

Classes and Objects

A class is a fundamental building block of Object-Oriented Programming in C++. C++ evolved from C by adding object-oriented features. Objects are instances of classes. A class is defined with the keyword class, followed by its name and a body enclosed in curly braces.

Libraries

Libraries extend the functionality of C++ programs. Many functions and objects used in code are provided by libraries. The Standard Template Library (STL) offers common structures and algorithms, and C++ also inherits many functions from the C Standard Library. Library functions are declared in header files. Users can also create custom libraries. A detailed reference for C++ libraries can be found at C++ Libraries Reference.

Preprocessor Directives

Preprocessor directives are processed before compilation and are used to include code from libraries or perform other pre-compilation tasks. The #include directive is the most common and is used to specify a header file that contains declarations for functions, objects, or macros needed in the program.





Copyright © by Zafar Yasin. All rights reserved.