Skip to main content

Posts

Showing posts from July, 2007

Introduction to Inheritance in C++

Inheritance in C++ is one of the major aspects of Object Oriented Programming (OOP). It is the process by which one object can inherit or acquire the features of another object. In C++ class and structure can use inheritance. It means we can make one class (known as derived class) to acquire or inherit the features of another class (known as base class). Base class has general features common to all the derived classes and derived class (apart from having all the features of the base class) adds specific features. This enables us to form a hierarchy of classes. Ex. if we define a class ‘computer’ then it could serve as the base class for defining other classes such as ‘laptops’, ‘desktops’ etc.. This is because as you know that laptops have all the features of computers and so have desktops (and so should their classes) but they have their specific features too. So, rather than defining all the features of such classes from scratch, we make them inhe

How String Functions Work? Part II

This is the second part of the article How String Functions (strinh.h) Work? Here we'll be designing our own version of some other commonly used standard library string manipulation function that we discussed in the article String Manipulation Functions (string.h) II. These will help increase your programming skills further. mystrlwr(): // mystrlwr function #include<iostream.h> // -- FUNCTION PROTOTYPES -- char *mystrlwr( char *); // -- ENDS -- void main() { char ch[]= "C++ ProGramming123" ; cout<<mystrlwr(ch); } char *mystrlwr( char *str) { char *temp; temp=str; while (*str!= '\0' ) { // change only if its a // UPPER case character // intelligent enough not to // temper with special // symbols and numbers if (*str>= 65 && *str<= 90 ) *str+= 32 ; str++; } return temp;; } mystrupr():

What is Polymorphism?

Polymorphism means to have one interface for different methods or functions. It is the ability through which we can do different operations (by calling different functions) from the same interface. In C++ functions and operators can be made to be polymorphic, but in this article we’ll only be discussing about polymorphic functions. There can be two types of polymorphism which are discussed below: Compile-Time Polymorphism: When we have two or more polymorphic function (overloaded functions) and the call to a particular function is resolved (or known) at the compile-time, it is called compile-time polymorphism. The following example program illustrates this. Please read the comments for further information. // program to demonstrate // compile-time polymorphism #include<iostream.h> // -- FUNCTION PROTOTYPES -- // -- OVERLOADED FUNCTIONS -- void func( int ); void func( char *); // -- ENDS -- void main()

Array of Functions

In the article Pointers to Function , we saw how pointers can be made to point at functions and hence can be used to invoke them. By far the most important use of pointers to functions is to have arrays of functions. This can be achieved as stated below You already know that we can have arrays of pointers and pointers can be made to point at functions. So combining both we can have array of pointers to functions put differently, we can have array of functions. The example program below demonstrates how we can have array of functions; please note that this concept is mostly used in writing compilers and interpreters, so you shouldn’t expect the program to do anything serious or useful! // Program to demonstrate // array of functions #include<iostream.h> // -- FUNCTION PROTOTYPES -- void func1(); void func2(); void func3(); void func4(); void func5(); // -- ENDS -- void main() { // notice the prototype void (*ptr[5])();

String Manipulation Functions (string.h) II

This is the continuation of the article String Manipulation Functions (string.h) in which we’re discussing about the string manipulation functions. Here I have listed 8 functions along with their prototype (simplified) and a short description. One thing to note here is that unlike the last article on this topic, I have not included example programs, since the functions (with their prototypes) are pretty much straightforward. strlwr: Prototype: char *strlwr(char *) This function converts the given string to lowercase and returns the same. strupr: Prototype: char *strupr(char *) This function converts the given string to UPPERCASE and returns it. strncat: Prototype: strncat(char *str1, const char *str2, int n) It appends first ‘n’ characters of str2 to the end of str1. strncpy: Prototype: int strncmp(char *str1, const char *str2, int n) This function compares first ‘n’ characters of str1 with str2, it returns 0 if compare was su

How String Functions (string.h) Work?

In the previous article String Manipulation Functions (string.h) , we had a look at some of the commonly used string manipulation functions. There is no denying the fact that those functions are useful but have you ever wondered how those functions actually work or what is the algorithm behind their working? If yes then read on… In this article I am going to present you with our own version of the string manipulation functions that we had discussed, namely strlen(), strcpy(), strcat() and strcmp() . Our versions will do the same thing as done by the original functions but surely they would teach us a lot! Let's have a look at them one-by-one: mystrlen // mystrlen- function #include<iostream.h> int mystrlen(const char *); void main(void) { char ch[]="This is great!"; cout<<"Length:"<<mystrlen(ch); } int mystrlen(const char *str) { int len=0; while(str[len]!='\0') len++; r

