Sdut Object-Oriented Programming Practice 2 (function template)

Source: Internet
Author: User

Sdut Object-Oriented Programming Practice 2 (function template)

Object-Oriented Programming machine Exercise 2 (function template) Time Limit: 1000 MS Memory limit: 65536 K The topic description uses arrays and function templates to calculate the maximum number of five numbers (considering integers, single precision, and long integers respectively ). Enter five int integers, five float integers, and five long integers. The output outputs the maximum values of five int integers, the maximum values of five float integers, and the maximum values of five long integers. Sample Input
11 22 666 44 5511.11 22.22 33.33 888.88 55.551234567 222222 333333 444444 555555
Sample output
666888.881234567


Using function templates for the same question is much easier than using function overloading.

The code is both readable and concise ..

# Include
 
  
Using namespace std; // template declaration. T is the type parameter template.
  
   
// Define a general function. Use T as the virtual type name T max (T a []) {int I; T m = 0; for (I = 0; I <5; I ++) {if (a [I]> m) m = a [I];} return m ;}// main function int main () {int I; int x [5], X; float y [5], Y; long z [5], Z; // input for (I = 0; I <5; I ++) cin> x [I]; for (I = 0; I <5; I ++) cin> y [I]; for (I = 0; I <5; I ++) cin> z [I]; // template call X = max (x); Y = max (y ); Z = max (z); // output cout <X <endl; cout <Y <endl; cout <Z <endl; return 0 ;}
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.