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的函数与获取帮助的方法
调用函数和定义我们自己的函数,并使用Python内置的文档,是成为一位Pythoner的开始。 通过我的上篇文章,相信您已经看过并使用了print和abs等函数。但是Python还有许多其他函数,并且定义自己的函数是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中账户区分为:User Accounts(用户账户) 和 Service Accounts(服务账户) 两种,它们的设计及用途如下: UserAccount是给kubernetes集群外部用户使用的&am…...
SpringMVC-响应数据
一、引子 我们在上一篇文章SpringMVC-组件解析里介绍了SpringMVC框架执行一个请求的过程,并演示了快速使用Controller承接请求。本篇我们将深入介绍SpringMVC执行请求时,如何响应客户端。 二、响应类型 SpringMVC的数据响应方式主要分为两类ÿ…...
数学建模:数据相关性分析(Pearson和 Spearman相关系数)含python实现
相关性分析是一种用于衡量两个或多个变量之间关系密切程度的方法。相关性分析通常用于探索变量之间的关系,以及预测一个变量如何随着另一个变量的变化而变化。在数学建模中,这是常用的数据分析手段。 相关性分析的结果通常用相关系数来表示ÿ…...
使用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.请编程实现双向链表的头插,头删、尾插、尾删 头文件: #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 驱动开发基础知识——总线设备驱动模型(七)
个人名片: 🦁作者简介:学生 🐯个人主页:妄北y 🐧个人QQ:2061314755 🐻个人邮箱:2061314755qq.com 🦉个人WeChat:Vir2021GKBS 🐼本文由…...
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,默认英文且不带取消保存imagedata的选项。 最简单粗暴的方法就是在json文件保存时把传递过来的imagedata数据设定为None,方法如下: 找到labelme的源文件,例如:D:\conda\envs\deeplab\Lib\site-packages…...
vscode 如何修改c/c++格式化风格,大括号不换行
在Visual Studio Code(VSCode)中,若要修改C代码格式化的风格以实现大括号不换行,通常会借助于插件C/C扩展中的ClangFormat配置。以下是具体的步骤: 确保已安装了C/C扩展: 打开VSCode的扩展市场(…...
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很简单,刻录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-损失函数-笔记
损失函数,用来衡量预测值和真实值之间的区别。是机器学习里面一个非常重要的概念。 三个常用的损失函数 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(Aspect Oriented Programming,面向切面编程)的代码实现需要依赖Spring AOP库。由于Spring AOP库并不直接支持非Spring应用,你需要将Spring AOP库作为依赖项添加到项目中,并使用Spring AO…...
MongoDB 字段中数据类型不一致序列化异常排查与处理
MongoDB 字段中数据类型不一致序列化异常排查与处理 背景如下,因为项目迁移愿意,一个使用Mongodb的业务拥有C#和Java两组Api。Java Api开发和测试都很顺利。上线一段时间后,客服反馈记录都不见了。查看数据库发现,时间字段拥有两…...
网络安全简介
网络安全: 网络安全攻击分为被动攻击和主动攻击。 1. 被动攻击:是指攻击者从网络上窃取了他人的通信内容,通常把这类的攻击称为截获,被动攻击只要有2种形式:消息内容泄漏攻击和流量分析攻击。由于攻击者没…...
【Docker】.NET Core 6.0 webapi 发布上传到Docker Desktop并启动运行访问,接口返回数据乱码解决方法
欢迎来到《小5讲堂》,大家好,我是全栈小5。 这是《Docker容器》系列文章,每篇文章将以博主理解的角度展开讲解, 特别是针对知识点的概念进行叙说,大部分文章将会对这些概念进行实际例子验证,以此达到加深对…...
Qt Widget类解析与代码注释
#include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this); }Widget::~Widget() {delete ui; }//解释这串代码,写上注释 当然可以!这段代码是 Qt …...
【解密LSTM、GRU如何解决传统RNN梯度消失问题】
解密LSTM与GRU:如何让RNN变得更聪明? 在深度学习的世界里,循环神经网络(RNN)以其卓越的序列数据处理能力广泛应用于自然语言处理、时间序列预测等领域。然而,传统RNN存在的一个严重问题——梯度消失&#…...
在 Nginx Stream 层“改写”MQTT ngx_stream_mqtt_filter_module
1、为什么要修改 CONNECT 报文? 多租户隔离:自动为接入设备追加租户前缀,后端按 ClientID 拆分队列。零代码鉴权:将入站用户名替换为 OAuth Access-Token,后端 Broker 统一校验。灰度发布:根据 IP/地理位写…...
P3 QT项目----记事本(3.8)
3.8 记事本项目总结 项目源码 1.main.cpp #include "widget.h" #include <QApplication> int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); } 2.widget.cpp #include "widget.h" #include &q…...
镜像里切换为普通用户
如果你登录远程虚拟机默认就是 root 用户,但你不希望用 root 权限运行 ns-3(这是对的,ns3 工具会拒绝 root),你可以按以下方法创建一个 非 root 用户账号 并切换到它运行 ns-3。 一次性解决方案:创建非 roo…...
SpringBoot+uniapp 的 Champion 俱乐部微信小程序设计与实现,论文初版实现
摘要 本论文旨在设计并实现基于 SpringBoot 和 uniapp 的 Champion 俱乐部微信小程序,以满足俱乐部线上活动推广、会员管理、社交互动等需求。通过 SpringBoot 搭建后端服务,提供稳定高效的数据处理与业务逻辑支持;利用 uniapp 实现跨平台前…...
uniapp微信小程序视频实时流+pc端预览方案
方案类型技术实现是否免费优点缺点适用场景延迟范围开发复杂度WebSocket图片帧定时拍照Base64传输✅ 完全免费无需服务器 纯前端实现高延迟高流量 帧率极低个人demo测试 超低频监控500ms-2s⭐⭐RTMP推流TRTC/即构SDK推流❌ 付费方案 (部分有免费额度&#x…...
Spring Boot+Neo4j知识图谱实战:3步搭建智能关系网络!
一、引言 在数据驱动的背景下,知识图谱凭借其高效的信息组织能力,正逐步成为各行业应用的关键技术。本文聚焦 Spring Boot与Neo4j图数据库的技术结合,探讨知识图谱开发的实现细节,帮助读者掌握该技术栈在实际项目中的落地方法。 …...
AI编程--插件对比分析:CodeRider、GitHub Copilot及其他
AI编程插件对比分析:CodeRider、GitHub Copilot及其他 随着人工智能技术的快速发展,AI编程插件已成为提升开发者生产力的重要工具。CodeRider和GitHub Copilot作为市场上的领先者,分别以其独特的特性和生态系统吸引了大量开发者。本文将从功…...
IoT/HCIP实验-3/LiteOS操作系统内核实验(任务、内存、信号量、CMSIS..)
文章目录 概述HelloWorld 工程C/C配置编译器主配置Makefile脚本烧录器主配置运行结果程序调用栈 任务管理实验实验结果osal 系统适配层osal_task_create 其他实验实验源码内存管理实验互斥锁实验信号量实验 CMISIS接口实验还是得JlINKCMSIS 简介LiteOS->CMSIS任务间消息交互…...
