Cpp Data Types

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

More by this Author

  • The Importance of Doing Laundry
    19

    We could nag, but this is better. It is less work for us to use the words of others to emphasize the importance of doing laundry. Besides, if you saw the way we dressed, you'd laugh your mouse off.

  • Data Hiding in Java
    12

    Data Hiding is an aspect of Object Oriented Programming (OOP) that allows developers to protect private data and hide implementation details. In this tutorial we examine basic data hiding techniques in Java.

  • Introduction to Windows Notepad
    5

    A useful text editor is an essential component of any personal computer. Every day we need to make notes, compose documents, and record vital pieces of information. We depend on our text editor. Microsoft provided...


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working