Archive for the ‘C/C++’ Category

What is the difference between “overloading” and “overridding”?

December 10, 2009

Function Overloading is the implementation of static polymorphism where same function name having different defenitions for different uses.

Function overriding is applicable for dynamic polymorphism where where the function name and the definition are same but the function call is decided at the run time according to the type of object reference.

Advertisements

ABC to Convert Integers to Strings in C++

June 23, 2009

I was solving a problem in TopCoder. It required a conversion of  integer to a string. This is simple in Java but a little complicated in C++. So, I am jotting down here some of the link that helped me. I will also try to make a mention of the problem associated with them.

The first one – itoa. Well, itoa considered unsafe to use this in C++. You may get an error like ” itoa was not declared in the scope “. I got stuck with the same error. It was then I started searching for solutions. The link following is about itoa. Note that, itoa doesn’t work in TopCoder.

http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/

The next link use boost and a string stream. Again, the header file “boost/lexical_cast.hpp” is not detected by TopCoder.

http://ubuntuforums.org/showthread.php?t=271660

So, I feel, more or less the best way is to have you own customized code.

string convertInt(int number)
{
    if (number == 0)
        return "0";
    string temp="";
    string returnvalue="";
    while (number>0)
    {
        temp+=number%10+48;
        number/=10;
    }
    for (int i=0;i<temp.length();i++)
        returnvalue+=temp[temp.length()-i-1];
    return returnvalue;
}

The following link has more information about it.

http://www.cplusplus.com/forum/beginner/7777/

If you have any doubt, post in them as comments.

In Java, you can go about with some code like the following. Note, ‘s’ is a String and ‘a’,’ b’ and ‘c’ are Integers.

s = a + ” ” + b + ” ” + c;

Though something like above is possible in C++, it requires ‘a’,’ b’ and ‘c’ to be Strings.

Happy coding ! 🙂

boost/lexical_cast.hppboost/lexical_cast.hpp

How to use scanf to read Strings with Spaces ?

June 10, 2009

I knew this for two years now, but it dint strike me before to put it up in the blog; don’t know why ?

Its easy and simple, I don’t think I need to explain. Only the syntax needs to be remembered.

scanf(” %[^\n]s”,a);

May be you get a question in your mind. What does ‘[^\n]’ do ? Does it set the delimiter to ‘\n’. Well, you on the right track. Yes, it does. Instead on ‘\n’, if you had put a ‘\t’ it would consider all words with spaces as string until you press a ‘Tab’. You can even have a  ‘  ‘(a space). The scanf can be expanded as

scanf(” %[^ ]s”,a);  // Note – There is a space after ^

The only differece between scanf(” %s”,a) and scanf(” %[^ ]s”,a) is that, when you enter a string with two words, the former considers each word as a new string whereas the latter consider only the first word as a string and the other word is ignored.

As a Example, consider the string “Hello World“, the former reads “Hello” and “World” as two strings (if you had called ‘scanf’ twice) and the latter reads only the first word “Hello” (even if you had called ‘scanf’ twice) ! Go ahead and experiment with other delimiters !! 😀

How to set precision in C++

June 1, 2009

In C++, you can use the ‘precision’ function to set the precision of the output. The function ‘precision’ is present in the header file ‘iostream.h’. The following,

cout.precision(n);

will have totally print ‘n’ d igits in the output with the decimal point in between(say ‘x’ before the decimal and ‘n-x’ after the decimal. The value of x will depend on the result). For example, if n = 4, the result is

0.2727 for 3/11, and
1.272 for 14/11

The example illustrated below provide a clear guidance on how to use the precision function. The Problem is to find the sum of ‘n’ numbers and then print the output in a single line.

#include<iostream>

using namespace std;

int main()
{
double a,s=0;
while( cin >> a )
{
s += a;
}
cout.precision(15);
cout<<s<<‘\n’;
return 0;
}

%d bloggers like this: