ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software

Cpp Data Types

Updated on October 2, 2010

Introduction

This tutorial introduces overloaded functions and some side-effects when native data types are coerced at compile time.

/******************************************************************************
 * Date Type exercises and overloaded functions                      main.cpp *
 *                                                                            *
 * Author: nicomp                                                             *
 * Abstract:  Overcoming Potential data typing problems with overloading.     *
 *            If you comment out all the overloaded functions but one,        *
 *             the program will still build and execute, but with very        *
 *             different results.                                             *
 *                                                                            *
 * Revision History:                                                          *
 *                                                                            *
 ******************************************************************************/
#include <iostream>
using namespace std;

// Comment this out to force all the function calls in the main() to resolve to "foo(unsigned int l)"
//#define ALL_FUNCTIONS


// prototypes for overloaded functions
void foo(unsigned int);
#ifdef ALL_FUNCTIONS        // An example of conditional compilation
void foo(signed int);
void foo(double);
void foo(char);
#endif

void main(void)
{

    signed int in   = -1;
    unsigned int un =  45;
    double db       = 200.45;
    char ch         = 'A';

//    No internal data conversions going on here ......
    cout << "\n signed int   "  << in ; foo(in);
    cout << "\n unsigned int "  << un ; foo(un);
    cout << "\n double       "  << db ; foo(db);
    cout << "\n char         "  << ch ; foo(ch);
    cout << "\n";
}
//**************************************************************
void foo(unsigned int l)
{
    cout << "\t\t\t unsigned int  >" << l << "<";
}

//**************************************************************
#ifdef ALL_FUNCTIONS
//**************************************************************
void foo(signed int l)
{
    cout << "\t\t\t signed int  >" << l << "<";
}
//**************************************************************
void foo(char l)
{
    cout << "\t\t\t\t char:  >" << l << "<";
}
//**************************************************************
void foo(double l)
{
    cout << "\t\t\t double:  >" << l << "<";
}
#endif

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.