Sunday, January 20, 2008

Secrets Of C++

Secrets of C++


Here I am not going to talk about concepts of c++ instead of that how compiler achieves C++ concepts? And how it generates code for that? Before we start discussing about this we should change our mind set. So first we will recall programmer’s evolution.


Earlier day’s programmer started to program in machine code. It’s very hard to program in machine language because address calculation is very difficult, a small mistake cause to rewrite entire code. So a group of people started to abstract machine code for that they developed assembler. After that it plays wonderful role for software development. Programmers still not satisfied with assembler’s abstraction so they decided to abstract assembly code for that they developed compiler. It’s like


English level -----) Compiler ------) Assembler ------) machine code
Statement

First level abstraction was machine code then second level abstraction is assembly code. Programmers still not satisfied with the existing abstraction so they decided to change the way programmer thinks, so they developed C++ concepts and C++ compiler. When programming in C++, programmers no need to think about computer structure they only need to focus on the problem for solving.


I raise a question to readers… what role plays by the compiler for achieving C++ concepts and how it generates code? If u have executable there is no concept like C++ or C or any abstraction. Everyone knows that technology is only for reducing human beings work. So this abstraction also one type of technology or framework for developers.

Class
Class is for grouping a set of variables and function. Here class name plays a great role and we can also say class name is the core of C++ concepts. Through class name only C++ compiler maintains variable’s uniqueness. How?

Consider below example

#include

class Add
{
int x,y;
public:
Add(int a, int b)
{
x = a;
y = b;
}

int evaulate()
{
return (x + y);
}
};

class Sub
{
int x,y;
public:
Sub(int a, int b)
{
x = a;
y = b;
}

int evaulate()
{
return (x - y);
}
};

Add add(20, 10);
Sub sub(20, 10);

main()
{

int res1, res2;

res1 = add.evaulate();
res2 = add.evaulate();

printf("%d %d",res1,res2);
}


We defined two classes one is Add another one is Sub both the classes contains function evaluate() so just think how compiler solves this problem? Compiler uses class name for solving This issue it will prefix the class name with function name which is belongs to that class this is not only for functions and also member variables.

So it will become


#include

int Add_x;
int Add_y;

int Sub_x;
int Sub_y;

Add_Add(int a, int b)
{
Add_x = a;
Add_y = b;
}
int Add_evaulate()
{
return (Add_x + Add_y);
}

int Sub_Sub(int a, int b)
{
Sub_x = a;
Sub_y = b;
}
int Sub_evaulate()
{
return (Sub_x + Sub_y);
}

main()
{
int res1, res2;

Add_Add(20, 10);
Sub_Sub(20, 10);


res1 = Add_evaulate();
res2 = Sub_evaulate();

printf("%d %d", res1, res2);
}

So compiler translates the C++ code like C level, now i thing u can understand how C++ compiler makes programmer to think in terms of problem solving instead of thinking in terms of computer structure. this is simple we will discuss how inheritance, function overloading virtual functions and all achieved by compiler.
====================================
next section will come soon keep watching this space
====================================

No comments: