20240205作业

2024-02-06 03:28
文章标签 作业 20240205

本文主要是介绍20240205作业,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

第四章  堆与拷贝构造函数

一 、程序阅读题

1、给出下面程序输出结果。

#include <iostream>

using namespace std;

class example{

int a;

public:

example(int b=5){a=b++;}

void print(){a=a+1;cout<<a<<"";}

void print()const{cout<<a<<endl;}

};

int main(){

example x;

const example y(2);

x.print();

y.print();

return 0;

}

6 2

2、运行程序,写出程序执行的结果。

#include <iostream>

using namespace std;

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';}

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

return 0;

}

7 8

3 3

1 1

7 3

4 4

3. 给出下面程序输出结果。

#include <iostream>

using namespace std;

int a[8]={1,2,3,4,5,6,7};

void fun(int *pa,int n);

int main(){

int m=8;

fun(a,m);

cout<<a[7]<<endl;

return 0;

}

void fun(int *pa,int n){

for (int i=0;i<n-1;i++)

*(pa+7)+=*(pa+i);

}

28

4. 给出下面程序输出结果。

#include <iostream>

using namespace std;

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

};

int main(){

A x1,x2(3);

A *p=&x2;

(*p).setA(x2.getA()+5);

x1.setA(10+x1.getA());

cout<<x1.getA()<<""<<x2.getA()<<endl;

return 0;

}

10 8 

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

#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[10];

if(!p){

cout << "Allocation error \n";

return -1;

}

for(int j=0; j<10; j++)

    p[j].Set_i_j(j,j);

for(int k=0; k<10; 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

 

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

#include <iostream>

using namespace std;

class Vector{

public:

    Vector (int s = 100);

    intElem(int ndx);

    void Display();

    void Set ();

    ~Vector ();

protected:

    int size;

    intbuffer;

};

Vector::Vector (int s){

    buffer = new int [size = s];

    for(int i = 0; i<sizei++)

        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 =0jsizej ++)

        cout << buffer[j<<endl;

}

void Vector::Set(){

    for(int j =0j<sizej++)

        buffer[j] = j + 1;

}

Vector::~Vector(){

    delete [] buffer;

}

int main(){

    Vector a(10);

    Vector b(a);

    a.Set();

    b.Display();

    return 0;

}

1

2

3

4

5

6

7

8

9

10

  1. 读下面的程序与运行结果,添上一个拷贝构造函数来完善整个程序。

#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(const CAT& cat):itsAge(new int(*cat.itsAge)){}

CAT::~CAT(){

    delete itsAge;

    itsAge = 0;

}

int 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:" << boots.GetAge() <<endl;

    cout << "setting frisky 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

 

这篇关于20240205作业的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/683006

相关文章

作业提交过程之HDFSMapReduce

作业提交全过程详解 (1)作业提交 第1步:Client调用job.waitForCompletion方法,向整个集群提交MapReduce作业。 第2步:Client向RM申请一个作业id。 第3步:RM给Client返回该job资源的提交路径和作业id。 第4步:Client提交jar包、切片信息和配置文件到指定的资源提交路径。 第5步:Client提交完资源后,向RM申请运行MrAp

Java高级Day38-网络编程作业

112.网络编程作业 //1.使用字符流的方式,编写一个客户端程序和服务器端程序//2.客户端发送"name",服务器端接收到后,返回"我是nova"//3.客户端发送"hobby",服务器端接收到后,返回"编写java程序"//4.不是这两个问题,回复"你说啥呢"​​===============//客户端//===============public class SocketT

0906作业+思维导图梳理

一、作业: 1、创捷一个类似于qq登录的界面 1)源代码 #include "widget.h"#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget){ui->setupUi(this);//QPushbutton:登录、退出this->join = new QP

2024.9.6 作业

1> 手写unique_ptr指针指针 #include <iostream>using namespace std;template <typename T>class my_unique_ptr{public:explicit my_unique_ptr(T *p = nullptr) noexcept // 构造函数{ptr = p;}~my_unique_ptr() noexcep

9月6号作业

1:.h文件 #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QWidget> #include<QIcon> //图标类 #include<QLabel> //标签类 #include<QMovie> //动图类 #include<QLineEdit> //行编辑器类

Flink实例(六十九): flink 作业提交(四)总结

独立集群提交 # 启动集群bin/start-cluster.sh# 提交job./bin/flink run ./examples/batch/WordCount.jar --input hdfs:/user/yuan/input/wc.count --output hdfs:/user/yuan/swwwttt yarn session # 启动集群./bin/

【#第三期实战营闯关作业 ## 茴香豆:企业级知识库问答工具】

今天学习了《 茴香豆:企业级知识库问答工具》这一课,对大模型的应用有了更深得认识。以下是记录本课实操过程及截图: 搭建茴香豆虚拟环境: 输入以下命令 ``studio-conda -o internlm-base -t huixiangdou 成功安装虚拟环境截图 安装茴香豆 cd /root 克隆代码仓库 git clone https://github.com/internlm/h

Quartz 作业调度器

1、Quartz  java实现  注:这里使用的是Quartz1.6.5版本(包:quartz-1.6.5.jar)   [java]  view plain copy //测试main函数   //QuartzTest.java   package quartzPackage;         import java.text.SimpleDateFormat

清华MEM作业-利用管理运筹学的分析工具slover求解最优解的实现 及 通过使用文件或者套节字来识别进程的fuser命令

一、清华MEM作业-利用管理运筹学的分析工具slover求解最优解的实现         最近又接触了一些线性求解的问题,以前主要都是在高中数学里接触到,都是使用笔算,最后通过一些函数式得出最小或者最大值,最近的研究生学业上接触到了一个Excel solver分析工具,对这种线性求最优解的问题感觉使用起来真是得心应手。在使用这个工具前,EXCEL里需要先装上solver工具,装起来很也简单,网上

opencv作业

作业下载地址: 链接:http://pan.baidu.com/s/1qYQnbkw 密码:v7y9