Вы находитесь на странице: 1из 2

// Example 1 of Function Overloading.

#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 numbers::add(int a, int b)


{
cout << "Inside void add(int,int) function." << endl;
cout << a << " + " << b << " = " << a+b << endl;
// here a and b are local variables to function add(int,int) and are
// different then variables a and b of class numbers.
// so then how to assign values to a and b of class numbers.
// here is the way.
numbers::a = a;
numbers::b = b;

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(int l, int b)


{
length = l;
breadth = b;
a = length * breadth;
cout << "Area of a Rectangle = " << 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();
};

Вам также может понравиться