当前位置: 首页 > news >正文

作业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,3

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;

}

答:108

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

#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;

}

 答:

Multi[0] is: 0

Multi[1] is: 1

Multi[2] is: 4

Multi[3] is: 9

Multi[4] is: 16

Multi[5] is: 25

Multi[6] is: 36

Multi[7] is: 49

Multi[8] is: 64

Multi[9] is: 81

Destroying..9

Destroying..8

Destroying..7

Destroying..6

Destroying..5

Destroying..4

Destroying..3

Destroying..2

Destroying..1

Destroying..0

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

#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;

}

int main()

{

    Vector a(10);

    Vector b(a);

    a. Set ();

    b. Display ();

 

return 0;

}

 答:

结果:0 1 4 9 16 25 36 49 64 81

拷贝构造函数:

Vector::Vector(const Vector& other) :size(other.size),buffer(other,buffer) {}

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(other.itsAge){}

http://www.lryc.cn/news/294917.html

相关文章:

  • LeetCode、790. 多米诺和托米诺平铺【中等,二维DP,可转一维】
  • Python 的 sys 模块常用方法
  • Kafka 使用手册
  • STM32F407移植OpenHarmony笔记7
  • cortexM c语言和汇编嵌套编程
  • 国外传输大文件必选工具
  • Redis渗透SSRF的利用
  • 【深度学习】基于PyTorch架构神经网络学习总结(基础概念基本网络搭建)
  • 专业排版设计软件:QuarkXPress 2024 for mac中文激活版
  • 3593 蓝桥杯 查找最大元素 简单
  • Postgresql数据库存储过程中的事务处理
  • Redis——缓存的持久化
  • kafka-splunk数据通路实践
  • C语言第十九弹---指针(三)
  • TCP/IP LWIP FPGA 笔记
  • 2024年海外优青项目申报指南
  • threejs之常用贴图
  • Unity类银河恶魔城学习记录3-1 EnemyStateMachine源代码 P47
  • 使用webstorm调试vue 2 项目
  • 深度学习缝模块怎么描述创新点?(附写作模板+涨点论文)
  • html,css,js速成
  • 《Docker极简教程》--Docker基础--基础知识(一)
  • Web html和css
  • Three.js学习6:透视相机和正交相机
  • ❤ React18 环境搭建项目与运行(地址已经放Gitee开源)
  • 2024 RTE行业(实时互动行业)人才发展学习总结
  • 92.网游逆向分析与插件开发-游戏窗口化助手-显示游戏数据到小助手UI
  • Stable Diffusion 模型下载:majicMIX fantasy 麦橘幻想
  • docker compose安装minio
  • 二、SSM 整合配置实战