作业2024/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++));

}

7 8

3 3

1 1

7 3

4 4

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 8

  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 Sampl0; if(!p) { cout << "Allocation error \ n"; return; } for(int j =0; j<l0; j ++) pj. Set_i_j (j, j); for(int k=0; k<l0; k++) cout <<"Multi" \<\ is:"<< pk.GetMulti () <<endl; delete p; return 0; } Multi0 is: 0 Multi1 is: 1 Multi2 is: 4 Multi3 is: 9 Multi4 is: 16 Multi5 is: 25 Multi6 is: 36 Multi7 is: 49 Multi8 is: 64 Multi9 is: 81 Destroying..0 Destroying..1 Destroying..2 Destroying..3 Destroying..4 Destroying..5 Destroying..6 Destroying..7 Destroying..8 Destroying..9 |

  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 << bufferj <<endl; } void Vector:: Set () { for(int j =0; j<size; j++) bufferj = j + 1; } Vector:: ~ Vector() { delete \[\] buffer; } int main() { Vector a(10); Vector b(a); a. Set (); b. Display (); return 0; } 加的构造函数 #include <iostream> using namespace std; class Vector { public: Vector(int s = 100); Vector(const Vector& other); // 拷贝构造函数 int& Elem(int ndx); void Display(); void Set(); ~Vector(); protected: int size; int* buffer; }; Vector::Vector(int s) { buffer = new intsize = s; for (int i = 0; i < size; i++) bufferi = i * i; } Vector::Vector(const Vector& other) { size = other.size; buffer = new intsize; for (int i = 0; i < size; i++) bufferi = other.bufferi; } int& Vector::Elem(int ndx) { if (ndx < 0 || ndx >= size) { cout << "error in index" << endl; exit(1); } return bufferndx; } void Vector::Display() { for (int j = 0; j < size; j++) cout << bufferj << endl; } void Vector::Set() { for (int j = 0; j < size; j++) bufferj = j + 1; } Vector::~Vector() { delete\[\] buffer; } 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

添加

CAT::CAT(const CAT& other) {

itsAge = new int;

*itsAge = *other.itsAge;

}

CAT::~CAT() {

delete itsAge;

itsAge = 0;

}

相关推荐
可编程芯片开发几秒前
基于双PI控制器结构的六步逆变器供电无刷直流电机调速simulink仿真
算法
Sagittarius_A*22 分钟前
后量子密码|通识认知 02|量子威胁核心原理:Shor 算法如何击穿传统公钥密码
算法·信息安全·密码学·量子计算
青山木34 分钟前
Hot 100 --- 在排序数组中查找元素的第一个和最后一个位置
java·数据结构·算法·leetcode
其美杰布-富贵-李38 分钟前
聚类算法详解:K-means、层次聚类与 DBSCAN
算法·机器学习·kmeans·聚类
乱七八糟的屋子1 小时前
MTL4 (Matrix Template Library) 超详细实战教程|C++高性能稠密/稀疏矩阵计算
c++·矩阵·稀疏矩阵·mtl4·matrixtemplate·c++线性代数·高性能数值计算
碧海银沙音频科技研究院1 小时前
4 BES2710编译环境搭建方法
嵌入式硬件·算法
mifengxing2 小时前
LeetCode 238 除自身以外数组的乘积|无除法O(n)时间+O(1)空间双解法
java·算法·leetcode
依然鸣2 小时前
PTA团体程序设计天梯赛L2真题讲解L2-045-048
数据结构·c++·经验分享·学习·算法·pat考试·pat
有点。2 小时前
C++深度优先搜索(二)
开发语言·c++·深度优先
丢掉幻想准备斗争2 小时前
5.5树与二叉树的应用
算法