learncpponline

Write first C++ Program using Microsoft Visual Studio

This article will guide you to write first C++ program using Visual Studio 2008 and explain you step by step on how to create your first C++ program. Follow the following simple steps and you should be able to create your first c++ program without any issues: Open the Microsoft Visual Studio 2008 application. Click on File > New > Project as shown below: On performing the above steps, following window will be displayed. Under “Project types:” select Visual C++ > Win32. At the right hand side, under Templates Select Win32 Console Application. Enter the Project Name in the Name field, Location and the the Name of the Solution as desired and click on OK button. On performing the above step, following window will be displayed: Click on Next Button. Select Console application as Application type and under Additional options, select Empty project and click Finish. On clicking Finish button,...

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...

Function Overloading in C++

One significant addition made to the capabilities of function in C++ is that of function overloading. With this facility you can have multiple functions with the same name, unlike C, where all the functions in a program must have unique names. In C, every function has to have a unique name. At times this becomes annoying. For example, there are following three different functions that return the absolute value of an argument: int abs (int i); long labs (long l); double fabs (double d); All these functions do the same thing, so it seems unnecessary to have three different function names. C++ overcomes this situation by allowing the programmer to create three different functions with the same name. This is called function overloading. Let us illustrate function overloading with an example as follows: int max (int, int); int max (int, int, int); //function prototypes int main() { cout << max(99,...

Inline Functions in C++

As we know, when we call a function from the main program, the control jumps to the function and then comes back to the calling program when execution of function is completed. This process, although looks simple, but involves a lot of background processes as of passing of parameters to the function, allocating of storage for function’s local variables, storing the current variables, etc. C++ provides a convenient way of omitting all these processes by simply declaring the function to be inline. When we declare a function inline, we tell the compiler to replace each call to such function(declared inline) with the explicit code of the function. To declare a function inline all we need to do is to add the word inline before the definition of the function as: inline data-type function_name(type 1 arg 1, type 2 arg 2, …, type n arg n); Also, in the function-prototype of...

Default Arguments in Functions

In C, if a function is defined to receive 2 arguments, whenever we call this function we have to pass 2 values to this function. If we pass one value then some garbage value is assumed for the last argument. As against this, functions in C++ have an ability to define default values for arguments that are not passed when the function call is made. The default arguments are given only in the function prototype and should not be repeated in the function definition. The compiler uses the prototype information to build a call, not the function definition. Let us illustrate this concept with an example: Here is an example of a prototype (i.e., function declaration) with default values: float amount(float principal, int period, float rate = 0.15); The default value is specified in a manner syntactically similar to the variable initialization. The above prototype declares a default value of...