2.5作业

一 、程序阅读题

1、给出下面程序输出结果。

#include <iostream.h>

class example

{int a;

public:

example(int b=5){a=b++;}

void print(){a=a+1;cout <<a<<"";}

void print()const

{cout<<a<<endl;}

};

void main()

{example x;

const example y(2);

x.print();

y.print();

}

6

2

2、运行程序,写出程序执行的结果。

#include<iostream.h>

class Location

{ public:

int X,Y;

void init(int initX,int initY);

int GetX();

int GetY();

};

void Location::init (int initX,int initY)

{X=initX;

Y=initY;

}

int Location::GetX()

{return X;

}

int Location::GetY()

{return Y;

}

void display(Location& rL)

{cout<<rL.GetX()<<" "<<rL.GetY()<<'\n';

}

void main()

{

Location A[5]={{5,5},{3,3},{1,1},{2,2},{4,4}};

Location *rA=A;

A[3].init(7,3);

rA->init(7,8);

for (int i=0;i<5;i++)

display(*(rA++));

}

循环结果为,第一行:73174,第二行:83134

3. 给出下面程序输出结果。

#include <iostream.h>

int a[8]={1,2,3,4,5,6,7};

void fun(int *pa,int n);

void main()

{int m=8;

fun(a,m);

cout<<a[7]<<endl;

}

void fun(int *pa,int n)

{for (int i=0;i<n-1;i++)

*(pa+7)+=*(pa+i);

}

28

4. 给出下面程序输出结果。

#include <iostream.h>

class A

{

int *a;

public:

A(int x=0):a(new int(x)){}

~A() {delete a;}

int getA() {return *a;}

void setA(int x) {*a=x;}

};

void main()

{

A x1,x2(3);

A *p=&x2;

(*p).setA(x2.getA()+5);

x1.setA(10+x1.getA());

cout<<x1.getA()<<""<<x2.getA()<<endl;

}

10

2

  1. 阅读下面的程序,写出运行结果:

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| #include < iostream.> using namespace std; class Samp { public: void Set_i_j(int a, int b){i=a,j=b;} ~Samp() { cout <<"Destroying.." << i <<endl; } int GetMulti () { return i * j; } protected: int i; int j; }; int main () { Samp * p; p = new Samp[l0]; if(!p) { cout << "Allocation error \ n"; return; } for(int j =0; j<l0; j ++) p[j]. Set_i_j (j, j); for(int k=0; k<l0; k++) cout <<"Multi[" <<k <<"] is:"<< p[k].GetMulti () <<endl; delete [ ] p; return 0; } |

  1. 写出下面程序的运行结果,请用增加拷贝构造函数的方法避免存在的问题。

|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| #include < iostream> using namespace std; class Vector { public: Vector (int s = 100); int& Elem(int ndx); void Display(); void Set (); ~Vector (); protected: int size; int* buffer; } Vector::Vector (int s) { buffer = new int [size = s]; for(int i = O; i<size; i + + ) buffer [i] = i* i; } int& Vector:: Elem(int ndx) { if(ndx< 0 || ndx> = size) { cout << "error in index" <<endl; exit (1); } return buffer [ndx]; } void Vector::Display () { for(int j =0; j< size; j ++) cout << buffer[j] <<endl; } void Vector:: Set () { for(int j =0; j<size; j++) buffer[j] = j + 1; } Vector:: ~ Vector() { delete [] buffer; } Vector::Vector(const Vector &other) { size=other.size; buffer=new int[size]; for(int i=0;i<size;i++) buffer[i]=other.buffer[i]; } int main() { Vector a(10); Vector b(a); a. Set (); b. Display (); return 0; } |

7.读下面的程序与运行结果,添上一个拷贝构造函数来完善整个程序。

|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| #include < iostream> using namespace std; class CAT { public: CAT(); CAT(const CAT&); ~CAT(); int GetAge() const (return * itsAge;) void SetAge(int age) { * itsAge = age; } protected: int * itsAge; }; CAT::CAT () { itsAge = new int; *itsAge = 5; } CAT::~CAT () { delete itsAge; itsAge = 0; } void main() { CAT frisky; cout << "frisky's age:" << frisky. GetAge() <<endl; cout <<"Setting frisky to 6... \ n"; frisky. SetAge ( 6 ); cout << "Creating boots from frisky \ n"; CAT boots(frisky); cout <<"frisky's age:" << frisky. GetAge() <<endl; cout << "boots'age:" << boons. GetAge () <<endl; cout << "setting frisk,, to 7 .... n"; frisky. SetAge (7); cout <<"frisky"s age:" << frisky. GetAge() <<endl; cout <<"boots' age:" << boots. GetAge() <<endl; } |

运行结果为:

frisky's age:5

Setting frisky to 6...

Creating boots from frisky

frisky's age:6

boots' age:6

Setting frisky to 7...

frisky's age:7

boots' age:6

相关推荐
John_ToDebug1 小时前
Chrome 内置扩展 vs WebUI:浏览器内核开发中的选择与实践
前端·c++·chrome
焊锡与代码齐飞1 小时前
嵌入式第三十五课!!Linux下的网络编程
linux·运维·服务器·开发语言·网络·学习·算法
省四收割者2 小时前
Go语言入门(10)-数组
数据结构·经验分享·笔记·vscode·算法·golang
lxmyzzs2 小时前
【图像算法 - 21】慧眼识虫:基于深度学习与OpenCV的农田害虫智能识别系统
人工智能·深度学习·opencv·算法·yolo·目标检测·计算机视觉
KeithTsui2 小时前
GCC C语言整数转换的理解(Understanding of Integer Conversions in C with GCC)
c语言·开发语言·算法
jiunian_cn2 小时前
【Linux】线程
android·linux·运维·c语言·c++·后端
jdlxx_dongfangxing4 小时前
C++ 序列式容器深度解析:vector、string、deque 与 list
c++·stl
欧阳小猜4 小时前
深度学习②【优化算法(重点!)、数据获取与模型训练全解析】
人工智能·深度学习·算法
小欣加油4 小时前
leetcode 904 水果成篮
c++·算法·leetcode
有Li4 小时前
CXR-LT 2024:一场关于基于胸部X线的长尾、多标签和零样本疾病分类的MICCAI挑战赛|文献速递-深度学习人工智能医疗图像
论文阅读·人工智能·算法·医学生