假期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 3

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

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

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

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| #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; } 拷贝构造函数: Vector::Vector(const Vector& other){ size = other.size; buffer = new int[size]; for(int i=0;i<size;i++) buffer[i] = other.buffer[i]; } 结果: 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; } |

拷贝构造函数:

CAT::CAT(const CAT& other) {

itsAge = new int;

*itsAge = *other.itsAge;

}

运行结果为:

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

相关推荐
Dontla31 分钟前
Rust泛型系统类型推导原理(Rust类型推导、泛型类型推导、泛型推导)为什么在某些情况必须手动添加泛型特征约束?(泛型trait约束)
开发语言·算法·rust
Ttang2337 分钟前
Leetcode:118. 杨辉三角——Java数学法求解
算法·leetcode
喜欢打篮球的普通人37 分钟前
rust模式和匹配
java·算法·rust
java小吕布1 小时前
Java中的排序算法:探索与比较
java·后端·算法·排序算法
tumu_C1 小时前
C++模板特化实战:在使用开源库boost::geometry::index::rtree时,用特化来让其支持自己的数据类型
c++·开源
杜若南星1 小时前
保研考研机试攻略(满分篇):第二章——满分之路上(1)
数据结构·c++·经验分享·笔记·考研·算法·贪心算法
路遇晚风1 小时前
力扣=Mysql-3322- 英超积分榜排名 III(中等)
mysql·算法·leetcode·职场和发展
Neophyte06081 小时前
C++算法练习-day40——617.合并二叉树
开发语言·c++·算法
木向1 小时前
leetcode104:二叉树的最大深度
算法·leetcode