⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 练习.txt

📁 钱能主编 C++程序设计教程(第一版) 该书习题的答案代码
💻 TXT
字号:
14.1
#include <iostream.h>

class Samp
{
public:
	void Setij(int a,int b){i=a;j=b;}

	~Samp()
	{
		cout<<"Destroying..."<<i<<endl;
	}
	int GetMulti(){return i*j;}
protected:
	int i;
	int j;
};

void main()
{
	Samp* p;
	p=new Samp[10];
	if(!p)
	{
		cout<<"Allocation error"<<endl;
		return;
	}

	for(int j=0;j<10;j++)
		p[j].Setij(j,j);

	for(int k=0;k<10;k++)
	{
		cout<<"Multi["<<k<<"] is:"
			<<p[k].GetMulti()<<endl;
	}
	delete []p;
}

14.2
#include <iostream.h>
#include <stdlib.h>

class Vector
{
public:
	Vector(int s=100);
	Vector::Vector(Vector &v);
	int& Elem(int ndx);
	void Display();
	void Set();
	int GetSize()
	{
		int a=size;
		return a;
	}
	~Vector();
protected:
	int size;
	int* buffer;
};

Vector::Vector(int s)
{
	buffer= new int[size=s];
	if(!buffer)
	{
		cout<<"Allocation error!"<<endl;
		exit(1);
	}
	for(int i=0;i<size;i++)
		buffer[i]=i*i;
}

Vector::Vector(Vector &v)
{
	buffer=new int[size=v.GetSize()];
	for(int k=0;k<size;k++)
		buffer[k]=k*k;
}

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()
{
	cout<<"Destructing buffer:"<<buffer<<endl;
	delete []buffer;
}

void main()
{
	Vector a(10);
	Vector b(a);
	a.Set();
	b.Display();
}


14.3
#include <iostream.h>

class X
{
public:
	X(int a=10)
	{
		cout<<"constructing a new class "<<(i=a)
			<<" and its address is:"<<&i<<endl;
	}
	X(X &b);
	void dis()
	{
		cout<<"func dis:"<<i<<"	"<<&i<<endl;
	}
	~X()
	{
		cout<<"Destructing class "<<i<<"	"<<&i<<endl;
	}
protected:
	int i;
};

X::X(X &b)
{
	cout<<"copy a class of "<<b.i<<" ";
	i=b.i+5;
	cout<<"and the address of i in copy class is:"<<&i<<endl;
}

X f(X &x)
{
	cout<<"in func f()"<<"......	";
	cout<<"leave f()."<<endl;
	return x; 
}

void main()
{
	X a(1);
	a.dis();
	cout<<"-------------"<<endl;

	X b=f(X(2));
	b.dis();
	cout<<"-------------"<<endl;
	
	a=f(a);
	a.dis();
	cout<<"-------------"<<endl;
}

14.4
#include <iostream.h>

class CAT
{
public:
	CAT();
	~CAT();
	CAT(const CAT &c)
	{
		//cout<<"constructing a copy"<<"	";
		itsAge=new int;
		*itsAge=*(c.itsAge);
		//cout<<itsAge<<endl;
	}
	int GetAge() const
	{
		return *itsAge;
	}
	void SetAge(int age)
	{
		*itsAge=age;
	}
protected:
	int* itsAge;
};

CAT::CAT()
{
	//cout<<"constructing a new CAT"<<"	";
	itsAge=new int;
	*itsAge=5;
	//cout<<itsAge<<endl;
}
CAT::~CAT()
{
	delete itsAge;
	itsAge=0;
}

void main()
{
	CAT frisky;
	cout<<"frisky's age:"<<frisky.GetAge()<<endl;
	//cout<<"--------------------------"<<endl;

	cout<<"Setting frisky to 6 ..."<<endl;
	frisky.SetAge(6);
	//cout<<"--------------------------"<<endl;
	
	cout<<"Creating boots from frisky"<<endl;
	CAT boots(frisky);
	cout<<"frisky's age:"<<frisky.GetAge()<<endl;;
	cout<<"boots' age:"<<boots.GetAge()<<endl;
	//cout<<"--------------------------"<<endl;

	cout<<"Setting frisky to 7 ..."<<endl;
	frisky.SetAge(7);
	cout<<"frisky's age:"<<frisky.GetAge()<<endl;
	cout<<"boots' age:"<<boots.GetAge()<<endl;
	//cout<<"--------------------------"<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -