第一章 控制台应用程序
for(;i } void set::Copy(set s){ int i; for(i=0;i bool set::Equal(set s){ int i; if(num!=s.num) return false; for(i=0;i if(s.Member(elements[i])) return false; return true; //有元素不同 //元素个数不同 } void set::print(){ cout<<\集合的元素包括:\ int i; for(i=0;i } void set::Intersect(set s1,set s2){ int i,j; s2.num=0; for(i=0;i for(j=0;j if(elements[i]==s1.elements[j]){ s2.elements[s2.num++]=elements[i]; break; } } ErrCode set::Union(set s1,set s2){ int i; s1.Copy(s2); for(i=0;i if(s2.AddElem(elements[i])==overflow) return overflow; return noErr; } bool set::Contain(set s){ int i; for(i=0;i if(!Member(s.elements[i])) return false; 26 C++程序设计实践教材 return true; } int main(){ int i; char ch[30]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N', 'O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d'}; set s,s1,s2,s3,s4; ErrCode b; for(i=0;i<10;i++){ b=s.AddElem(ch[i]); b=s1.AddElem(ch[2*i]); b=s2.AddElem(ch[3*i]); } cout<<\ cout<<\ cout<<\ s.print(); s1.print(); s2.print(); s.Intersect(s1,s3); b=s.Union(s1,s4); cout<<\ cout<<\ s3.print(); s4.print(); if(s3.Contain(s4)) cout<<\ else cout<<\ if(s4.Contain(s3)) cout<<\ else cout<<\ if(s3.Equal(s4)) cout<<\ else cout<<\ for(i=6;i<10;i++){ s.RmvElem(ch[i]); s1.RmvElem(ch[i]); s2.RmvElem(ch[i]); } cout<<\删除部分元素后:\ cout<<\ cout<<\ cout<<\ return 0; } s.print(); s1.print(); s2.print(); 2. 编程:建立一个分数类。分数类的数据成员包括分子和分母,操作包括 显示、输入、约分、通分、比较、加、减、乘、除、求相反数。分数类定义如下: class fraction{ int above; int below; void reduction(); //分子 //分母 //约分 27 第一章 控制台应用程序 void makeCommond(fraction); //通分 public: fraction(int=0,int=1); fraction add(fraction); fraction sub(fraction); fraction mul(fraction); fraction div(fraction); fraction reciprocal(); bool epual(fraction); bool lessThan(fraction); void display(); void input(); }; //构造函数 //两分数相加 //本分数减去实参分数 //两分数相乘 //本分数除以实参分数 //求倒数 //等于运算 //小于运算 //显示分数 //输入分数 bool greaterThan(fraction); //大于运算 完成所有成员函数并进行检验。 实验十一 引用与拷贝构造函数 一、实验目的 1. 掌握引用概念及应用,引用作函数参数。 2, 学会编写与应用拷贝构造函数。 二、实验内容 1. 范例:编写一个函数,其原型为:void Index(int a[], int n,int & sub),其功 能是,在大小为 n 的数组 a 中,查找某个数 sub,若找到,将其下标存放在 sub 中,若没找到,将-1 存放在 sub 中,在主调函数中通过判断值来判断数组中是否 有该数。在这里,sub 是引用类型的参数,但起返回值的作用。 #include void Index(int a[], int n,int & sub); int main(){ int b[25]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}; int n=25,sub; cout<<\输入小于100的正整数:\ cin>>sub; Index(b,n,sub); if(sub!=-1) cout<<\所在元素下标为:\ else cout<<\未找到。\ cout<<\再输入小于100的正整数:\ cin>>sub; Index(b,n,sub); if(sub!=-1) cout<<\所在元素下标为:\ else cout<<\未找到。\ return 0; 28 C++程序设计实践教材 } void Index(int a[], int n,int & sub){ int i,k=sub; sub=-1; for(i=0;i if(a[i]==k){ sub=i; break; } } 2. 范例(可选):一个声明为返回引用的函数,还可以用在赋值号的左边, 即这种函数调用本身是一个左值。 【程序】 #include index(3) = 16; for(int i=0;i<5;i++) cout << index(i)<<' '; cout< int& index(int i){ return array[i]; } [注意] 由于函数 index()是返回引用的类型,所以该函数调用可以作为一个左值, 程序的表达式 index(3)=16 将 16 赋给左边的函数调用,由于该函数调用返回对 array[3] 的引用,所以可以将 array[3]赋新值。该程序运行的结果将使得整型数 组 array 中的各元素值变为: 2,4,6,16,10。 3. 范例:为矩形类定义拷贝构造函数。矩形类见主教材例 4.2。 在 rect.h 中类 rect 定义中加: Rectangle(Rectangle & rec); 在rect.cpp中添加: Rectangle::Rectangle(Rectangle & rec){ left=rec.left; top=rec.top; right=rec.right; bottom=rec.bottom; }//拷贝构造函数 在 main()函数中添加: Rectangle rect2(rect1); 29 第一章 控制台应用程序 cout<<\由拷贝构造函数生成的rect2:\ rect2.Show(); 这里的拷贝构造函数是缺省的按成员语义定义的,只作为一个练习,可以不 写,系统会自动提供。 4. 编程:将实验十的集合类和分数类的集合类型或分数类型参数改为引用, 并添加拷贝构造函数。 实验十二 运算符重载 一、实验目的 1. 掌握运算符重载为成员函数的方法。 2. 理解友元函数和友元类。 3. 掌握运算符重载为友元函数的方法。 4. 使用静态数据成员。 二、实验内容 1. 范例:为主教材例题 4.2 的 Rectangle 类增加加减复合赋值和加减运算符 重载函数。加减复合赋值语义定义为固定长方形的左上角,对右下角的坐标进行 加减运算,使新矩形的长宽为原两矩形长宽之和或差,对加和减复合赋值定义为 成员函数。两个矩形加减运算定义为友元函数。 class Rectangle { int left, top ; int right, bottom; public: Rectangle(int l=0, int t=0, int r=0, int b=0); ~Rectangle(){}; //析构函数,在此函数体为空 void Assign(int l, int t, int r, int b); void SetLeft(int t){ left = t;} // 以下四个函数皆为内联成员函数 void SetRight( int t ){ right = t;} void SetTop( int t ){ top = t;} void SetBottom( int t ){ bottom = t;} void Show(); void operator +=(Rectangle&); void operator -=(Rectangle&); friend Rectangle operator+(Rectangle &, Rectangle&); friend Rectangle operator-(Rectangle &, Rectangle&); }; //将上述内容保存为rect.h #include Rectangle::Rectangle(int l , int t, int r, int b) { left = l; top = t; right = r; bottom = b; 30 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库C++程序设计实践样例(6)在线全文阅读。
相关推荐: