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

 

相关推荐

  1. git-<span style='color:red;'>5</span>

    git-5

    2024-02-07 12:22:02      51 阅读
  2. Webpack5

    2024-02-07 12:22:02       63 阅读
  3. CSS-5

    2024-02-07 12:22:02       55 阅读
  4. DevOps(5)

    2024-02-07 12:22:02       58 阅读
  5. 5《Linux》

    2024-02-07 12:22:02       53 阅读
  6. basicPython-5

    2024-02-07 12:22:02       44 阅读
  7. HTML<span style='color:red;'>5</span>

    HTML5

    2024-02-07 12:22:02      49 阅读
  8. JVM-<span style='color:red;'>5</span>

    JVM-5

    2024-02-07 12:22:02      44 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-07 12:22:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-07 12:22:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-07 12:22:02       82 阅读
  4. Python语言-面向对象

    2024-02-07 12:22:02       91 阅读

热门阅读

  1. 2024/2/6

    2024-02-07 12:22:02       49 阅读
  2. 计算机网络相关题目及答案(第四章)

    2024-02-07 12:22:02       56 阅读
  3. win11安装mysql8.3.0压缩包版 240206

    2024-02-07 12:22:02       54 阅读
  4. 拼音笔记笔记

    2024-02-07 12:22:02       52 阅读
  5. 什么是大模型

    2024-02-07 12:22:02       47 阅读
  6. #P12365. 相逢是首歌

    2024-02-07 12:22:02       43 阅读
  7. SQL--DQL

    SQL--DQL

    2024-02-07 12:22:02      38 阅读
  8. Postgresql自定义函数—表名作为函数参数

    2024-02-07 12:22:02       56 阅读
  9. postgresql 初始化驱动支持高可用配置

    2024-02-07 12:22:02       42 阅读
  10. Leetcode 377 组合总和 Ⅳ

    2024-02-07 12:22:02       46 阅读
  11. k8s-sercret

    2024-02-07 12:22:02       48 阅读
  12. linux系统非关系型数据库memcached

    2024-02-07 12:22:02       53 阅读