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

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

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;

}

答:

1

1

4

9

16

25

36

49

64

81

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;

}

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

相关文章:

  • 政安晨:示例演绎Python的函数与获取帮助的方法
  • 88 docker 环境下面 前端A连到后端B + 前端B连到后端A
  • k8s学习-Service Account和RBAC授权
  • SpringMVC-响应数据
  • 数学建模:数据相关性分析(Pearson和 Spearman相关系数)含python实现
  • 使用pandas将excel转成json格式
  • 双向链表的插入、删除、按位置增删改查、栈和队列区别、什么是内存泄漏
  • Linux 驱动开发基础知识——总线设备驱动模型(七)
  • RTthread线程间通信(邮箱,消息队列,信号/软件中断)---03信号(软件中断)源码分析
  • 老版本labelme如何不保存imagedata
  • vscode 如何修改c/c++格式化风格,大括号不换行
  • IP协议(2) 和 数据链路层协议基础
  • Flink-1.18.1环境搭建
  • deepin20.9安装及配置
  • 2-2 动手学深度学习v2-损失函数-笔记
  • 非springboot 使用aop 切面
  • MongoDB 字段中数据类型不一致序列化异常排查与处理
  • 网络安全简介
  • 【Docker】.NET Core 6.0 webapi 发布上传到Docker Desktop并启动运行访问,接口返回数据乱码解决方法
  • 【Android Gradle 插件】自定义 Gradle 插件模块 ⑤ ( 完整总结 )
  • 浅析现代计算机启动流程
  • 七月论文审稿GPT第2.5和第3版:分别微调GPT3.5、Llama2 13B以扩大对GPT4的优势
  • Android Studio导入项目 下载gradle很慢或连接超时
  • 如何使用VSCode上运行Jupyter,详细案例过程出可视化图
  • Linux中有名管道和无名管道
  • [SWPUCTF 2021 新生赛]easyupload1.0
  • 【Linux网络编程三】Udp套接字编程(简易版服务器)
  • 【Rust】字符串,看这篇就够了
  • 单片机和 ARM 的区别
  • JavaScript从入门到精通系列第三十一篇:详解JavaScript中的字符串和正则表达式相关的方法