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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #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 |