home work day5

第四章  堆与拷贝构造函数

 一 、程序阅读题

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

7

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

}

35

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;

}

0 1 4 9 16 25 36 49 64 81 

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;

}

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

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/670823.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

flutter使用qr_code_scanner扫描二维码

qr_code_scanner仓库地址&#xff1a;qr_code_scanner | Flutter Package 需要添加android和ios的相机权限和本地相册权限&#xff1a; android中添加权限: 在android\app\build.gradle中修改&#xff1a;minSdkVersion 20 并且在android/app/src/main/AndroidManifest.xml中…

【力扣】Z字形变换,模拟+直接构造

Z字形变换原题地址 方法一&#xff1a;利用二维矩阵模拟 对于特殊情况&#xff0c;z字形变换后只有一行或只有一列&#xff0c;则变换后的字符串和原字符串相同。 对于一般情况&#xff0c;我们可以考虑按照题目要求&#xff0c;把字符串按照Z字形存储到二维数组中&#xff…

【linux】git和gdb调试工具

在linux下提交代码同步到gitee 1.创建一个新的仓库&#xff08;演示步骤&#xff09; 2.init 这两个步骤用于识别提交代码的身份&#xff0c;一个你的名字&#xff0c;一个你的邮箱 开启本地仓库 克隆本地仓库成功 我们将这个仓库拷到了111目录底下. 我们发现少了一个.gitig…

测试python模块每个模块耗时

1.安装包 pip install snakeviz2. 运行测试 # 模块测试运行命令 PYTHONPATH$PWD python -m cProfile -o profile.stats_v2 new_core/schemes/cost_bom/bom_server.py #可视化运行的结果 python -m snakeviz profile.stats_v2 --server -H 0.0.0.0 -p 80813. 参数解释 cProfil…

最小覆盖子串[困难]

优质博文&#xff1a;IT-BLOG-CN 一、题目 给你一个字符串s、一个字符串t。返回s中涵盖t所有字符的最小子串。如果s中不存在涵盖t所有字符的子串&#xff0c;则返回空字符串"" 。 对于t中重复字符&#xff0c;我们寻找的子字符串中该字符数量必须不少于t中该字符数量…

使用Nginx搭建旁路服务器获取客户端真实IP

一、前言 在实际业务开发过程中&#xff0c;很多时候有记录客户端真实IP的需求&#xff0c;但是从客户端发送的请求往往会经过很多代理服务器&#xff0c;导致后端服务获取的IP为代理以后的IP&#xff0c;不具有业务含义。为了解决这个问题&#xff0c;可以搭建一个旁路服务器…

谷歌seo搜索引擎优化方法有什么?

想知道谷歌优化方法有什么&#xff0c;首先要了解谷歌搜索引擎的工作原理&#xff0c;谷歌的工作原理主要是通过“爬虫”来实现的&#xff0c;所谓“爬虫”就是一只能够读取并分析网页内容的程序&#xff0c;或者也能理解成机器人&#xff0c;当你在谷歌上输入关键词进行搜索时…

彻底学会系列:一、机器学习之线性回归(二)

0. 概念和公式 请参考&#xff1a;一、机器学习之线性回归&#xff08;一&#xff09; 1. 涉及公式 1.1 简单线性回归 y w x b y wx b ywxb 1.2 多元线性回归 y ^ w 1 X 1 w 2 X 2 . . . w n X n w 0 \hat y w_1X_1 w_2X_2 ... w_nX_n w_0 y^​w1​X1​w2​X2​.…

uniapp小程序实现直播组件live-player全屏问题

实现效果&#xff1a; 代码&#xff1a; <template><view class"player-content"><!-- #ifdef APP-PLUS --><video id"myVideo" :src"srcLink" autoplay controls><!-- 打开全屏 --><image class"img…

K8S-PV 与PVC

