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

C++面向对象程序设计习题集(7)

来源:网络收集 时间:2019-04-14 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

return *this; }

Point Point::operator=(Point P) {

this->x-=p.x;this->y-=p.y; return *this; }

void main() {

Point pl(2,3),p2(3,4),p3(p2) cout<<\:p3.disp(); p3.offset(10,10);

cout<<\:\

cout<<\:\ <

cout<<\:\ p3-=p2;

cout<<\:\

p3=pl+p3; //先将pl+p3的结果放在p1中,然后赚给p3,所以pl=p3 cout<<\:\.disp(); p3=pl-p2;

cout<<\}

24.

编写一个对具有n个元素的数组x[]求最大值的程序,要求将求最大值的函数设 计成函数模板。 【知识点】:3.6 【参考分】:20分 【难易度】:C 【答案】:

#include template T max(T x[],int n) {

int 1;

T maxv=x[0]; for(i=1;i

void main() {

int a[]=(4,5,2,8,9,3};

double b[]={3.5,6.7,2,5.2,9.2};

cout<<\数组最大值:\,6)<

25.

编写一个函数模板,它返回两个值中较小者,同时要求能正确处理字符串。 【知识点】:3.6 【参考分】:20分 【难易度】:C 【答案】:设计一个函数模板templateT min(T a,T b),可以进行int、float和char等类型数据的处理,为了能正确处理字符串,添加一个重载函数专门处理字符串比较,即char*min(char *a,char *b)。程序如下:

#include #include template T min(T a,T b) {

return(a

char *min(char *a,char *b) {

return(strcmp(a,b)<0?a:b); }

void main() {

double a=3.56,b=8.23;

char s1[]=\,s2[]=\ cout<<\输出结果:\

cout<< \中较小者:\,b) <

26.

设计一个数组类模板Array,其中包含重载下标运算符函数,并由此产生模板类Array和Array,使用一些测试数据对其进行测试。 【知识点】:3.6 【参考分】:25分 【难易度】:B 【答案】:

#include #include template class Array

T *elems;

int size; public:

Array(int S); //构造函数 ~Array();

T& operator[](int); //重载下标运算符 void operator=(T); //重载等号运算符 };

template Array::Array(int S) {

size=s;

elems=new T[SiZe]; for(int i=0;i

template Array::~Array() {

delete elems; }

template T& Array::operator[](int index) {

return elems[index]; }

template void Array::operator=(T temp) {

for(int i=0;i

void main() {

int 1,n=10;

Array arrl(n); //产生整数型模板类及其对象arrl Array arr2(n); //产生字符型模板类及其对象arr2 for(i=0; i

arr1[i]='a'+i; //调用重载运算符 arr2[i]='a'+i; }

cout<<\ ASCII码 字符\; for(i=O;i

cout< 27.

一个Sample类模板的私有数据成员为T n,在该类模板中设计一个“operator==”重载运算符函数,用于比较各对象的n数据是否相等。 【知识点】:3.6 【参考分】:20分 【难易度】:C 【答案】:

#include template class Sample {

T n; public:

Sample(T i){n=i;}

int operator==(Sample &); };

template int Sample::operator==(Sample &S) {

if(n==s.n)

return 1; else return 0; }

void main()

{ Samples1(2),s2(3);

cout<<\与s2的数据成员\相等\:\不相等\ Samples3(2.5),s4(2.5);

cout<<\与s4的数据成员\相等\不相等\}

28.

设计一个模板类Sample,用于对一个有序数组采用二分法查找元素下标。 【知识点】:3.6 【参考分】:25分 【难易度】:B 【答案】:

#include #define Max 100 template class Sample T A[Max]; int n; public:

Sample(){}

Sample(T a[],int i);

int seek(T c); void disp() {

for (int i=0; itemplate Sample::Sample(T a[],int i) {

n=i;

for(int j=O; j

template int Sample::seek(T c)

int low=0,high=n-l,mid; while(low<=high) {

mid=(low+high)/2; if(A[mid]==c) return mid;

else if (A[mid]

return -1; }

void main() {

char a[]=”acegkmpwxz”; Samples(a,i0);

cout<<\元素序列:\.disp();

cout<<\元素'g'的下标:\.seek('g')<

29.

编写一个程序,设计一个点类Point,并由此派生出一个圆类。 【知识点】:5.1 5.2 【参考分】:20分 【难易度】:B 【答案】:

#include class Point {

int x,y; public:

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库C++面向对象程序设计习题集(7)在线全文阅读。

C++面向对象程序设计习题集(7).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/591154.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: