I'M working my way through the book "C++ without fear by Brian Overland" and there are two small programs with to totally different types of type casting and the book fails to explain the difference, please help me understand. Thanks. The first one is on a while loop and the second one is on a for loop.

prime1.cpp

using namespace std;

int main()
{
    int n;      // number to test for prime-ness
    int i;      // loop counter
    int is_prime;   // boolean flag

    // assume that a number is prime untio proven otherwise

    is_prime = true;

    // get a number from the keyboard

    cout<< "Enter a number and press ENTER: ";
    cin>> n;

    // test for primeness by checking for divisiblity by all whole numbers from 2 to sqrt(n)

    i = 2;

    while(i <= sqrt(static_cast<double>(n)))
    {
        if(n % i == 0)
            is_prime = false;
        i++;
    }

    // print results

    if(is_prime)
        cout<< "Number is prime.";
    else
        cout<< "Number is not prime.";

    system("pause");
    return 0;
}

prime2.cpp

#include <Stdafx.h>
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    int n;
    int i;
    int is_prime;

    // assume that a number is prime until proven otherwise

    is_prime = true;

    // get a number from the keyboard
    cout<< "Enter a number and press ENTER: ";
    cin>> n;

    // test for prime-ness

    for(i = 2; i <= sqrt((double) n); i++)
    {
        if(n % i == 0)
            is_prime = false;
    }

    // print results

    if(is_prime)
        cout<< "Number is prime.";
    else
        cout<< "Number is not prime.";

    system("pause");
    return 0;
}

I know that the C-Style cast aka (Type)Variable is basically trial an error of all the casts put together. At least that's what I was taught.

Example:

(double) Variable would be equivalent to trying all of the following and whichever works first, that's used:

const_cast<double> Variable
reinterpret_cast<double> Variable
static_cast<double> Variable.

Whichever of the above works is the one the program will use. Now in the above, it never uses dynamic_cast. I'm not sure why but I read up on dynamic casting and apparently its to do with upcasting and downcasting.

Btw, static_cast is used when you know the type your casting from doesn't need a runtime check to be casted to another type. It's used to put reverse implicit conversions and put additional restrictions. I personally only used this on classes so far.. I usually prefer to use C-Style casting though.

That's about it. Dynamic cast someone else will have to explain because I don't know it.

Ok.. theres not much of a difference, but as far as i know.. they both go down to the basics..
Wen u use (double) variable in the above case the value of the variable is changed by adding more accuracy wen u go from a lower to higher data type and is subsequently reduced when going from a higher to lower data type. ( Wen i say higher data type i mean larger size and more precision digits). Whereas, in the case of static type cast.. i think the value isn't changed, and only some rubbish is filled in..

Example :

Int n = 6;
float b;
b=n;
b= (float) n; (Both the statements give the same result)

Here, n=6, b= 6.000000.....

Otherwise with static, n=6, b=6.!@#$%^&* (Here !@#$%^&* is random trash the computer stores up)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.