Thursday, September 14, 2006

Virtual function mechanism

Tips :
Virtual function mechanism is key feature of C++ for dynamic binding.
Whenever a class declares a virtual function or is derived directly or indirectly from a class which declares a virtual function, the complier adds an extra hidden member variable which points to the virtual table. A virtual table is nothing but an array of pointers to the virtual functions. The entries in the virtual table are changed at run time to point to the correct function.

while allocating memory for a class, the first four bytes are pointer to the virtual table. The virtual table itself contains pointers to the virtual functions of the object.
So what we were doing in the program is changing the address of the virtual table pointer to the fn MyFunc() ...


we try to change the address of the virtual function pointer in the following program:


#include
#include
#include
//Pointer to a function returning void

typedef void (*PFN)();
typedef struct
{
PFN Fn;
} VTable;

void MyFunc()
{
cout<<"\n This is MyFunc() \n";
}

//Base class
class CBase
{
public:
virtual void Display() {
cout<<"\n CBase :: Display() function \n";
}
};


//Derived class

class CDerived :public CBase{
public :
void Display() {
cout<<"\n CDerived :: Display() function \n";
}
};

int main(int argc, char* argv[]){

CDerived derivedObj;
CBase *pBase =&derivedObj;

//Create our own VTable
VTable myOwnTable ;
//Pointer to Fn MyFunc()

myOwnTable.Fn = MyFunc;
//Holder for pointer to virtual table
VTable *pVTable = &myOwnTable;

//Tweak the virtual table pointer by changing the first 4 bytes (assuming a long holds pointer)
memcpy(&derivedObj, &pVTable , sizeof(long));

pBase->Display();

getch();
return 0;

}

Tuesday, September 12, 2006

13_September_2006

Tips :
What is the difference between a bug,defect and an error?

Error: A human action that produces an incorrect result. Programmatically mistake leads to error. bug: An informal word describing any of the above. Deviation from the expected result. A software bug is an error, flaw, mistake, failure, or fault in a computer program that prevents it from working as intended, or produces an incorrect result. Bugs arise from mistakes and errors, made by people, in either a program's source code or its design. It is said that there are bugs in all useful computer programs, but well-written programs contain relatively few bugs, and these bugs typically do not prevent the program from performing its task. Defect:
Problem in algorithm leads to failure. A defect is for something that normally works, but it has something out-of-spec.

When should our destructor be virtual? (C++)
When someone will delete a derived-class object via a base-class pointer.
In particular, here's when we need to make our destructor virtual:

What special considerations are needed when forward declarations are used with member objects?
The order of class declarations is critical.
The compiler will give you a compile-time error if the first class contains an object (as opposed to a pointer to an object) of the second class.
For Example :
class Fred; // Okay: forward declaration
class Barney
{
Fred x; // Error: The declaration of Fred is incomplete
};
class Fred
{
Barney* y;
};

One way to solve this problem is to reverse order of the classes so the "used" class is defined before the class that uses it:

class Barney; // Okay: forward declaration
class Fred
{
Barney* y; // Okay: the first can point to an object of the second
};
class Barney {
Fred x; // Okay: the second can have an object of the first
};

News :
Brasilia: Four days ahead of his meeting with President Musharraf, Prime Minister Manmohan Singh made it clear on Tuesday that he would take up the question of foreign support for terrorism in India in his talks with the Pakistani leader. The two are scheduled to meet on the sidelines of the XIV Non-aligned summit in Havana for an unstructured interaction.
Quote of the day :
If one studies too zealously, one easily loses his pants. - Einstein

Thursday, September 07, 2006

Tips

Tips:

The difference in Visual Studio and VS.net 2003 :

For example if we create a new "Hello world" project in (Visul studio ) VC++,
and we run the program from visual studio, it wont take the debug folder as current folder, it takes the source code folder as current folder.
if we are running the program in VS.net, by default it takes "debug" folder as a current folder.

Quote of the day :
--------------------------
Knowledge exists in two forms - lifeless, stored in books, and alive, in the consciousness of men. The second form of existence is after all the essential one; the first, indispensable as it may be, occupies only an inferior position.Einstein


Puzzle-I:
~~~~~~~~~
You have to measure exactly 4 liters of water, but you only have a 3-liter bottle and a 5-liter bottle. How do you do it?
.
.
.
.


Answer:-------
Fill the 3-liter bottle and pour it into the empty 5-liter bottle. Fill the 3-liter bottle again, and pour enough to fill 5-liter bottle. This leaves exactly 1 liter in the 3-liter bottle. Empty the 5-liter bottle; pour the remaining 1 liter from the 3-liter bottle into the 5-liter bottle. Fill the 3-liter bottle and pour it into the 5-liter bottle. The 5-liter bottle now has exactly 4 liters.
Here is another way to do it*
Fill the 5-liter bottle and pour water from it into the 3-liter bottle until it is full.This leaves 2 liters in the 5-liter bottle. Empty the 3-liter bottle and pour the 2 liters of water from the 5-liter bottle into the 3-liter bottle. Fill the 5-liter bottle again. Fill the 3-liter bottle from the 5-liter bottle. Since the 3-liter bottle had 2 liters of water, only one liter is transferred leaving exactly 4 liters of water in the 5-liter jug

PUzzle-II:
~~~~~~~~~
From the following clues, you must figure out who is the owner of each pet, and where the pet got lost.
1.A rabbit and a dog are two of the lost pets. 2.The pet lost in the garden is owned by Mary. 3.Robert does not own a dog. 4.John's pet was lost in the woods. 5.The cat was not lost in the woods or in the park.






Answer:------
Strategy for solving logic problemsI. Read the complete problem:
Get a general idea of the problem. Here we have 3 owners, 3 pets, and 3 locations. You are being asked to match each pet with an owner and a location. A 3×3 table, also called a logic matrix, can be used to organize this information.
II. Make a list of the members of each category:
Owners: Mary, Robert, JohnPets: rabbit, dog, catPlaces: park, garden, woods
III. Start filling the table with information from the clues.
The clues may provide direct or deduced information and can be processed in any order. We start by putting the owner names in the top row. Here, we start with clue number 2:
2. The pet lost in the garden is owned by Mary.
Owners Mary John Robert Pets Places garden
5. The cat was not lost in the woods or in the park.Therefore, the cat must have been lost in the garden.
Owners Mary John Robert Pets cat Places garden
4. John's pet was lost in the woods.... and, therefore, Robert's pet must have been lost in the park.
Owners Mary John Robert Pets cat Places garden woods park
1. A rabbit and a dog are two of the lost pets.3. Robert does not own a dog.This means that Robert owns the rabbit, and John must own the dog.
Owners Mary John Robert Pets cat dog rabbit Places garden woods park