0% found this document useful (0 votes)
20 views

14A Function Overloading-All Programs

This document contains two examples of function overloading in C++. The first example defines a class with two add functions - one that takes no parameters and prompts the user for input, and another that takes two integer parameters and assigns them to class variables. The second example defines a shape class with three area functions - one that calculates square area from length, one that calculates rectangle area from length and breadth, and one that calculates circle area from radius. Both examples call the different overloaded functions to demonstrate their unique behavior.

Uploaded by

darshan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

14A Function Overloading-All Programs

This document contains two examples of function overloading in C++. The first example defines a class with two add functions - one that takes no parameters and prompts the user for input, and another that takes two integer parameters and assigns them to class variables. The second example defines a shape class with three area functions - one that calculates square area from length, one that calculates rectangle area from length and breadth, and one that calculates circle area from radius. Both examples call the different overloaded functions to demonstrate their unique behavior.

Uploaded by

darshan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 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();
};

You might also like