Question

1) Discuss Overloaded Subprograms concept in C++, C# and Java, with examples and conclusion 2) Discuss...

1) Discuss Overloaded Subprograms concept in C++, C# and Java, with examples and conclusion

2) Discuss Generic Subprograms Subprograms concept in C++, C# and Java, with examples and conclusion.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. Overloaded Subprograms:

  • These are those subprograms where the name of one function is the same as that of another in the same referencing environment.
  • The result of how the function responds will depend on the type of operands.
  • For example, if there is an operator named +, so if the operands are given as float value then this will add two float numbers and if the integer is passed in the function then it will add two integers value. So, the same function as having multiple meanings.
  • These subprograms differ in the order, the number of parameters, type of parameters or in return type.
  • The overloading of functions is allowed in C, C#, and Java.

Example Code:
Java:

public class addition {
//function which will take two integer values
public int addition(int x, int y)
{
return (x + y);
}
  
//function which will take two double values.
public double addition(double x, double y)
{
return (x + y);
}
//main code
public static void main(String args[])
{
addition s = new addition();
System.out.println(s.addition(1, 2));
System.out.println(s.addition(1.5, 2.5));
}
}

Screenshot :

C++
#include <iostream>
using namespace std;
//overloaded class
class additionData {
public:
//function which take two integer input
int addition(int x, int y) {
return x+y;
}
//method which takes two float
float addition(float x,float y) {
return x+y;
}
};
//main
int main(void) {
additionData s;

// Call print to print integer
cout<<s.addition(1,2);
cout<<"\n";
cout<<s.addition(1.5f,2.5f);
}

Screenshot:

C#
using System;
public class additionData {
  
// method which will take two integer values
public int add(int x, int y)
{
return x + y;
}
//method which will take 2 float values
public float Add(float x,float y)
{
return (x+y);
}
//main
public static void Main(String[] args)
{
additionData s = new additionData();
  
int sum1 = s.add(1, 2);
Console.WriteLine(sum1);
  
float sum2 = s.Add(1.5f, 2.5f);
Console.WriteLine(sum2);
}
}


Screenshot:

Conclusion:

  • In all these languages, we have defined two functions with the same name "addition" which is having a different type of input parameters (integer and float).
  • The respective function is called according to the type of parameter which is used to call the method.
  • Thus, overloading is having same function name but having multiple meanings. And each function differs by number of parameters, type of parameters or the return type of methods.

2. Generic Subprograms

  • These are those subprograms which are used when we have to pass different types of parameters into the method.
  • The same function needs to be performed on all types of parameters. So, we will define only one function which will take all different input parameters and perform the operation.
  • For example, when we need to sort the elements of an array where those elements can be float or integer. So, here only parameter types differ but the operation which needs to perform is the same. Hence, generic subprograms come handy.
  • Generic Subprograms provide parametric polymorphism that takes the generic parameters.
  • Generic sub programming is allowed in C#, C++, and Java. In C++, this is implemented using templates whereas, in C# and Java, we have defined a class named generic.

Examples
Java

//generic class
class TestClass<T>
{
//object of generic class
T o;
//constructor of generic class
TestClass(T o) { this.o = o; }
public T getObject() { return this.o; }
}
//main class
public class Main
{
public static void main (String[] args)
{
//integer type
TestClass <Integer> io = new TestClass<Integer>(15);
System.out.println(io.getObject());

//string type
TestClass <String> so =
new TestClass<String>("Java code");
System.out.println(so.getObject());
}
}

Screenshot:

C++

#include <iostream>
using namespace std;
//template to print the values
template <typename T>
//print function
T print(T x)
{
return x;
}
// main   
int main()
{
cout << print<int>(15) << endl;
cout << print<string>("C++ code") << endl;
return 0;
}

Screenshot:

C#
//generic class
public class TestClass<T> {
private T data;
  
// using properties
public T value
{
  
// using accessors
get
{
return this.data;
}
set
{
this.data = value;
}
}
}
// main
class Test {
// Main method
static void Main(string[] args)
{
// string type
TestClass<string> n = new TestClass<string>();
n.value = "C # code";
// integer type
TestClass<float> v = new TestClass<float>();
v.value = 5.0F;
Console.WriteLine(n.value);
Console.WriteLine(v.value);
}
}

Screenshot:

Conclusion:

  • In these above all codes, we have defined a generic class which will take input of type integer and string and it will print that data.
  • So, generic subprograms are those programs which have the same functions but differ only in the types of parameter. So, we will write only one function to work on these types.

Friend, this was a really nice question to answer. If you find my answer helpful, please like it. Thanks.

Add a comment
Know the answer?
Add Answer to:
1) Discuss Overloaded Subprograms concept in C++, C# and Java, with examples and conclusion 2) Discuss...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT