Skip to main content

Posts

Showing posts from December, 2007

!!Happy New Year - 2008!!

Merry Christmas to Everyone

Inline Functions and their Uses

It’s a good practice to divide the program into several functions such that parts of the program don’t get repeated a lot and to make the code easily understandable. We all know that calling and returning from a function generates some overhead. The overhead is sometimes to such an extent that it makes significant effect on the overall speed of certain complex and function-oriented programs. In most cases, we have only a few functions that have extensive use and make significant impact on the performance of the whole program. Not using functions is not an option, using function-like macros is an option, but there is a better solution, to use Inline Functions. Yes, like it sounds, inline functions are expanded at the place of calling rather than being “really called” thus reducing the overhead. It means wherever we call an inline function, compiler will expand the code there and no actual calling will be done. Member functions of ...

Using a Stack to Reverse Numbers

Yeah, I heard many of you saying this and I know it’s no big deal to reverse a number and neither is it using stack to do so. I am writing this just to give you an example of how certain things in a program can be done using stacks. So, let’s move on… As many of you already know, a stack is a Data Structure in which data can be added and retrieved both from only one end (same end). Data is stored linearly and the last data added is the first one to be retrieved, due to this fact it is also known as Last-In-First-Out data structure. For more info please read Data Structures: Introduction to Stacks . Now, let’s talk about reversing a number, well reversing means to rearrange a number in the opposite order from one end to the other. Suppose we have a number 12345 then its reverse will be 54321 Ok, now let’s have a look at the example program which does this: // Program in C++ to reverse // a number using a Stack // ...

Classes and Structures in C++

In C, a structure (struct) gives us the ability to organize similar data together. You may wonder what I said. It is so in C, this is because structure is one of the few things which is more or less entirely different in the two languages (C and C++). In C++, the role of structures is elevated so much as to be same as that of a class. In C, structure could only include data as variables and arrays but in C++ they can also include functions, constructors, destructors etc. and in fact everything else that a class can. Knowing this, it wouldn’t be wrong to say that in C++, structures are an alternate way of defining a class. However there are some differences. Look at the following code: // First difference between a class // and a structure in C++ // define a structure struct mystruct { char name[ 25 ]; int id_no; }; void main() { mystruct a; // in C, it is necessary to // include the struct keyword /...