You are on page 1of 9

FUNCTION OVERLOADING

QUESTION 1:
Design a class to overload a function Joystring() as follows:

(i) void Joystring (String s, char ch1, char ch2) with one string argument and two character
arguments that replaces the character argument ch1 with the character argument ch2 in the
given string s and prints the new string.
Example:
Input value of s =”TECHNALAGY”
ch1=’A’,
ch2=’O’
Output : “TECHNOLOGY”

(ii) void Joystring (String s) with one string argument that prints the position of the first space
and the last space of the given string s.
Example:
Input value of =”Cloud computing means Internet based computing”
Output : First index : 5
Last index : 36
(iii) void Joystring ( String s1, String s2 ) with two string arguments that combines the two
strings with a space between them and prints the resultant string.

Example:
Input value of s1 =”COMMON WEALTH “
Input value of s2=”GAMES “
Output : COMMON WEALTH GAMES
(use library functions)
QUESTION 2:
Design a class to overload a function sum() as follows:
(i) int sum(int a, int b) to calculate and return the sum of all the even numbers in the range a to
b.
Sample Input:
a = 14
b = 16
Sample Output:
Sum = 70
(ii) double sum(double n) to calculate and return the product of the following series:
Product = 1.0 × 1.2 × 1.4 × … × N
(iii) int sum(int n) to calculate and return the sum of only odd digits of the number N.
Sample Input:
N = 43961
Sample Output:
Sum = 13
Write the main() method to create an object and invoke the above methods.
QUESTION 3:
Write a program to create a class which will have the following functions:

void polygon (int n, char ch):with one integer argument and one character type argument that draws a filled square of
side n using the character stored in ch.

Void polygon( int x, int y): with two integer arguments that draws a filled rectangle of length x and breath y using the
symbol ‘@’

Void polygon() with no argument that draws a filled triangle shown below. Example: input
value of n = 2, ch= ‘o’

Output: OO

OO

ii) input value of x= 2, y= 5


Output: @@@@@

@@@@@

iii) output: *
**
***
****
*****

QUESTION 4:
Design a class to overload function pattern() as follows
a) void pattern() without any argument to print $$$$$$$$$$
b) void pattern() with one character argument to print ##########
c) void pattern() with one integer and one character argument to print @@@@@@@@@
Write a function main to call the above methods
Sample output
$$$$$$$$$$
##########
@@@@@@@@@@
QUESTION 5:
Design a program to overload a function rect()
void rect(int n,char ch) With one integer argument and one character type argument draw
filled square of side n using the character stored in ch.
void rect(int l, int b, char ch)- With two integer argument and 1 character argument draw a
filled rectangle of length l and breadth b using the character stored in ch.
Get the following output.
@@@@@
@@@@@
A
BB
BB
CCC
CCC
CCC
DDDD
DDDD
DDDD
DDDD
QUESTION 6:
Design a class to overload a function compare as follows:
(a) void compare (int, int) – to compare two integer values and print the greater of the two
integers.
(b) void compare (char, char ): to compare the numeric value of two characters and print
the character with higher numeric value.
(c) void compare(String, String): to compare the length of the two strings and print the
longer of the two.
QUESTION 7:
Overload a function check() to perform the following task
i) to check if a number is Armstrong. A number is said to be Armstrong if the sum of
cubes of its digits is equal to the number.
ii) ii) To check if given string is palindrome
QUESTION 8:
QUESTION 9:

QUESTION 10:

QUESTION 11:
QUESTION 12:
Design a class to overload a functions as follows:
1. Find the sum of the series x(x+y) + x2(x2+y2) +x3(x3+y3)+ … + xn(xn+yn)
2. S = (x+ (x2/2!) + (x3/3!)+…….n)
Write the main() method to create an object and invoke the above methods.
QUESTION 13:
Create overloaded methods named void calc_volume ( ), that has been overloaded to perform
the following functions
calc_volume (double) for sphere Volume of Sphere (4/3) * pi * r* r* r
calc_volume (double, double) for cylinder Volume of Cylinder pi * r* r * height
calc_volume (float, float) for cone Volume of Cone (1/3) * pi * radius2 * height
QUESTION 14:
Design a class to overload a functions as follows:
1. S = 1!X + 2!X2 + 3!X3 + 4!X4 + …………………………n
2. S = (X+1)2+(X+2)3+(X+3)4+(X+4)5+…………… +(X+N)N+1
Write the main() method to create an object and invoke the above methods.
QUESTION 15:
Design a class to overload a function num_calc( ) as follows:

1. void num_calc( int num, char ch) with one integer argument and one character
argument, computes the square of integer argument if choice ch is ‘s’ otherwise finds its
cube.
2. void num_calc( int a, int b, char ch ) with two integer argument and one character
argument. It computes the product of integer arguments if ch is ‘p’ else adds the
integers.
3. void num_calc( Sting s1, String s2) with two string arguments, which prints whether
the strings are equal or not

QUESTION 16:

QUESTION 17:

QUESTION 18:

Specify the class finder with the following overloaded function:

int findmax(int n1, int n2) – to decide and return largest from n1 ,n2. If both the numbers are
same the return 0.
int findmax(int n1,int n2,int n3) –to decide and return largest from n1,n2,n3. If all the
numbers are same then return 0.
Write main function to call above function
QUESTION 19:
Write a program using a function called area() to compute the area of a:
(i) Circle(II * r)where II = 3.14
(ii) Square(side*side)
(iii) Rectange(length * breadth)
Display the menu to output the area as per user's choice

QUESTION 20:

Design a class to overload a function compute() as follows:


(i) void compute(int,char): to compute the square of the integer argument if the given
character argument is ‘s’ otherwise find its cube.
(ii) void compute(double char): to compute volumeof a cube if the given character
argument is ‘v’ otherwise find its diagonal.
(iii) void compute(int,int,char): to compute area of a rectangle if the given character
argument is ‘a’ otherwise finds its perimeter.
Volume of cube=side3
Area of rectangle=length*breadth
Diagonal of cube=a√3
Perimeter of rectangle=2*(length+breadth)
QUESTION 21:
Design a class to overload a function series() as follows:
(i) double series(double n) with one double argument and returns the sum of the
series.sum = 1/1 + 1/2 + 1/3 + ..... 1/n
(ii) (ii) double series(double a, double n) with two double arguments and returns the sum
of theseries. Sum = 1/a2 + 4/a5 + 7/a8 + 10/a11 ..... to n terms
QUESTION 22:

QUESTION 23:
QUESTION 24:
Design a class to overload a function check() as follows:

i) void check(String str, char ch) – to find and print the frequency of a
character in a string.

Example :Input: Str = “success” ch = ‘s’

Output: number of s present is=3

ii) void check (String s1) – to display only the vowels from string s1 ,
after converting it to lower case.

Example :Input:S1= “computer”

Output: o u e

You might also like