Polymorphism in Cpp

/******************************************************************************
 * Main() for Overloaded Functions Demo                              main.cpp *
 *                                                                            *
 * Author:                                                      *
 * Abstract: Yes                                                              *
 *                                                                            *
 * Revision History:                                                          *
 *                                                                            *
 ******************************************************************************/
#include <iostream>
#include "Graphics.h"

using namespace std;

void main()
{
    DrawCircle(1,2,3,4);    // The DrawCircle() function that requires 4 arguments is called

}
<<Cut>>
/******************************************************************************
 * Overloaded Functions                                          Graphics.cpp *
 *                                                                            *
 * Author: nicomp                                                             *
 * Abstract: These functions illustrate techniques for overloading functions. *
 *                                                                            *
 * Revision History:                                                          *
 *                                                                            *
 ******************************************************************************/

#include "Graphics.h"

// The functions that are prototyped in Graphics.h should be defined here.


// This function is called DrawCircle and accepts 3 integers.
int DrawCircle(int intCenterX, int intCenterY, int intRadius)
{
    // this is a stub. It provides just enough code to allow the project to build
    return 1;
}

// This function is called DrawCircle and accepts 4 integers.
int DrawCircle(int intCenterX, int intCenterY, int intEdgePointX, int intEdgePointY)
{
    // this is a stub. It provides just enough code to allow the project to build
    return 1;
}

// This will cause a compiler error. Note the name mangling in the error text.
int DrawCircle(int intEdgePointX1, int intEdgePointY1, int intEdgePointX2, int intEdgePointY2)
{
    // this is a stub. It provides just enough code to allow the project to build
    return 1;
}

<<Cut>>


#pragma once
/******************************************************************************
 * Overloaded Functions                                            Graphics.h *
 *                                                                            *
 * Author: nicomp                                                             *
 * Abstract: These functions illustrate techniques for overloading functionsr.*
 *           The C++ compiler differentiates functions by their signature.    *
 *           A function signature is defined as the argument list and the     *
 *            function name. The return type is not considered.               *
 *                                                                            *
 * Revision History:                                                          *
 *                                                                            *
 ******************************************************************************/

// This prototype declares a function called DrawCircle that accepts 3 integers
int DrawCircle(int intCenterX, int intCenterY, int intRadius);

// This prototype declares a function called DrawCircle that accepts 4 integers
int DrawCircle(int intCenterX, int intCenterY, int intEdgePointX, int intEdgePointY);

// Note that the names of the arguments are not considered in the function signature.
// Only the function name along with the # and types of arguments are used to differentiate functions.

// The function declared below will cause a compiler error, but not in this file.
// This file will happily compile despite the glaring error.
// The compiler error will be flagged where the function is defined; Graphics.cpp
int DrawCircle(int intEdgePointX1, int intEdgePointY1, int intEdgePointX2, int intEdgePointY2);


More by this Author


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