Saved Bookmarks
| 1. |
Here are some desired effects. Indicate whether each can be accomplished with default arguments, with function overloading, with both, or which neither. Provide appropriate prototypes.(i) repeat (10, ‘-‘) displays the indicated character (‘-‘ in this case) given number of times (10 here). While repeat() displays ‘*’ character 12 times. Also repeat(‘#’) displays the given character (‘#’ here) 12 times and repeat (7) displays ‘*’ given no of times (7 here).(ii) average (4,7) returns int average of two int arguments, while average (4.0, 7.0) returns the double average of two double values.(iii) mass (density, volume) returns the mass of an object having a density of density and a volume of volume, while mass (density) returns the mass having a density of density and a volume of 1.0 cubic meters. All quantities are type double.(iv) average (4,7) returns an int average of the two int arguments when called is one file, and it returns a double average of the two int arguments when called in a second file in the same program.(v) handle (a-character) returns the reversed case of the passed character or prints it twice depending upon whether you assign the return value to it or not. |
|
Answer» (i) It can be accomplished with function overloading. void repeat(int n, char c); void repeat(); void repeat(char c); void repeat(int n); (ii) It can be accomplished with function overloading. int average(int a,int b); double average(double a,double b); (iii) It can be accomplished with default argument. double mass (density d, volume v); double mass(density d, volume v=1.0); (iv) It can be accomplished with function overloading. int average(int a,int b); double average(int c,int d); (v) It can be accomplished with function overloading. char handle(char a); void handle(char a); |
|