当前位置: 首页 > 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;

}

相关文章:

2024/2/5

第四章 堆与拷贝构造函数 一 、程序阅读题 1、给出下面程序输出结果。 #include <iostream.h> class example {int a; public: example(int b5){ab;} void print(){aa1;cout <<a<<"";} void print()const {cout<<a<<endl;} …...

政安晨:示例演绎Python的函数与获取帮助的方法

调用函数和定义我们自己的函数&#xff0c;并使用Python内置的文档&#xff0c;是成为一位Pythoner的开始。 通过我的上篇文章&#xff0c;相信您已经看过并使用了print和abs等函数。但是Python还有许多其他函数&#xff0c;并且定义自己的函数是Python编程的重要部分。 在本…...

88 docker 环境下面 前端A连到后端B + 前端B连到后端A

前言 呵呵 最近出现了这样的一个问题, 我们有多个前端服务, 分别连接了对应的后端服务, 前端A -> 后端A, 前端B -> 后端B 但是 最近的时候 却会出现一种情况就是, 有些时候 前端A 连接到了 后端B, 前端B 连接到了 后端A 我们 前端服务使用 nginx 提供前端 html, js…...

k8s学习-Service Account和RBAC授权

1.1 ServiceAccount 介绍 首先Kubernetes中账户区分为&#xff1a;User Accounts&#xff08;用户账户&#xff09; 和 Service Accounts&#xff08;服务账户&#xff09; 两种&#xff0c;它们的设计及用途如下&#xff1a; UserAccount是给kubernetes集群外部用户使用的&am…...

SpringMVC-响应数据

一、引子 我们在上一篇文章SpringMVC-组件解析里介绍了SpringMVC框架执行一个请求的过程&#xff0c;并演示了快速使用Controller承接请求。本篇我们将深入介绍SpringMVC执行请求时&#xff0c;如何响应客户端。 二、响应类型 SpringMVC的数据响应方式主要分为两类&#xff…...

数学建模:数据相关性分析(Pearson和 Spearman相关系数)含python实现

相关性分析是一种用于衡量两个或多个变量之间关系密切程度的方法。相关性分析通常用于探索变量之间的关系&#xff0c;以及预测一个变量如何随着另一个变量的变化而变化。在数学建模中&#xff0c;这是常用的数据分析手段。   相关性分析的结果通常用相关系数来表示&#xff…...

使用pandas将excel转成json格式

1.Excel数据 2.我们想要的JSON格式 {"0": {"raw_data1": "Sam","raw_data2": "Wong","raw_data3": "Good","layer": "12v1"},"1": {"raw_data1": "Lucy…...

双向链表的插入、删除、按位置增删改查、栈和队列区别、什么是内存泄漏

2024年2月4日 1.请编程实现双向链表的头插&#xff0c;头删、尾插、尾删 头文件&#xff1a; #ifndef __HEAD_H__ #define __HEAD_H__ #include<stdio.h> #include<stdlib.h> #include<string.h> typedef int datatype; enum{FALSE-1,SUCCSE}; typedef str…...

Linux 驱动开发基础知识——总线设备驱动模型(七)

个人名片&#xff1a; &#x1f981;作者简介&#xff1a;学生 &#x1f42f;个人主页&#xff1a;妄北y &#x1f427;个人QQ&#xff1a;2061314755 &#x1f43b;个人邮箱&#xff1a;2061314755qq.com &#x1f989;个人WeChat&#xff1a;Vir2021GKBS &#x1f43c;本文由…...

RTthread线程间通信(邮箱,消息队列,信号/软件中断)---03信号(软件中断)源码分析

信号 实际使用看这一个 #if defined(RT_USING_SIGNALS)rt_sigset_t sig_pending; /**< the pending signals 记录来了的信号 */rt_sigset_t sig_mask; /**< the mask bits of signal 记录屏蔽的信号 */rt_sigh…...

老版本labelme如何不保存imagedata

我的版本是3.16&#xff0c;默认英文且不带取消保存imagedata的选项。 最简单粗暴的方法就是在json文件保存时把传递过来的imagedata数据设定为None&#xff0c;方法如下&#xff1a; 找到labelme的源文件&#xff0c;例如&#xff1a;D:\conda\envs\deeplab\Lib\site-packages…...

vscode 如何修改c/c++格式化风格,大括号不换行

在Visual Studio Code&#xff08;VSCode&#xff09;中&#xff0c;若要修改C代码格式化的风格以实现大括号不换行&#xff0c;通常会借助于插件C/C扩展中的ClangFormat配置。以下是具体的步骤&#xff1a; 确保已安装了C/C扩展&#xff1a; 打开VSCode的扩展市场&#xff08;…...

IP协议(2) 和 数据链路层协议基础

IP协议续 1.路由选择 在复杂的网络结构中,我们需要找到一个通往终点的路线,这就是路由选择 举个例子:我们在没有手机导航之前,想去一个地方得是到一个地方问一下路的方式最终找到目的地 路由的过程,其实就是样子问路的过程 1.当IP数据包到达路由器的时候,会查看目的IP 2.路由器…...

Flink-1.18.1环境搭建

下载 下载flink安装包 Index of /dist/flink/flink-1.18.1 下载flink-cdc安装包 Release Release 3.0.0 ververica/flink-cdc-connectors GitHub 安装 添加环境变量 vi ~/.bash_profile export FLINK_HOME=/home/postgres/flink/flink-1.18.1 export PATH=$PATH:$FL…...

deepin20.9安装及配置

安装deepin20.9很简单&#xff0c;刻录u盘 安装 一路next apt install nginx global vim-nox debian11 使用apt安装php, 使php多版本共存_debain11 php5-CSDN博客 vim LeaderF安装问题 - 知乎 debian10安装vue环境, 包括安装node.js-CSDN博客 debian安装vue3 nodejs20-CSD…...

2-2 动手学深度学习v2-损失函数-笔记

损失函数&#xff0c;用来衡量预测值和真实值之间的区别。是机器学习里面一个非常重要的概念。 三个常用的损失函数 L2 loss、L1 loss、Huber’s Robust loss 均方损失 L2 Loss l ( y , y ′ ) 1 2 ( y − y ′ ) 2 l(y,y^{\prime})\frac{1}{2}(y-y^{\prime})^{2} l(y,y′)21…...

非springboot 使用aop 切面

在非Spring Boot应用中使用AOP&#xff08;Aspect Oriented Programming&#xff0c;面向切面编程&#xff09;的代码实现需要依赖Spring AOP库。由于Spring AOP库并不直接支持非Spring应用&#xff0c;你需要将Spring AOP库作为依赖项添加到项目中&#xff0c;并使用Spring AO…...

MongoDB 字段中数据类型不一致序列化异常排查与处理

MongoDB 字段中数据类型不一致序列化异常排查与处理 背景如下&#xff0c;因为项目迁移愿意&#xff0c;一个使用Mongodb的业务拥有C#和Java两组Api。Java Api开发和测试都很顺利。上线一段时间后&#xff0c;客服反馈记录都不见了。查看数据库发现&#xff0c;时间字段拥有两…...

网络安全简介

网络安全&#xff1a; ​ 网络安全攻击分为被动攻击和主动攻击。 1. 被动攻击&#xff1a;是指攻击者从网络上窃取了他人的通信内容&#xff0c;通常把这类的攻击称为截获&#xff0c;被动攻击只要有2种形式&#xff1a;消息内容泄漏攻击和流量分析攻击。由于攻击者没…...

【Docker】.NET Core 6.0 webapi 发布上传到Docker Desktop并启动运行访问,接口返回数据乱码解决方法

欢迎来到《小5讲堂》&#xff0c;大家好&#xff0c;我是全栈小5。 这是《Docker容器》系列文章&#xff0c;每篇文章将以博主理解的角度展开讲解&#xff0c; 特别是针对知识点的概念进行叙说&#xff0c;大部分文章将会对这些概念进行实际例子验证&#xff0c;以此达到加深对…...

基于数字孪生的水厂可视化平台建设:架构与实践

分享大纲&#xff1a; 1、数字孪生水厂可视化平台建设背景 2、数字孪生水厂可视化平台建设架构 3、数字孪生水厂可视化平台建设成效 近几年&#xff0c;数字孪生水厂的建设开展的如火如荼。作为提升水厂管理效率、优化资源的调度手段&#xff0c;基于数字孪生的水厂可视化平台的…...

04-初识css

一、css样式引入 1.1.内部样式 <div style"width: 100px;"></div>1.2.外部样式 1.2.1.外部样式1 <style>.aa {width: 100px;} </style> <div class"aa"></div>1.2.2.外部样式2 <!-- rel内表面引入的是style样…...

WordPress插件:AI多语言写作与智能配图、免费AI模型、SEO文章生成

厌倦手动写WordPress文章&#xff1f;AI自动生成&#xff0c;效率提升10倍&#xff01; 支持多语言、自动配图、定时发布&#xff0c;让内容创作更轻松&#xff01; AI内容生成 → 不想每天写文章&#xff1f;AI一键生成高质量内容&#xff01;多语言支持 → 跨境电商必备&am…...

莫兰迪高级灰总结计划简约商务通用PPT模版

莫兰迪高级灰总结计划简约商务通用PPT模版&#xff0c;莫兰迪调色板清新简约工作汇报PPT模版&#xff0c;莫兰迪时尚风极简设计PPT模版&#xff0c;大学生毕业论文答辩PPT模版&#xff0c;莫兰迪配色总结计划简约商务通用PPT模版&#xff0c;莫兰迪商务汇报PPT模版&#xff0c;…...

多模态图像修复系统:基于深度学习的图片修复实现

多模态图像修复系统:基于深度学习的图片修复实现 1. 系统概述 本系统使用多模态大模型(Stable Diffusion Inpainting)实现图像修复功能,结合文本描述和图片输入,对指定区域进行内容修复。系统包含完整的数据处理、模型训练、推理部署流程。 import torch import numpy …...

MySQL 部分重点知识篇

一、数据库对象 1. 主键 定义 &#xff1a;主键是用于唯一标识表中每一行记录的字段或字段组合。它具有唯一性和非空性特点。 作用 &#xff1a;确保数据的完整性&#xff0c;便于数据的查询和管理。 示例 &#xff1a;在学生信息表中&#xff0c;学号可以作为主键&#xff…...

Qt 事件处理中 return 的深入解析

Qt 事件处理中 return 的深入解析 在 Qt 事件处理中&#xff0c;return 语句的使用是另一个关键概念&#xff0c;它与 event->accept()/event->ignore() 密切相关但作用不同。让我们详细分析一下它们之间的关系和工作原理。 核心区别&#xff1a;不同层级的事件处理 方…...

ubuntu系统文件误删(/lib/x86_64-linux-gnu/libc.so.6)修复方案 [成功解决]

报错信息&#xff1a;libc.so.6: cannot open shared object file: No such file or directory&#xff1a; #ls, ln, sudo...命令都不能用 error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory重启后报错信息&…...

算法—栈系列

一&#xff1a;删除字符串中的所有相邻重复项 class Solution { public:string removeDuplicates(string s) {stack<char> st;for(int i 0; i < s.size(); i){char target s[i];if(!st.empty() && target st.top())st.pop();elsest.push(s[i]);}string ret…...

数据分析六部曲?

引言 上一章我们说到了数据分析六部曲&#xff0c;何谓六部曲呢&#xff1f; 其实啊&#xff0c;数据分析没那么难&#xff0c;只要掌握了下面这六个步骤&#xff0c;也就是数据分析六部曲&#xff0c;就算你是个啥都不懂的小白&#xff0c;也能慢慢上手做数据分析啦。 第一…...