第一范文网 - 专业文章范例文档资料分享平台

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

来源:用户分享 时间:2025/9/20 2:19:21 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

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; }

搜索更多关于: C++实验5报告-面向对象程序设计 的文档
C++实验5报告-面向对象程序设计.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c29k8l7hlvh7u3cm9b9nu86wqu5roq70039t_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top