String Manipulation Functions (string.h)

This article discusses about the classic string manipulation functions defined in the string.h header file. From quite a while peoples have been asking me to write an article on the standard library string manipulation functions. These functions are defined in the string.h header file, so you must include it to use them. There are dozens of string functions in the string.h header file and thus it is difficult to list them all. So rather than listing them all I would be discussing in detail about only few commonly used string manipulation functions along with an example program illustrating how each function is used. strlen: Prototype: int strlen(const char *string); This function takes the base address of the string as the argument and returns the number of characters in it (including spaces). // strlen() string manipulation // function #include<iostream.h> #include<string.h> void main(void) { char ch[]="String Manipulati

Introduction to Linked Lists III

In the article Introduction to Linked Lists , we introduced the concept of linked list, the example program was programmed to be able to add and display the elements in the linked list. In reality only addition of elements to the linked list is not enough to take the most out of linked list; we should be able to do other operations such as insertion, deletion of elements etc. This article would teach you to do such operation (insertion, addition, deletion etc). The program itself is quite big and has enough comments so I won’t discuss anything here; rather I leave it up to you to understand all the operations yourself! // -- Linked Lists -- // ------------------ // Example program to illustrate // addition, insertion, deletion // and display of nodes in the // linked list #include<iostream.h> // node class, this will // represent the nodes or elements // of the linked list class node { public: int info; node *link

Introduction to Linked Queues

In one of the article Introduction to Linked Stacks , I said that representing data structures such as Stacks and Queues as arrays had one major problem that it can’t have more than a predefined number of elements. To overcome this we used linked lists to represent stacks. In this article we’ll use linked lists to represent queues. Below are some graphics that illustrate the addition and retrieval of elements to and from the linked queue. FIG.: Addition of data to the linked queue FIG.: Retrieval of elements from the linked queue I don’t think there is anything more that needs to be discussed, so let’s have a look at the example program: // -- Linked Queues -- // C++ Example Program to // illustrate the representation // of queues as linked lists #include<iostream.h> // node class, this will // represent the nodes or elements // of the linked queues class node { public: int info; node *link; }; // declare global objects node

Increase your Programming Skills III

Here I have listed some questions related to strings in c++, solve them to increase your programming skills. Problem #1: // Problem related to strings #include<iostream.h> void main(void) { char ch[]="Programming Skills"; int i=0; cout<<ch[++i]; cout<<ch[i++]; cout<<i++[ch]; cout<<++i[ch]; } Problem #2: // Problems related to strings #include<iostream.h> void main(void) { char ch[]="Programming Skills"; char *s="Programming Skills"; cout<<sizeof(ch)<<sizeof(s); cout<<sizeof(*ch)<<sizeof(*s); } Problem #3: // Problems related to strings #include<iostream.h> void main(void) { int n[]={4,3,2,1,0}; for(int i=0;i<5;i++) cout<<n[n[i]]; } Problem #4: // Problems related to strings #include<iostream.h> void main(void) { int n[]={11,10,9,8,7,6,5,4,3,2,1,0}; char ch[]="C++ La

Increase your Programming Skills II

This is the continuation of the last article… Increase your Programming Skills Solve the problems listed here to gain programming skills. Problem #5: // Problem or Question in C++ // -------------------------- // Problem related to pointers // for increasing programming // skills #include<iostream.h> void main() { char ch1[]="Programming"; char ch2[]="Skills"; char *s1=ch1; char *s2=ch2; while(*s1++=*s2++); cout<<ch1; } Problem #6: // Problem or Question in C++ // -------------------------- // Problem related to pointers // for increasing programming // skills #include<iostream.h> void main() { int i, *j; i=1; j=&i; cout<<i**j*i+*j; } Problem #7: // Problem related to pointers #include<iostream.h> void main() { int ar[]={1,2,3,4,5}; char *p; p=(char *)ar; cout<<*((int *)p+3); } Problem #8: // Problem related to poin

Increase your Programming Skills I

Here I have listed some selected problems or questions related to pointers in C++. Solve them to increase your programming skills. Problem #1: // Problem or Question in C++ // -------------------------- // Problem related to pointers // for increasing programming // skills #include<iostream.h> void main() { char ch[]="I_Like_C++"; cout<<*&*&ch; } Problem #2: // Problem or Question in C++ // -------------------------- // Problem related to pointers // for increasing programming // skills #include<iostream.h> void main() { int i,*p,**q,***r; i=10; p=&i; q=&p; r=&q; cout<<i<<*p<<**q<<***r; } Problem #3: // problem related to pointers #include<iostream.h> void main() { char ch[]="Programming Skills"; char *s=ch; cout<<s+++3; } Problem #4: // Problem related to pointers #include<iostream.h>

Introduction to Linked Stacks

In the article Data Structures: Introduction to Stacks , we saw that there was one major disadvantage of representing stacks using arrays- the stack like the array could have a limited number of elements, while stacks should be able to grow up to any number of elements. Besides this there were other disadvantages too. In one of the other article about Linked Lists , we noticed one useful property of linked lists that they can grow up to any size to accommodate for the addition of elements and it efficiently uses the memory too. So if we combine both of this to from a linked version of the stack then it won’t have the shortcomings that the array version had. This is what this article is all about. pushing and popping As you know that addition of elements to the stack is known as pushing while retrieval is known as popping. The process of pushing and popping in case of linked version of stack is slightly different from the array version. The following

Macros with Arguments

Have a look at the following code: #include<iostream.h> #define MAX 10 void main(void) { for(int i=1;i<=MAX;i++) cout<<i<<endl; } Above we have used the type of macro expansion that we learnt in the article Introduction to C++ Preprocessor Directives Do you know that just like functions we can have arguments in the macros too! The following code shows how: #include<iostream.h> // this is how macros with // arguments are defined // NOTE: THERE SHOULD NOT BE // ANY SPACE BETWEEN THE // MACRO TEMPLATE (i.e. AREA) // AND THE PARANTHESIS // CONTAINING THE ARGUMENTS // (i.e. (x)) #define AREA(x) (x*x) void main(void) { // calling a macro // is almost similar // to calling a function cout<<AREA(10); } It is that simple! However, keep one thing in mind as stated in the comments not to put any space between the macro template and the braces containing the argument. #define ARE

Hurray! My 50th Post

I am so glad to inform you all that this is my 50th post on this blog. In the span of 57 days this blog has grown in terms of both content and readership. I won’t take much of your time saying it out loud but I would like to thank all of my readers and subscribers for their valuable response and co-operation! Thanking You Arvind Gupta

Introduction to Basic Encryption and Decryption

Encryption is a familiar sounding word which means to convert readable data in such a form that it becomes un-understandable or un-meaningful. It is employed almost everywhere where any confidential data is needed to be kept or transferred. Encryption goes hand in hand with decryption which means to convert un-meaningful encrypted data to its original meaningful form. Here in this article we are going to design two functions, one for encryption and other for decryption, to illustrate the basic concept of encryption and decryption. Please note that the example program provided in this article is for illustrative purpose only, there are a few limitations in the program which limits its practical use. How encryption and decryption works? The main concept behind encryption is to convert the readable data into something which looks un-meaningful to us. It could be achieved in various ways but the simplest one is to change the ASCII code of the data. Ex. #

Changing the case (lower, upper) of Strings

In this article, we will be designing two functions to change the case of strings. One would change a string from lower case to upper case while the other would do the opposite. Although we have pre-defined functions for doing this in a header file, but this article is for those who dare to know how all these operations are done internally. Changing the case: How is it done? The main theory lies in the way C++ treats character constants and strings. Have a look at the following code: #include<iostream.h> void main(void) { char first='A'; char second=65; cout<<first; cout<<endl; cout<<second; cout<<endl; } whose output is: A A Press any key to continue This is because ‘A’ and its ASCII code 65 are equivalent to the compiler and in c++ we can manipulate it in whatever way we like. Now look at the following code: #include<iostream.h> void main(void) { char arr[4]="ABC"; char a

Merging Two-Dimensional Arrays (Matrices)

