Search This Blog

Thursday, December 8, 2011

Printing different representations of a Number

Today I wanted to print Binary representations of 0 to 40... But I did not know how to do it.
I did knew few algorithms for conversion but I was looking for ready-made solution.

"itoa() is the required function"

Here is a simple C++ program in QT to print Decimal, Binary, Octal and Hex Representation of numbers 0 to 99



#include <QtCore/QCoreApplication>
#include <stdlib.h>
#include <iostream>
#include <iomanip>

using namespace std;

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

    QCoreApplication a(argc, argv);

    char binary_repr[12];
    char octal_repr[12];
    char hex_repr[10];

    cout << "| Decimal |  Binary  | Octal  | Hex |" << endl;
    for(int i = 0; i <=99; ++i)
    {

        _itoa_s(i, binary_repr, 2);
        _itoa_s(i, octal_repr, 8);
        _itoa_s(i, hex_repr, 16);

        cout << "|" << setw(9) <<  i
             << "|" << setw(10) << binary_repr
             << "|" << setw(8) << octal_repr
             << "|"  << setw(5) << hex_repr
             << "|" << endl;
    }
    return a.exec();
}


Sunday, September 12, 2010

Wanna learn Design patterns?

If you are OOP guy with more than 3-4 years experience then mostly your interview wont complete without questions on "Design patterns".
Even if you don't want to prepare for interview you will need Design patterns knowledge to understand vocab in design discussions.
Even if you don't do job anywhere (which solves this vocab problem - I design software for my own!), Design patterns can be useful tools for saving your efforts in reinventing the wheel.

The most reliable and respected book on this subject by GoF (Gang of Four) Design Patterns : Elements of Reusable Object Oriented Software is difficult to understand (at least for beginners).

Thanks to Anwar Khan, my senior colleague who suggested me Head First Design Patterns (HFDP).  I thought that this book is for Java guys (you know I am C++ dude). But when Anwar convinced me that C++ and Jave both are OOP languages and if I understand C++ then I should be easily able to learn it from HFDP.
This book is great! Thanks a ton to authors for making subject easy to understand (they have also made learning fun :) ). The book teaches you patterns in such a great way that they permanently fit into your memory. Trust me you don't need Java background, but probably some OOP background is a must. This book explains most of the fundamental and commonly used design patterns in detail. It does not explain few patterns like Builder, Chain of responsibility and few others in detail. It does provide some Bird's Duck's eye view of these patterns. I am sure you will also like learning Design patterns from this book.

After you have read this book do not forget to read GoF's book. HFDP also guides you on how to make use of GoF's book. GoF's book is (according to me) ultimate book on this subject. I have not read GoF completely yet - but I am planning to do that soon.

Here are few web links (some I got from HFDP and some from Google), which are useful in learning design patterns -
http://c2.com/cgi/wiki?DesignPatterns
http://sourcemaking.com/design_patterns
http://www.vincehuston.org/dp/

In my own opinion if you are serious about OOP, then you should also read Grady Booch's Object Oriented Analysis and Design

If you have any useful links/ suggestion for book on this subject - please post a comment.

Tuesday, July 13, 2010

Is it necessary to specify virtual keyword in derived classes? (C++)

No - not at all necessary! BUT please do it so that the maintenance programmer wont have horror experience debugging your code :)

Let me support it with sample code :


#include <iostream>

using namespace std;

class Base {
public:
 Base() { cout << "Base::Base()" << endl; }
 virtual void VirtFun() { cout << "Base::VirtFun()" << endl; }
 virtual ~Base() { cout << "Base::~Base()" << endl; }
};

class Derived : public Base {
public:
 Derived() { cout << "Derived::Derived()" << endl; }
 void VirtFun() { cout << "Derived::VirtFun()" << endl; }
 ~Derived() { cout << "Derived::~Derived()" << endl; }
};

int main () {
 
 Base* pBase = new Derived();
 pBase->VirtFun();
 delete pBase;

 return 0;
}

Output of this program is :
Base::Base()
Derived::Derived()
Derived::VirtFun()          
Derived::~Derived()
Base::~Base()

Even though the virtual keyword is not specified in the Derived class - the VirtFunc() and Destructor of virtual class are automatically virtual (because virtual keyword is specified for them in Base class).

Also note that : only the functions which are specified as virtual in Base class will be by default virtual in the Derived classes. If the function is not virtual in Base class - then it is not so.

I am not sure whether this features is compiler dependent or not, I have tested this code using 'Microsoft Visual C++ 2005' and G++, both give same output.