Question

In: Computer Science

Examine each of the following code fragments to identify the type of polymorphism it represents (overloading,...

Examine each of the following code fragments to identify the type of polymorphism it

represents (overloading, coercion, parametric, or subtype)

double x, y;

   x = 5;

   y = 4;  

   print("result= " + x / y);  

b.

int sum(int a, int b) {

   std::cout << "Sum of ints\n";  

   return a + b;  

   }  

   double sum(double a, double b) {

    std::cout << "Sum of doubles\n";

    return a + b;  

   }  

   int main() {  

     std::cout << "The sum is " << sum(7, 8) << std::endl;  

     std::cout << "The sum is " << sum(7.2, 8.2) << std::endl;  

   }  

Solutions

Expert Solution

The given code fragment represents to overloading polymorphism:

What is overloading polymorphism?

Overloading in Object oriented programing means the using the same name of functions in the program with different parameters and return types to perform different tasks.

Why given code has overloading:

The code uses sum() user defined method with two different type of agruments and return type i.e function sum is overloaded.

Below sum() function will accept int type parameter a and b and return an int type value

int sum(int a, int b){

std::cout<<”Sum of ints \n”;

return a+b;

}

Overloaded sum() function will accept double type parameter a and b and return a double type value

double sum(double a, double b){

std::cout<<”Sum of doubles \n”;

return a+b;

}

Thus from above description it can be stated that it is overloaded polymorphism.

Why the given fragment is not of type coercion, parametric, or subtype polymorphism:

· Coercion is a polymorphism where the type of a polymorphism where the type of an object is casted to another type. The given segment does not convert the type of variables, thus the code does not have this type of polymorphism

· Parametric polymorphism is a way to perform same action of any data type e.g. largest(1,2), largest(a,b) will return largest between two passed parameters. This is not the polymorphism type shared in given code fragment.

· SubType polymorphism used one base class and one or more derived class tp achive the goal of polymorphism. The given code does not have many classes in it, thus it is not of this type of polymorphism.


Related Solutions

Fill in the blanks with suitable code and identify the type of inheritance in the following...
Fill in the blanks with suitable code and identify the type of inheritance in the following code snippet.(CO5) [Write all the three constructors completely in the answer sheet. Don’t write any other parts of the program] class Xyz { int one, two; public: Xyz(--------) {one=i; --------; } }; class Abc { int dc; public: Abc(int k) {-----------;} }; class Ijk: public Abc, public Xyz { int f; public: Ijk(int l, int m, int z, int n):-------,--------- { f=n;} }; int...
1. Examine/Compare Plant and Animal Cells. 2. Identify the organelles within each type of cell and...
1. Examine/Compare Plant and Animal Cells. 2. Identify the organelles within each type of cell and explain their corresponding function(s). 3. Describe the structure and function of cell components, with emphasis on eukaryotes. 4. Discuss the evolutionary relationship of prokaryotic and eukaryotic cells.
I need to identify what type of study (experimental, correlational, quasi-experimental, or descriptive) each statement represents....
I need to identify what type of study (experimental, correlational, quasi-experimental, or descriptive) each statement represents. I don't understand how to identify the keywords that indicate which type of research or how to determine the type of research just by one sentence. 1. The purpose of this study was to examine the relationship among stress, anxiety, and blood pressure in persons with a family history of strokes. 2. The purpose of the study was to compare the effects of a...
Given the following three yield curves, explain the type of market each represents. With respect to...
Given the following three yield curves, explain the type of market each represents. With respect to the center yield-curve what does that shape indicate with respect to short-, medium- and long-term risk of the economy. Which yield curve is the ‘normal’ and which is the ‘inverted’? (One of the graphs is upward sloping exponentially, the middle graph is a parabola, and the last graph is downward sloping exponentially.)
Required Identify each of the following events as an accrual, a deferral, or neither Type of...
Required Identify each of the following events as an accrual, a deferral, or neither Type of Accounting Events EventsType of Accountinga.Paid cash in advance for a one-year insurance policy.b.Paid cash to settle an account payable.c.Collected accounts receivable.d.Paid cash for current salaries expense.e.Paid cash to purchase supplies.f.Provided services on account.g.Provided services and collected cash.h.Paid cash to purchase land.i.Recognized accrued salaries at the end of the accounting period.J.Paid a cash dividend to the stockholders.kRecognized revenue on account at the end of the period.l.Collected cash...
1 For each of the following variables, identify the type of variable (numeric or categorical) and...
1 For each of the following variables, identify the type of variable (numeric or categorical) and level of measurement (nominal, ordinal, interval, or ratio). a. Business size (micro, small, medium, large) b. Airplane ticket price 2 Consider the following sample of data: 1.2 28.4 32.6 17.0 13.5 2.2 38.0 41.4 1.9 14.4 21.8 23.8 a. Calculate the quartiles. b. Calculate the average c. Calculate the standard deviation. 3  Given the following five-number summary, are there any outliers in the data? Min:...
Question 33 For each of the following situations, identify the type of change, and whether the...
Question 33 For each of the following situations, identify the type of change, and whether the change (or correction) should be made prospectively or retrospectively. Type of Change Change (or correction) to be Made (a) When the company purchased its equipment, it estimated that the equipment would have a useful life of ten years. Now, eight years later, it appears that the equipment will be useful for an additional five years from the current date. Correction of a Prior Period...
For each of the following four diuretics, identify what type of diuretic it is and how...
For each of the following four diuretics, identify what type of diuretic it is and how it works. Indapamide: Bumetanide: Spirolactone: Osmitrol:
Consider the following actions for dealing with global climate change. Identify whether each represents adaptation or...
Consider the following actions for dealing with global climate change. Identify whether each represents adaptation or mitigation.                 a) Building up tall levees to stop the rising ocean from flooding a town. Mitigation Adaptation b) Rising the tax on gasoline. Adaptation Mitigation c) Giving a subsidy for people to buy hybrid cars. Adaptation Mitigation d) Giving a subsidy for people to buy air conditioners for their homes. Mitigation Adaptation
For each of the following Visual Basic code snippets, identify the syntax error.   If intX >...
For each of the following Visual Basic code snippets, identify the syntax error.   If intX > 100   lblResult.Text = "Invalid Data" End If   Dim str As String = "Hello" Dim intLength As Integer intLength = Length(str) If intZ < 10 Then   lblResult.Text = "Invalid Data"   Dim str As String = "123" If str.IsNumeric Then   lblResult.Text = "It is a number." End If   Select Case intX   Case < 0     lblResult.Text = "Value too low."   Case > 100     lblResult.Text = "Value too high."   Case Else     lblResult.Text = "Value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT