C++实验5报告-面向对象程序设计 联系客服

发布时间 : 星期六 文章C++实验5报告-面向对象程序设计更新完毕开始阅读3a10b5bfeffdc8d376eeaeaad1f34693daef1091

Henan University of Technology Experiment’s Report

The 5th experiment Template

Lab Exercises

Class:

Name:

StuID:

Date: 2018-12-28

Score:

Lab Exercise — Overloading printArray

I Lab Objectives

In this lab, you will practice:

1) Overloading a function template for printing an array.

2) Using function templates to create function-template specializations.

II Description of the Problem

Overload function template printArray so that it takes two additional integer arguments, namely int lowSubscript and int highSubscript. A call to this function will print only the designated portion of the array. Validate lowSubscript and highSubscript; if either is out of range or if highSubscript is less than or equal to lowSubscript, the overloaded printArray function should return 0; otherwise, printArray should return the number of elements printed. Then modify main to demonstrate both versions of printArray on arrays a, b and c. Be sure to test all capabilities of both versions of printArray.

//Original program

// Using function-template specializations. #include using namespace std;

// function template printArray definition template< typename T >

void printArray( const T * const array, int count ) {

for ( int i = 0; i < count; ++i ) cout << array[ i ] << \ cout << endl;

} // end function template printArray

int main() {

const int aCount = 5; // size of array a const int bCount = 7; // size of array b const int cCount = 6; // size of array c

int a[ aCount ] = { 1, 2, 3, 4, 5 };

double b[ bCount ] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };

Henan University of Technology Experiment’s Report

The 5th experiment Template

char c[ cCount ] = \

cout << \

// call integer function-template specialization printArray( a, aCount );

cout << \

// call double function-template specialization printArray( b, bCount );

cout << \

// call character function-template specialization printArray( c, cCount ); } // end main

III Sample Output

IV Problem-Solving Tips

1. To overload the printArray function template, declare another function template, also named printArray, that takes two additional int parameters, lowSubscript and highSubscript.

2. When iterating over the range from lowSubscript to highSubscript, make sure to include both values within the range, to avoid an off-by-one error.

Henan University of Technology Experiment’s Report

The 5th experiment Template

V Your Solution

#include using std::cout; using std::endl;

template< typename T >

void printArray( const T *array, int count ){ for ( int i = 0; i < count; i++ ) cout << array[ i ] << \cout << endl; } template< typename T >

int printArray(const T *array,int count, int lowSubscrip, int highSubscript ){

if(highSubscript<=0||lowSubscrip<0||lowSubscrip>=highSubscript||count<(highSubscript-lowSubscrip)) return 0; int coun = 0;

for ( int i=lowSubscrip;i<=highSubscript;i++){ coun++;

cout << array[ i ] << ' '; } cout<< '\\n'; return coun; } int main() {

const int ACOUNT = 5; const int BCOUNT = 7; const int CCOUNT = 6;

int a[ ACOUNT ] = { 1, 2, 3, 4, 5 };

double b[ BCOUNT ] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 }; char c[ CCOUNT ] = \int elements;

cout << \printArray( a, ACOUNT ); cout << \

elements = printArray(a,ACOUNT,0,ACOUNT-1);

cout << elements << \elements = printArray(a,ACOUNT,1,3); cout << elements << \cout << \elements = printArray(a,ACOUNT,-1,10); cout << elements << \cout << \printArray( b, BCOUNT ); cout << \

elements = printArray(b,BCOUNT,0,BCOUNT - 1); cout << elements << \

Henan University of Technology Experiment’s Report

The 5th experiment Template

cout << \elements = printArray(b,BCOUNT,1,3); cout << elements << \cout << \elements =printArray(b,BCOUNT,-1,10); cout << elements << \cout << \printArray( c, CCOUNT ); cout << \

elements =printArray(c,CCOUNT,0, CCOUNT - 2); cout << elements << \cout << \elements =printArray(c,CCOUNT,1, 3); cout<< elements << \cout << \elements = printArray(c,CCOUNT,-1, 10);

cout << elements << \ return 0; }