In the article Merging One Dimensional Arrays , we discussed how to merge one-dimensional arrays, in this article we will be discussing about the merging of two-dimensional arrays. Merging as you know is the process of combining two similar things. In the context of arrays, it means to form a big array from two smaller arrays which has all the elements from both the arrays. In case of one-dimensional arrays there is only one way in which two arrays can be merged but in case of two-dimensional arrays there are two ways. Suppose, we have the following two 2d arrays (matrices): mat1={ {1, 2, 3}, {4, 5, 6}, {7, 8, 9} } and mat2={ {10, 11, 12}, {13, 14, 15}, {16, 17, 18} } then they can be merged in the following two ways: merge_row={ {1, 2, 3, 10, 11, 12}, {4, 5, 6, 13, 14, 15}, {7, 8, 9, 16, 17, 18} } merge_col={ {1, 2, 3}, {4, 5, 6}, {7,

Introduction to Linked Lists Part II

In the previous article Introduction to Linked Lists , we introduced the basic concept of linked list. To make the program (an the article) as simple as possible, we discussed only the addition and display of nodes in the linked list although necessary we didn't’t discussed the deletion of node in that article. In this article we will be discussing about the deletion of nodes from linked lists. Deletion of node (elements) from a linked list The node to be deleted can be represented by many ways but here we will be representing it by its info. So if we have the following linked list And we want to delete node1 then we will express it by its info part (i.e. 10). The main theory behind deletion of nodes is pretty simple. We need to make the link pointer of the node before the target node (to be deleted) to point at the node after the target node. Suppose if we wish to delete node having info as 10 from the above linked list then it will be accompl

Introduction to Linked Lists

We have been using arrays to store similar data linearly. While arrays are simple to understand and easy to implement in common situations, they do suffer from some drawbacks which are listed below: Arrays have fixed dimensions, even if we dynamically allocate the dimension it remains constant throughout. So there is a limit to the number of elements it can store. Operations such as insertion and deletion are pretty much difficult to implement and increases the overhead because these operations require elements in the array to be physically shifted. Linked lists overcome these drawbacks and are commonly used to store linear data. Actually elements of linked lists (called as nodes) store two information, data and the link (pointer) pointing to the next elements (node). The elements (nodes) are linked sequentially with the help of link pointers. So we can say that linked lists are collection of nodes which have data and are linked sequentially so that all the nodes or elements

Introduction to Recursive Functions

Recursion is a process of defining something in terms of itself. Function recursion therefore means to define a function in terms of itself, in other words a function that calls itself inside its body is known as recursive functions. Example: void func (something) { something… something… func(something); } Notice how the function func () is calling itself! Why use recursive Functions? In most cases recursive functions can be replaced by iterative statements, then why use recursive functions? Here are a few points that justify its use: Recursive functions make the code easier and simpler to understand. There are certain algorithms that could be very easily implemented using recursion but are pretty much difficult to implement using iterative or other non-recursive methods. Some of the people tend to think recursively, so their thoughts can be better implemented using recursion. Below is a s

Pointers to Function

Function Pointers is a rather confusing yet powerful feature of C++ programming language. Even if you have programming for a while I bet you have seen nothing like it, simply because they aren't’t needed in everyday programming. Their most common use is in writing compilers and interpreters. The main theory behind function pointers is, just like the contents of a variable can be accessed by a pointer, much the same way functions can also be invoked (called) by referencing it by a pointer to that function. Although variables and functions are two separate identities, both of these are stored at some memory address which can be pointed (and hence accessed) by a pointer. Going in detail of the working of function pointers will only confuse you so we skip that for now and move on to a simple example program. The function defined in the program is made as simple as possible to reduce confusions. Please note that the program only illustrates how function poin

Your Questions my Answers

[I have started this new section in which I will answer your questions publicly. Therefore, if you are having any confusions or problems, you can e-mails me at one.arvind@gmail.com . Please note that… read more ] Problem: I want to sort each row of a matrix in ascending order, after which I need to display another matrix whose elements are the column indices of the elements of the original matrix with respect to the corresponding elements of the sorted matrix. Please give a program to do so. eg. Original matrix={ {9,4,2}, {7,3,8} }; Sorted Matrix= { {2,4,9}, {3,7,8} }; Column Index= { {2,1,0}, {1,0,2} }; Dominic [ * ], Student, Nigeria [* Last Name Deleted on Request] Solution: Nice question Dominic, here is the solution. I will not discuss anything here as I have included enough comments in the program itself. //