Learn C++ Online

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++…

Introduction to Inheritance in C++

Let me introduce you to the concept of Inheritance in C++. Reusability is yet another important feature of C++. It is always nice if we could reuse something that already exists rather than trying to create the same all over again. It would not only save the time and money but also reduce frustration and increase readability. For instance, the reuse of a class that has already been tested, debugged and used many time can save us the effort of developing and testing the same again. Fortunately, C++ strongly supports the concept of reusability.The C++ classes can be reused in several ways. Once a class has been written and tested, it can be adapted by other programmers to suit their requirements. This is basically done by creating new classes which reuse the properties of the existing ones. The mechanism of deriving a new class from an old one is called...

Pointers to functions in C++

In C++, just as we can have pointers to variables, we can have pointers to functions as well. The general syntax of a pointers to a functions is: return-type (*pointer-name) (list of parameter); For example: void (*p) (float, int); In the above declaration, a pointer to a function returns void and takes the two formal arguments of float and int types respectively. After declaring a pointer to a function, the address of the function must be assigned to a pointer as follows: p = &function-name; where p is a pointer variable. Through pointer to function, the function is called as follows: A = (*p) (x, y); where, x and y are actual parameters, and A is a variable in which the function’s return value is stored, p is a pointer name which points to the function Let us illustrate pointer to function with a programming example as follows: #include<iostream.h> void...

Accessing of the Variables pointed to by the Pointer

Accessing of the variable pointed to by the pointer needs to be understood from two angles, i.e., Accessing of normal basic data-type variables Accessing of class-type objects Accessing of normal basic data-type variables: Let us straight-away learn the accessing of normal basic data-type (line int, float, etc.) variables by seeing a simple example as follows: int *p; int x; x = 20; p = &x; //read as assign address of x (&x) to pointer variable p cout << *p; //read as print content at pointer variable p The statement: cout << *p; prints the value 20 because *p means the value at the address p. Similarly: *p = *p + 10 is same as: x = x + 10; Note that if we write the following statement: cout << p; this prints the address of x. Similarly the statement: cout << &x; also prints the addresss of x. Accessing the...

Declaration of Pointers in C++

Pointer variables, like all other variables, must be declared before they may be used in a C++ program. The interpretation of a pointer declaration differs, however, from the interpretation of other variable declarations. When a pointer variable is declared, the variable name must be preceded by an asterisk (*). This identifies the fact that the variable is a pointer. The data type that appears in the declaration refers to the object of the pointer, i.e., the data item that is stored in the address represented by the pointer, rather than the pointer itself. This, the pointer declaration may be written in general terms as: data-type *ptvar; where, ptvar is the name of the pointer variable, and data-type refers to the datatype of the pointer’s object. Remember that an * (asterisk) must precede ptvar.

Basics of Pointers in C++

Before we start with basics of pointers, it is strictly recommended to go through the concepts of pointers from the Learn C Online (Click here). We have attempted to cover pointers in the most simplest manner possible. But, it will be impossible for you to grasp the knowledge of the pointers unless and until you have the concepts of pointers clear in your mind by reading – Learn C Online (Click here). A pointer is a variable that represents the location (rather than the value) of a data item, such as a variable or an array element. Suppose v is a variable that represents some particular data item. The compiler will automatically assign memory cells for this data item. The data item can then be accessed if we know the location (i.e. the address) of the first memory cell allocated to variable v by the compiler. The address of v‘s...