Fundamentals of C Plus Plus (C++)

Introduction to C++ Programming

As the name suggest, C++ is a programming language based on C with some added features (represented by ++) which makes possible the creation of comparatively efficient programs. C++ programming inherits all the good features of C by filtering out some of the limitations of C. In addition, C++ programming also possesses some extremely new unique concepts which were not at all present in C. However, since C++ is based on C, it is extremely important to have a thorough knowledge of C. However, by chance you are not thorough with C Programming then you are strictly recommended to have a look at Learn C Online tutorial – www.LearnCOnline.com. Remember, it is extremely important to have a strong foundation in order to build a strong structure. Now, let us turn our attention to the study of C++. So, let’s start our journey towards the study of C++…

sizeof operator in C++

sizeof operator in C++ is an unary operator which can be used to find the size of the data-type or expression or a variable. It returns the size of the memory allocated (in Bytes) by the compiler. Lets say, we want to find the size of the memory allocated to the data-type int. We can find it by using sizeof operator as shown in the below code: #include<iostream.h> #include<conio.h> void main() { clrscr(); int sizeOfInt; sizeOfInt = sizeof(int); cout << sizeOfInt << ” Bytes”; getch(); } Output: 2 Bytes As you can see, the above code calculated the size of int data type and displayed the result in C++.

Structures in C++

Structure is a data-type in which the individual elements can differ in data-type. Thus, a single structure might contain integer elements, floating-point elements and character elements. Pointers, arrays and other structures can also be included as elements within a structure. The individual structure elements are referred to as members. Defining of Structures in C++: In general terms, the composition of a structure can be defined as: storage-class struct tag{ data-type 1 member 1; data-type 2 member 2; … data-type 2 member n; }; In the above declaration, storage-class is an optional storage-class specifier, struct ia a required keyword, tag is a name (i.e., identifier) that identifies structure of this type, and data-type 1, data-type 2, …, data-type n represents the data-types of the members member 1, member 2, …, member n respectively. Once the composition of the structure has been defined, individual structure-type variables can be declared as follows: storage-class...

Arrays in C++

An array can be defined as a sequence of data in memory, wherein all the data are of the same type, and are placed in physically adjacent locations. An array is a group of elements that share a common name, and that are differentiated from one another by their positions within the array. For example, if we have five numbers, all of which are named as x, they may be listed as: The position of each of these elements can be indicated by means of a subscript as: x1 = 65 x2 = 33 x3 = 64 x4 = 88 x5 = 5 In mathematics, a subscript is a number written to the right of the variable name, slightly below the line, and usually in small type. The subscript indicates the position of the particular elements. In C++, square brackets are used for this purpose. The method of subscripting arrays...

Preprocessor Directives in C++

The C++ preprocessor is a collection of special statements, called directives, that are executed at the begenning of the compilation process. The preprocessor is usually an integral part of our compiler. It is a process that is executed by the compiler before our C++ program code is compiled into machine instructions. The job of the preprocessor is to prepare our source code, proper for the compile phase, according to the instructions that we have included in the source files. These instructions are called preprocessor directives. All preprocessor directives begin with the symbol #, so they are easily distinguishable from C++ language statements. Given below is the complete set of preprocessor directives: Directive Function #include Supports header file inclusions #if, #else, #elif, #endif, #if defined (or #ifdef), #if !defined (or #ifndef) Enables conditional compilation #define, #undef Enable symbol and macro substitution #line Allows redefinition of current line and filename #error Produces...

Input and Output in C++

Output in C++: Output in C Plus Plus (C++) is enabled by the object cout (pronounced as “C out”) which is predefined to corresponding to the standard output stream. A stream is an abstraction that refers to a flow of data. The standard output stream normally flows to the screen display, although it can be redirected to other output devices. cout, when combined with insertion or put-to operator << enables the output in C++ programs. << operator redirects the contents of the variable on its (<<‘s) right to the object on its(<<‘s) left. A simple example of output of a phrase is depicted below: cout << “LearnCppOnline.com is the best C++ programming tutorial site”; Above statement causes the phrase in the quotation marks to be displayed on the screen. Input in C++ Contrary to cout, to receive input through the keyboard what is used is object cin (pronounced as “C...