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;
}