持久存储卷&#xff08;Persistent Volume&#xff0c;PV&#xff09; PV 是k8s管理员定义的好的物理存储或者说实际存储&#xff0c;对应用来说是透明的&#xff0c;应用只需要向着PVC申请即可&#xff0c;具体使用的创建好的那个PV是由PVC去匹配和绑定的。 PV是集群中的定义…

速度规划:s形曲线------pencv c++绘图(1)

理论篇 代码篇&#xff1a; opencv环境配置 注意&#xff01;注意&#xff01;注意&#xff01; 配置结束后运行环境切换为如下再运行&#xff1a; #include <iostream> #include <cmath>#include <opencv2/opencv.hpp>using namespace std;double a_max…

github和gitee

github GitHub是一个面向开源及私有软件项目的托管平台&#xff0c;因为只支持Git作为唯一的版本库格式进行托管&#xff0c;故名GitHub。 github可以给提交的代码打上标签&#xff0c;方便版本的迭代和回退&#xff0c;也是一个存储代码的仓库 github工作区 gitee是gitHub的…

蓝桥杯---分小组

9名运动员参加比赛,需要分3组进行预赛. 有哪些分组的方案呢? 我们标记运动员为 A,B,C .... I 下面的程序列出了所有的分组方法。 该程序的正常输出为:

【CSS】margin塌陷和margin合并及其解决方案

【CSS】margin塌陷和margin合并及其解决方案 一、解决margin塌陷的问题二、避免外边距margin重叠&#xff08;margin合并&#xff09; 一、解决margin塌陷的问题 问题&#xff1a;当父元素包裹着一个子元素且父元素没有边框的时候&#xff0c;当给子元素设置margin-top:100px&…

JS实现一键复制、选中复制、选中多行复制

JS实现一键复制 首先我们准备一份通用的一键复制代码&#xff1a; export function copyTextFun(text) {if (!text) return falsevar textarea document.createElement(textarea) textarea.value text document.body.appendChild(textarea) textarea.select() message.dest…

【精选】java继承进阶——继承的特点 this、super

&#x1f36c; 博主介绍&#x1f468;‍&#x1f393; 博主介绍&#xff1a;大家好&#xff0c;我是 hacker-routing &#xff0c;很高兴认识大家~ ✨主攻领域&#xff1a;【渗透领域】【应急响应】 【python】 【VulnHub靶场复现】【面试分析】 &#x1f389;点赞➕评论➕收藏…

5 款提升 UI 设计效率的软件工具

你知道如何选择正确的UI设计软件吗&#xff1f;你知道设计漂亮的用户界面和带来良好用户体验的应用程序需要什么界面设计软件吗&#xff1f;基于APP界面的不同功能&#xff0c;所选择的APP界面设计软件也会有所不同。然而&#xff0c;并不是说所有的APP界面设计软件都非常精通&…

【安卓中kotlin 泛型的基本用法】

泛型的基本用法 泛型主要有两种定义方式&#xff0c;一种是定义泛型类&#xff0c;另一种是定义泛型方法&#xff0c;使用的语法结构都是。当然括号内的T并不是固定要求的&#xff0c;事实上你使用任何英文字母或单词都可以&#xff0c;但是通常情况下&#xff0c;T是一种约定…

VMware16安装CentOS7mini 中遇到的一些问题

1、安装后网络不通解决方法&#xff1a; 修改这个文件/etc/sysconfig/network-script/ifcfg-ens33下ONBOOTno这个选项为yes&#xff1b;重启网络service network restartping baidu.com 2、安装的CentOS mini 系统少了很多软件需要安装&#xff0c;联网后一般采用网络安装 y…

Leetcode02.05:链表求和

一、题目描述 给定两个用链表表示的整数&#xff0c;每个节点包含一个数位。 这些数位是反向存放的&#xff0c;也就是个位排在链表首部。 编写函数对这两个整数求和&#xff0c;并用链表形式返回结果。 示例&#xff1a; 输入&#xff1a;(7 -> 1 -> 6) (5 -> 9 -…