云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > C++期末考试复习题

C++期末考试复习题

  • 62 次阅读
  • 3 次下载
  • 2025/6/4 19:16:04

friend Complex operator+(Complex&, Complex&); //对“+”运算符进行重载

friend ostream& operator<<(ostream&, Complex&); //对“<<”运算符进行重载 friend istream& operator>>(istream&, Complex&); //对“>>”运算符进行重载 private:

double real,imag; };

要求:(1)写出该类的所有构造函数的类外定义代码。

(2)写出对运算符“+”、“<<”、“>>”进行重载的运算符重载函数的定义。

3. 编写程序,计算出复数对象c1和c2相加结果,并将其结果输出。

4. 从建立的数据文件f1.dat中读入10个整数放在数组中,找出并输出10个数中的最大者和它在数组中的序号。 解:

#include #include

using namespace std; int main( ) { }

cout<

for(i=1;i<10;i++)

if(a[i]>max)

{max=a[i]; order=i; }

cout<<\return 0;

int a[10],max,i,order;

fstream infile(\if( !infile ) { { }

infile>>a[i] ; cout<

cerr<<\for(i=0;i<10;i++)

5. 根据下面类中MaxMin 函数成员的原型和注释写出它的类外定义。 class AA { int* a; int n; int MS; public: void InitAA(int aa[], int nn, int ms) { if(nn>ms) {cout<<\ MS=ms; n=nn; a=new int[MS]; for(int i=0; i

int MaxMin(int& x, int& y); //从数组a的前n个元素中求出 //最大值和最小值,并分别由引用参数x和y带回, //同时若n大于0则返回1,否则返回0。 }; 解:

int AA::MaxMin(int& x, int& y) {

int mx,my; mx=my=a[0];

for(int i=1; imx) mx=a[i]; if(a[i]

x=mx; y=my;

if(n>0) return 1; else return 0; }

6. 根据下面类中构造函数的原型和注释写出它的类外定义。 class Array { int *a; //指向动态分配的整型数组空间 int n; //记录数组长度 public: Array(int aa[], int nn); //构造函数,利用aa数组长度nn初始化n, //利用aa数组初始化a所指向的数组空间

Array(Array& aa); //拷贝构造函数 Array& Give(Array& aa); //实现aa赋值给*this的功能并返回*this Array Uion(Array& aa); //实现*this和aa中的数组合并的 //功能,把合并结果存入临时对象并返回 int Lenth() {return n;} //返回数组长度 void Print() { //输出数组 for(int i=0; i

解:

Array::Array(Array& aa) {

n=aa.n;

a=new int[n];

for(int i=0; i

搜索更多关于: C++期末考试复习题 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

friend Complex operator+(Complex&, Complex&); //对“+”运算符进行重载 friend ostream& operator<<(ostream&, Complex&); //对“<>(istream&, Complex&); //对“>>”运算符进行重载 private: double real,imag; }; 要求:(1)写出该类的所有构造函数的类外定义代码。 (2)写出对运算符“+”、“<>”进行重载的运算符重载函数的定义。 3. 编写程序,计算出复数对象c1和c2相加结果,并将其结果输出。 4. 从建立的数据文件f1.dat中读入

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com