Wednesday, August 11, 2010

Overriding overloaded methods

#include <iostream>

using namespace std;

class B {
    public:
    virtual void A(string a, int b) {
        cout<<"B::A(string, int)"<<endl;
    }
    virtual void A(int c) {
        cout<<"B::A(int)"<<endl;
    }
};

class D : public B {
    public:
    virtual void A(string a, int b) {
        cout<<"D::A(string, int)"<<endl;
    }
};

int main() {
    D d;
    B* b = &d;
   
    b->A("a", 1);
    b->A(2);
   
    d.A("a", 1);
    d.A(2);

    return 0;
}

What do you think would be output of the above code?

Obviously, the output is not what most people will expect i.e.:
D::A(string, int)
B::A(int)
D::A(string, int)
B::A(int)


The code will instead give compilation error on line
     d.A(2);

This is because if you override one overloaded method, you need to override all of them, otherwise the ones you don't override are not available in the derived class.

To get things working you'll need to add following method in class D


virtual void A(int c) {
    B::A(c); 
}

As suggested by Kapil in the comments, the correct and elegant way to get it working is to add a declaration of A() in classD:

using B::A;

Monday, July 19, 2010

Domestication

I look in the mirror,
I look around,
I see the wildness tamed,
The jungles are lost,
The animals are gone,
all that is left are sanctuaries,
domestication taught in the name of education.

Friday, June 18, 2010

The biggest delusion of all times is time itself

Time was invented to sequence events, to record what happened first, and what after that. Just like a label. We never realized when time took control of our lives. Things went wrong when we thought of time as an absolute entity, shared by everything and everyone. Is time really same for everyone?

As I see, time is not same for everyone. Putting it simply: when speed (w.r.t. clock time) of some action gets slow, it's because of its time getting slow! Assuming time is absolute makes many thing simpler, looking it the other way can explain things which otherwise have no satisfactory answers.

When you are feeling bored lazy, your time goes fast, you spend like an hour while your watch shows that only 15 minutes have passed. Similarly when you are excited, enjoying, your time goes slower. No doubt being happy makes you live longer i.e. with respect to the clock time.

Some people seem to age faster, others look younger just because their time passes at different rate.

Some people can solve problems quickly, because their time passes fast. While you think they solved the problem in seconds they actually took a minute of "their time" to solve it.

Oh, by the way, if you have realized I just discarded "Time Travel" concept as viewed by most of us :).

Wednesday, April 7, 2010

Predefined or a matter of chance but not a choice!

Life is predefined or a matter of chance but not a choice. I have used this 'funda' many time. The best thing about this 'funda' is that the other person is left baffled. They obviously can't agree to such a thing and eehh at the same time they can't really disagree :D.

How do people do something? They do some action: blink eyes, move hands, speak etc. How do people do some action? They move some muscle in their body which results into action. How do muscle move? They get signals from brain. How do muscle get signals from brain? Chemical reactions! How does a brain thinks? Again chemical reactions. You can not deny the fact that every thing that happens in our body is result of some chemical reactions. Everything happening around us is a result of some chemical reactions.

Consider this: for a given condition, keeping everything fixed; the temperature, the pressure and everything else, will a chemical reaction produce exactly same products every time?

If you think that the answer is 'YES', then your life is nothing but a chain reaction. At any instant the present conditions are product of the previous reactions, which were product of previous reactions and so on till the 'start of time' when the first reaction took place. No matter what you do, or think to do, its all part of the game. Nothing could have happened except what happened. You have no control on what's happening.

If you think your answer to the question is 'NO', then your life is just a matter of chance. Since all the conditions remain same there can not be a logic as to determine what would be the products of a reaction. It has to be random. Everything you do or happening around is result of random reactions. You again have no control on what's happening.

Whether its predefined or a matter of chance life is surely not a choice.

Sunday, March 14, 2010

Right or Wrong??

Recently I came to know that many things I say or write sound daft and confuse people, so please don't mind if what follows doesn't makes any sense... :P 

What do you think... things are going the right way or the wrong way?
I'll tell you what I believe. It doesn't matters! Really, according to me it doesn't matters whether things are going the right or the wrong way; if you just believe that things are going the right way..rest everything will be fine :-)

Why so??

There are two things, first: your thinking that things are going right or wrong, and second: whether things are actually going right or wrong. Let us consider all four cases in detail:

o You think that things are going right, and things are actually going right:
Things are actually going right implies that your thinking must be correct, and you are thinking that things are going right, this complies with the original statement that things are going right.
(I did warn about my thoughts sounding daft)

o You think that things are going right, and things are actually going wrong:
Things are actually going wrong implies that your thinking must be incorrect, and you are thinking that things are going right. This means that things must not be going right, this again complies with the original statement that things are going wrong.

o You think that things are going wrong, and things are actually going right:
Things are actually going right implies that your thinking must be correct, and you are thinking that things are going wrong. This means that things must not be going right! oops.. now there is a contradiction with the original statement that things are actually going right.

o You think that things are going wrong, and things are actually going wrong:
Things are actually going wrong implies that your thinking must be incorrect, and you are thinking that things are going wrong. This meas that things must be going right! once again we have a contradiction with the original statement that things are actually going wrong.

Now you can see, if you think that everything is going the right way, you don't get any contradictions, if you think that things are going the wrong way then you always get contradictions in life. Contradiction means things in life don't fit together and you tend to feel even more that something is wrong which sure doesn't give you peace of mind. So, it doesn't even matters whether things are going the right way or the wrong way, if you think and feel that the right things are happening, everything will start to fall in place and fit together.