I am not sure if Strategy ( or Policy ) pattern is any different than simple use of Polymorphism with Encapsulation of OOPs theory.
So why should it be a part of GoF Design Pattern.
Comments welcome
Cheers,
Siddhartha
Monday, October 15, 2007
Thursday, October 4, 2007
Item 10 of More Effective C++ in an easy way
Though Scott's view is that I don't have RAII, but my approach saves un-necessary complications of using auto_ptr / smart_ptr. I respect Scott's view at the same time many people are not aware of the advantages of auto_ptr / smart_ptr and why to have more learning curve when a user doesn't want it at that time.
Basically I have initialized the pointers and then allocated memory to prevent resource leak during construction in the initialization list.
#include <iostream>
#include <string>
#include <list>
using namespace std;
class Image {
public:
Image(const string& imageDataFileName) {
fileName = imageDataFileName;
}
~Image() {
cout << "~Image()..." << endl;
}
private:
string fileName;
};
class AudioClip {
public:
AudioClip(const string& audioDataFileName) {
fileName = audioDataFileName;
throw exception();
}
~AudioClip() {
cout << "~AudioClip()..." << endl;
}
private:
string fileName;
};
class PhoneNumber {
public:
PhoneNumber(const int number) {
phNumber = number;
}
private:
int phNumber;
};
class BookEntry {
public:
BookEntry(const string& name,
const string& address = "",
const string& imageFileName = " ",
const string& audioClipFileName = " ") try : theName(name),theAddress(address),
// theImage(0),theAudioClip(0), // so sad that multiple initialization is not allowed
theImage(imageFileName != ""
? theImage=0, new Image(imageFileName) //This is the trick ',' operator does the job
: 0),
theAudioClip(audioClipFileName != ""
? theAudioClip = 0,new AudioClip(audioClipFileName)
: 0) {
} catch(...) {
cout << "exception caught during construction of BookEntry..." << endl;
if(theImage) delete theImage;
if(theAudioClip) delete theAudioClip;
throw;
}
~BookEntry() {
delete theImage;
delete theAudioClip;
}
void addPhoneNumber ( const PhoneNumber& number);
private:
string theName;
string theAddress;
list < PhoneNumber> thePhones;
Image *theImage;
AudioClip *theAudioClip;
};
int main() try {
cout << "Hello World!" << endl;
BookEntry bookEntry("sid");
return 0;
}
catch(exception &e) {
cout << " exception caught " << endl;
}
catch(...) {
cout << "Uncaught exception..." << endl;
}
Basically I have initialized the pointers and then allocated memory to prevent resource leak during construction in the initialization list.
#include <iostream>
#include <string>
#include <list>
using namespace std;
class Image {
public:
Image(const string& imageDataFileName) {
fileName = imageDataFileName;
}
~Image() {
cout << "~Image()..." << endl;
}
private:
string fileName;
};
class AudioClip {
public:
AudioClip(const string& audioDataFileName) {
fileName = audioDataFileName;
throw exception();
}
~AudioClip() {
cout << "~AudioClip()..." << endl;
}
private:
string fileName;
};
class PhoneNumber {
public:
PhoneNumber(const int number) {
phNumber = number;
}
private:
int phNumber;
};
class BookEntry {
public:
BookEntry(const string& name,
const string& address = "",
const string& imageFileName = " ",
const string& audioClipFileName = " ") try : theName(name),theAddress(address),
// theImage(0),theAudioClip(0), // so sad that multiple initialization is not allowed
theImage(imageFileName != ""
? theImage=0, new Image(imageFileName) //This is the trick ',' operator does the job
: 0),
theAudioClip(audioClipFileName != ""
? theAudioClip = 0,new AudioClip(audioClipFileName)
: 0) {
} catch(...) {
cout << "exception caught during construction of BookEntry..." << endl;
if(theImage) delete theImage;
if(theAudioClip) delete theAudioClip;
throw;
}
~BookEntry() {
delete theImage;
delete theAudioClip;
}
void addPhoneNumber ( const PhoneNumber& number);
private:
string theName;
string theAddress;
list < PhoneNumber> thePhones;
Image *theImage;
AudioClip *theAudioClip;
};
int main() try {
cout << "Hello World!" << endl;
BookEntry bookEntry("sid");
return 0;
}
catch(exception &e) {
cout << " exception caught " << endl;
}
catch(...) {
cout << "Uncaught exception..." << endl;
}
Sunday, September 30, 2007
Subtraction in Binary
What do you use to subtract two binary numbers?
two's compliment?
Why to have so complicated approach ( of course on Paper )
Just use our basic decimal system approach and subtract the number normally.
For e.g.,
binary decimal
100 4
011 3
subtract 4-3 = 1
Just think that 4 is hundred in binary and 3 is eleven ( as it looks like visually ).
Now subtract 11 from 100 , you will get 89. Now replace 8 by 0 and 9 by 1
you will get 1 as the answer in binary.
When you subtract two binary numbers as decimal numbers, you have four possible numbers 0,1 , 8 and 9. Now since the answer should be in binary, 8 is even so replace by 0 ( as 8 mod 2 = 0) and 9 is odd so replace by 1 ( as 9 mod 2 = 1 ).
See how easy it goes !
two's compliment?
Why to have so complicated approach ( of course on Paper )
Just use our basic decimal system approach and subtract the number normally.
For e.g.,
binary decimal
100 4
011 3
subtract 4-3 = 1
Just think that 4 is hundred in binary and 3 is eleven ( as it looks like visually ).
Now subtract 11 from 100 , you will get 89. Now replace 8 by 0 and 9 by 1
you will get 1 as the answer in binary.
When you subtract two binary numbers as decimal numbers, you have four possible numbers 0,1 , 8 and 9. Now since the answer should be in binary, 8 is even so replace by 0 ( as 8 mod 2 = 0) and 9 is odd so replace by 1 ( as 9 mod 2 = 1 ).
See how easy it goes !
Thursday, September 27, 2007
Singly linked list as Doubly
With the linked list the major concern is the space consumed by the pointer in the structure.
For singly linked list, there is one extra pointer in each node to point to the next node.
Similarly in doubly linked list, there are two extra pointers in each node, one point to previous node and other point to next node.
Now depending upon the machine word size, if it is 32 bit, the pointer consume 4 bytes and in 64 bit machine it consumes 64 bits. Which is quite a bit.
I have written a code in C++, which just take the space of a singly linked list but it can be used to traverse both ways like doubly linked list.
#include
using std::cout;
using std::endl;
template
class NODE {
public:
NODE() {
next = NULL;
}
NODE(const TYPE data) : data(data), next(NULL) { }
TYPE getData() {
return data;
}
NODE *getNext() {
return next;
}
void setNext(long next) {
this->next = reinterpret_cast(next);
}
private:
TYPE data;
NODE *next;
};
template
class LIST {
public:
LIST() {
XORdata = head = tail = NULL;
}
void add(TYPE data);
void traverseForward();
void traverseBackward();
private:
NODE *head;
NODE *tail;
NODE *XORdata;
};
template
void LIST::traverseBackward() {
NODE *tempTail = tail;
NODE *tempTail1 = tail;
NODE *tempXOR = XORdata;
while(tempTail) {
cout << "Data : " <<>getData() << p1 =" reinterpret_cast<"> (tempTail->getNext());
long p2 = reinterpret_cast<> (tempXOR);
tempTail = reinterpret_cast *>(p1^p2);
tempXOR = tempTail1;
tempTail1 = tempTail;
}
}
template
void LIST::traverseForward() {
NODE *tempHead = head;
NODE *tempHead1 = head;
NODE *tempXOR = NULL;
while(tempHead) {
cout << "Data : " <<>getData() <<>getNext()) break;
long p1 = reinterpret_cast<> (tempHead->getNext());
long p2 = reinterpret_cast<> (tempXOR);
tempHead = reinterpret_cast *>(p1^p2);
tempXOR = tempHead1;
tempHead1 = tempHead;
}
}
template
void LIST::add(TYPE data) {
if(NULL == head) {
tail = head = new NODE(data);
} else {
NODE *temp = new NODE(data);
long p1 = reinterpret_cast(XORdata);
long p2 = reinterpret_cast(temp);
tail->setNext(p1^p2);
XORdata = tail;
tail = temp;
}
}
int main(int argc, char **argv, char **arge) try {
LIST list;
for(int i = 10; i < 16; ++i )
list.add ( i);
list.traverseForward();
cout << "Now moving backward !!!" << endl;
list.traverseBackward();
return 0;
} catch(...) {
cout << "Uncaught exception!" << endl;
}
For singly linked list, there is one extra pointer in each node to point to the next node.
Similarly in doubly linked list, there are two extra pointers in each node, one point to previous node and other point to next node.
Now depending upon the machine word size, if it is 32 bit, the pointer consume 4 bytes and in 64 bit machine it consumes 64 bits. Which is quite a bit.
I have written a code in C++, which just take the space of a singly linked list but it can be used to traverse both ways like doubly linked list.
#include
using std::cout;
using std::endl;
template
class NODE {
public:
NODE() {
next = NULL;
}
NODE(const TYPE data) : data(data), next(NULL) { }
TYPE getData() {
return data;
}
NODE *getNext() {
return next;
}
void setNext(long next) {
this->next = reinterpret_cast
}
private:
TYPE data;
NODE *next;
};
template
class LIST {
public:
LIST() {
XORdata = head = tail = NULL;
}
void add(TYPE data);
void traverseForward();
void traverseBackward();
private:
NODE
NODE
NODE
};
template
void LIST
NODE
NODE
NODE
while(tempTail) {
cout << "Data : " <<>getData() << p1 =" reinterpret_cast<"> (tempTail->getNext());
long p2 = reinterpret_cast<> (tempXOR);
tempTail = reinterpret_cast
tempXOR = tempTail1;
tempTail1 = tempTail;
}
}
template
void LIST
NODE
NODE
NODE
while(tempHead) {
cout << "Data : " <<>getData() <<>getNext()) break;
long p1 = reinterpret_cast<> (tempHead->getNext());
long p2 = reinterpret_cast<> (tempXOR);
tempHead = reinterpret_cast
tempXOR = tempHead1;
tempHead1 = tempHead;
}
}
template
void LIST
if(NULL == head) {
tail = head = new NODE
} else {
NODE
long p1 = reinterpret_cast
long p2 = reinterpret_cast
tail->setNext(p1^p2);
XORdata = tail;
tail = temp;
}
}
int main(int argc, char **argv, char **arge) try {
LIST
for(int i = 10; i < 16; ++i )
list.add ( i);
list.traverseForward();
cout << "Now moving backward !!!" << endl;
list.traverseBackward();
return 0;
} catch(...) {
cout << "Uncaught exception!" << endl;
}
Tuesday, September 25, 2007
Cricket Puzzle
Contrary to an Indian, I don't like cricket at all ( at least before twenty 20 ).
Though when I came through the below mentioned cricket puzzle and having interest in solving puzzles. I somehow gained interest in knowing the rules. So people who know cricket should have a look.
Here it goes:
There are last 2 balls to bowl. Both the batsman are on 94 each.
The team require 7 runs to win. Both has to score century as well.
How will they help their team to win.
Though when I came through the below mentioned cricket puzzle and having interest in solving puzzles. I somehow gained interest in knowing the rules. So people who know cricket should have a look.
Here it goes:
There are last 2 balls to bowl. Both the batsman are on 94 each.
The team require 7 runs to win. Both has to score century as well.
How will they help their team to win.
Thursday, September 20, 2007
Atomic Compare and Swap algorithm
Is there anyone who knows how to make CSA in C/C++ to make it atomic ?
This would help in having producer-consumer lock free.
This would help in having producer-consumer lock free.
Why VTABLE is required for Abstract class in C++
I am not sure why VTABLE is required in C++ after all it is just an interface and it always refer to its derived classes only. One can manipulate the pointer to upcast but that has got no meaning ( logical ).
Therefore, my main question is why not optimize it and remove the VTABLE from abstract class.
I am looking for a satisfying answer.
Therefore, my main question is why not optimize it and remove the VTABLE from abstract class.
I am looking for a satisfying answer.
Subscribe to:
Posts (Atom)