20240205作业

第四章  堆与拷贝构造函数

一 、程序阅读题

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

#include 

using namespace std;

class example{

int a;

public:

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

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

void print()const{cout<

};

int main(){

example x;

const example y(2);

x.print();

y.print();

return 0;

}

6 2

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

#include 

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<

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 

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<

return 0;

}

void fun(int *pa,int n){

for (int i=0;i

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

}

28

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

#include 

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<

return 0;

}

10 8 

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

#include 

using namespace std;

class Samp{

public:

void Set_i_j(int a, int b){i=a,j=b;}

~Samp(){cout <<"Destroying.." << i <

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[" <

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 

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

        exit (1);

    }

    return buffer[ndx];

}

void Vector::Display(){

    for(int j =0jsizej ++)

        cout << buffer[j<;

}

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 

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

    cout << "Setting frisky to 6...\n";

    frisky.SetAge( 6 );

    cout << "Creating boots from frisky \n";

    CAT boots(frisky);

    cout << "frisky's age:" << frisky.GetAge() <;

    cout << "boots'age:" << boots.GetAge() <;

    cout << "setting frisky to 7...\n";

    frisky.SetAge(7);

    cout << "frisky's age:" << frisky.GetAge() <;

    cout << "boots' age:" << boots.GetAge() <;

}

运行结果为:

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

 

你可能感兴趣的:(算法,c++,开发语言)