C++ Program to demonstrate Function Name Overloading

This is a simple C++ Program to demonstrate Function Name Overloading. An example of function name overloading within a C++ class many_name is used in this program. The constructor is overloaded as well as one of the methods to illustrate what can be done in C++ programming language to achieve polymorphism.

#include <iostream>

class many_names {
int length;
int width;
public:
many_names(void); // Constructors
many_names(int len);
many_names(int len, int wid);
void display(void); // Display functions
void display(int one);
void display(int one, int two);
void display(float number);
};

many_names::many_names(void)
{
length = 8;
width = 8;
}

many_names::many_names(int len)
{
length = len;
width = 8;
}

many_names::many_names(int len, int wid)
{
length = len;
width = wid;
}

void many_names::display(void)
{
cout << "From void display function, area = " << length * width << "\n";
}

void many_names::display(int one)
{
cout << "From int display function, area = " << length * width << "\n";
}

void many_names::display(int one, int two)
{
cout << "From two int display function, area = " << length * width << "\n";
}

void many_names::display(float number)
{
cout << "From float display function, area = " << length * width << "\n";
}

main()
{
many_names small, medium(10), large(12, 15);
int gross = 144;
float pi = 3.1415, payroll = 12.50;

small.display();
small.display(100);
small.display(gross,100);
small.display(payroll);

medium.display();
large.display(pi);
}

// Result of execution
// From void display function, area = 64
// From int display function, area = 64
// From two int display function, area = 64
// From float display function, area = 64
// From void display function, area = 80
// From float display function, area = 180

 

M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post