C++ Language Elements
Functions
A C++ source code always necessarily contain only one main function, from where execution of the program starts. The other functions are then called from the main function. Main function is typically declaired using key word int before it, as it returns the integer value. Main funcion cannot be overloaded (it will have unique name), nor can it be static and inline. Parameters may or may not be passed to the main function.
Below, two commonly used main function declarations are shown. First one is without passing any
parameters, and for the second one two integer parameters "a" and "b" are passed:
1). int main().
2). int main(int a, int b).
A C++ function is named by return type. Common return types include void (no return type), int (integer return type), etc. The body of function is enlosed in curly brackets { - - - }. Functions can be overloaded.
Consider a simple illustrative example below of a C++ function to find the area of a square:
/*The function below calculate the area of square. The return type is void, so no need of assigning it
to a variable, as it is only called without any return type*/
{
double S; // S is one side of square declaired as of double precisions
cout << "\nEnter the value of S: "; // it will ask to enter the value for side "S".\n is for next line
cin >> S; // input value for side S
cout << "\nA = " << S * S; // calculate "A", the area of square
}
Data Types
C++ supports primtive primitive data types: int (integer type), void (no return type), float (single precision value), char (integer type, with size smaller than int type), double (double precision value), bool (true or false value).
There are also derived data types, such as Function, Array, Pointer, and Reference.
User can also define data types. C++ provides the following user-defined datatypes, such as Class,Structure,Union,Enumeration.
The values for various data types can be stored as variables in memory locations, which can store data and given names, so as to be called as reference. Variable names follow certain rules. They must begin with either letter or underscore, and can contain letters, underscore, and digits.
Classes and Objects
A class is fundamental building block of Object Oriented Programming. C++ evolved from C with addition of object orientation as central feature. Objects can be defined (or instantiated) from the classes. A class is defined with the keyword class, followed by the class name and the class body, enclosed by a pair of curly braces.
Libraries
To extend the basic functionality of source code to perform various tasks, many of the functions and variables used are provided as set of libraries. Use of libraries make it easier to maintain and run the code. The main library consist of Standard Template Library and modified form of inherited C Standard Library. Standard Template Library (STL) provides default functionality. The function library is from the C language. Library functions are defined in the header file. Other then some defined libraries, user can also define their own library. A good list of C++ libraries can be found at C++ Libraries Reference.Preprocessor Directives
Preprocessor directives are executed before compilation, and tells about location of code in the libraries. The #include statement is one of the several preprocessor directives that are used. It is applied on a header file, which tells the compiler about the use of run-time libraries.