Thursday, 10 November 2011

what is the difference between usage of a "macro" and a "typedef"?

In the first look it seems that a typedef is totally redundant as its job can be done using a macro in c++. the actual difference and the need for typedef can be clearly understood considering the usage of both these with pointers. All compiler does for a macro is to blindly replace the occurrences with the specified equivalent value before the compilation process. In case of the typedef c++ treats it as an alias in compile time for its definition.





#define intptr1 int* typedef int* intptr; Consider the below example: are they the same to the compiler? NO //are following two equivalent? intptr1 a,b; intptr2 a,b; Consider the macro defnition, intptr1 is simply replaced with int* int* a, b; // intptr replaced by int* Which means that we are declaring variable 'a' of type "int*" and variable 'b'of type "int". Something which we certainly didn't mean. The second definition involves intptr2, which is a typedef to int*. typedefs are handelled by the C++ compiler,
and they are treated identical to the type that they stand for. Thus, there is not just a "textual substritution", but much more than that: int *a, *b; // intptr2 is treated identical to int*


another fail case:


#define intptr1 int*
typedef int* intptr2;
//Are these equivalent?
const intptr1 x;
const intptr2 y;

Storage Class Specifiers in C++


Storage specifiers specify the scope linkage and lifetime of a variable.
The default storage specifier for all function parameters and variables is auto.


auto -  specifies an automatic variable. The storage scope is limited to the scope of the inner braces the variable is declared.


register - this indicates the compiler to store the variable in the CPU registers to allow fast access. However the modern compilers are smart enough= to figure which variables have to be stored in the registers.


mutable - denotes that the member can be modified even if the containing object is a const.


static - denotes a variable with static linkage, function parameters should never be made static.


extern - Denotes the variable is declared in another file.

Friday, 4 November 2011

Simple Game - Beginner C++


Guess the number game

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <stdlib.h>
#include <time.h>
//Author Thejas
using namespace std;
int generateRandomNumber();
int main()
{
    srand(time(NULL));
    int secret = generateRandomNumber();
    int inputVar = -1;
    cout<<"~~~~~~~~~~~Guess the Magic Number Game (1 to 100)~~~~~~~~~~~"<<secret<<endl;
    do
    {
        cout<<"Enter the number "<<endl;
        if(!(cin>> inputVar))
        {
            cout<<"You should only enter integers between 1 to 100"<<endl;
            cin.clear();
            cin.ignore(1000,'\n');
        }
        else if(inputVar > 100 || inputVar < 1)
        {
            cout<<"You should only enter integers between 1 to 100"<<endl;
            cin.clear();
            cin.ignore(1000,'\n');
        }else if(inputVar > secret)
        {
            cout<<"The secret number is lesser"<<endl;
        }else if(inputVar < secret)
        {
            cout<<"The secret number is greater"<<endl;
        }else{
            cout<<"You have fount the Secret number "<<secret<<endl;
            cout<<"Fireworks Busrsting!!!!!!!"<<endl;
            break;
        }
    }while(true);
    return 1;
}

int generateRandomNumber()
{
    int randomNum = (rand()%100)+1;
    return randomNum;
}

Sunday, 16 October 2011

signed/unsigned comparability

Saturday, 15 October 2011

Sorting Algorithm Handy Guide

Analysis of Sorting Algorithms:


STABILITY: Ability to maintain the relative Ordering(Duplicates in Data) of the input Data.
IN PLACE: Ability to perform the algorithm on the input data Memory, Not using extra Memory.
Time Complexity.

Sorting
STABILITY
IN PLACE
TIME COMPLEXITY
Best Case
Average Case
Worst Case
Insertion Sort
Yes
Yes
O(n)
O(n^2)
O(n^2)
Bubble Sort
Yes
Yes
O(n)
O(n^2)
O(n^2)
Selection Sort
Yes
Yes
O(n^2)
O(n^2)
O(n^2)
Quick  Sort
NO
Yes
O(nlogn)
O(nlogn)
O(n^2)
Merge Sort
YES
NO
O(nlogn)
O(nlogn)
O(nlogn)
Heap Sort
NO
NO
O(nlogn)
O(nlogn)
O(nlogn)

Thursday, 13 October 2011

const member function in C++, pointers and const in c++

In the below example we see that a function declared as const member of a class allows us to modify the contents of data member of the class. A brief explanation about the const member functions and const pointers are given in order to understand the behavior of the const Member Functions().


class String
{

    private:
        char* rep;

    public:
        String (const char*);
        void toUpper() const;
};

String :: String (const char* s)
{
    rep = new char [strlen(s)+1];
    strcpy (rep, s);
}

void String :: toUpper () const
{
    for (int i = 0; rep [i]; i++)
    rep[i] = toupper(rep[i]);
}

int main ()
{
    const String lower ("lower");
    lower.toUpper();

    cout << lower << endl;
    return 0;
}


A const member function, is a member function that does not mutate its member variables.
const on a member function does not imply const char *. Which would mean that you can't change the data in the address the pointer holds.
Your example does not mutate the member variables themselves.
A const on a member function, will ensure that you treat all of your member variables as const.
That means if you have:
int x;
char c;
char *p;
Then you will have:
const int x;
const char c;
char * const p; //<-- means you cannot change what p points to, but you can change the data p points to
There are 2 types of const pointers. A const member function uses the one I've listed above.

So the lesson to learn here is that the const keyword on member functions does not ensure that your object will not change at all.
It only ensures that each member variable will be treated as const. And for pointers, there is a big diff between const char * and char * const.
In the following statement, pOne is a pointer to a constant integer. The value being pointed to can’t be changed:

const int * pOne; // pointer to a constant integer

In this next statement, pTwo is a constant pointer to an integer. The integer can be changed, but pTwo can’t point to anything else:

int * const pTwo; // constant pointer to an integer

In this third statement, pThree is a constant pointer to a constant integer. The value being pointed to can’t be changed, and pThree can’t be changed to point to anything else:

const int * const pThree; // constant pointer to a constant integer

The trick to keeping this straight is to look to the right of the keyword const to find out what is being declared constant. If the type is to the right of the keyword, the value is constant:

const int * p1; // the int pointed to is constant

If the variable is to the right of the keyword const, the pointer variable itself is constant:

 int * const p2; // p2 is constant, it can’t point to anything else


Most of the time a const member function will mean that the member function will not modify the object itself, but this is not always the case, as the above example shows.