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

📄 堆与拷贝构造函数.txt

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

class Student
{
public:
	Student(char* pName="noName",int ssId=0)
	{
		id=ssId;
		strcpy(name,pName);
		cout<<"Constructing new student "<<pName<<endl;
	}
	Student(Student &s)
	{
		cout<<"Constructing copy of "<<s.name<<endl;
		strcpy(name,"copy of ");
		strcat(name,s.name);
		id=s.id;
	}
	void dis()
	{
		cout<<name<<endl;
	}
	~Student()
	{
		cout<<"Destrcuting "<<name<<endl;
	}
protected:
	char name[40];
	int id;
};

void fn(Student s)
{
	cout<<"In function fn()"<<endl;
	cout<<"now the name of randy is ";
	s.dis();
}

void main()
{
	Student randy("Randy",1234);
	randy.dis();
	cout<<"Calling fn()"<<endl;
	fn(randy);
	cout<<"Returned from fn()"<<endl;
}

//**************************
//**      ch14_2.cpp      **
//**************************
#include <iostream.h>
#include <string.h>

class Student
{
public:
	Student(char* pName="no name")
	{
		cout<<"Constructing new student "<<pName<<endl;
		strncpy(name,pName,sizeof(name));
		name[sizeof(name)-1]='\0';
	}
	Student(Student &s)
	{
		cout<<"Constructing copy of "<<s.name<<endl;
		strcpy(name,"copy of ");
		strcat(name,s.name);
	}
	void dis()
	{
		cout<<name<<endl;
	}
	~Student()
	{
		cout<<"Destructing "<<name<<endl;
	}
protected:
	char name[40];
};

class Tutor
{
public:
	Tutor(Student &s):student(s)
	{
		cout<<"Constructing tutor"<<endl;
	}
	void disT()
	{
		cout<<"In tutor"<<endl;
		student.dis();
	}
protected:
	Student student;
};

void fn(Tutor tutor)
{
	cout<<"In function fn()"<<endl;
	//tutor.disT();
}

void main()
{
	Student randy("Randy");
	Tutor tutor(randy);
	//tutor.disT();
	cout<<"Calling fn()"<<endl;
	fn(tutor);
	cout<<"Returned from fn()"<<endl;
}

//**************************
//**      ch14_3.cpp      **
//**************************
#include <iostream.h>
#include <string.h>

class Person
{
public:
	Person(char* pN)
	{
		cout<<"Constructing "<<pN<<endl;
		pName=new char[strlen(pN)+1];
		if(pName!=0)
		{
			strcpy(pName,pN);
		}
	}
	~Person()
	{
		cout<<"Destructing "<<pName<<endl;
		pName[0]='\0';
		delete pName;
	}
protected:
	char* pName;
};

void main()
{
	Person p1("Randy");
	Person p2=p1;
}


⌨️ 快捷键说明

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