14A Function Overloading-All Programs
14A Function Overloading-All Programs
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
class numbers
{
private:
int a,b;
public:
void add(void);
void add(int,int);
void showval()
{
cout << "Inside showval function." << endl;
cout << "The values are ... " << a << " " << b << endl;
}
};
void numbers::add()
{
cout << "Inside void add(void) function." << endl;
cout << "Enter two Numbers" << endl;
cin >> a >> b;
cout << a << " + " << b << " = " << a+b << endl;
}
void main()
{
clrscr();
// calling add()
numbers x;
cout << "Calling add() from main()." << endl;
x.add();
cout << "Calling showval() from main()." << endl;
x.showval();
cout << "Calling add(50,70) from main()." << endl;
x.add(50,70);
cout << "Calling showval() from main()." << endl;
x.showval();
getch();
};
// Example 2 of function overloading.
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
class shape
{
private:
int length,breadth;
float a,radius;
public:
void area(int); // area of a square.
void area(int,int); // area of a rectangle.
void area(double); // area of a circle.
};
void shape::area(int l)
{
length = l;
a = length * length;
cout << "Area of a square = " << a << endl;
}
void shape::area(double r)
{
radius = r;
a = 22.0 / 7 * radius * radius;
cout << "Area of a Circle = " << a << endl;
}
void main()
{
clrscr();
shape square, rect, circle;
square.area(5);
rect.area(10,20);
circle.area(10.00);
getch();
};