考试网 >> IT认证 >> 水平 >> 程序员 >> 谈谈c++的初始化工作(下)

谈谈c++的初始化工作(下)

发布时间:2006-06-28 02:36     点击:
分页:上一页  1 2 3 4 [5] 6  下一页

*/

下面的例子,是拷贝函数的问题,在上面的基础上,我改动了一下程序的结构,

定义了一个名空间。

具体的问题分析我留给下次,您就有机会细细的看了。您是否一切都清楚了?

您可以解决这个问题吗?

//human.h

#pragma once

#using

namespace Humanbeing

{

#define NULL 0

class Name

{

char *name;

public:

Name(char *_name=NULL):name(_name) {}

~Name() { }

char *getName(){ return name;}

};

class Human

{

Name *name; //

int ID; //唯一的标志

public:

Human(int id=0,char *_name=NULL):ID(id)

{

name=new Name(_name);//申请资源

}

~Human(void)

{

delete name;//释放资源

}

//拷贝赋值操作

Human &operator=(Human &human){

if(this!=&human){

ID=human.getID();

name=new Name(human.getName()->getName());

return *this;

}

}

int getID()const { return ID;}

Name *getName() { return name;}

};

//名空间里的函数

bool IsSameMan(Human one,Human another)

{

if(one.getID()==another.getID())

return true;

else return false;

}

}

//测试文件

#include

#include "human.h"

void main()

{

using namespace Humanbeing;

try{

Human lily(11100120,"lily");

Human lucy=lily;

if(IsSameMan(lucy,lily))

{

std::cout<<" They are the same one. ";

}else

std::cout<<" No,they're not the same one. ";

分页:上一页  1 2 3 4 [5] 6  下一页
版权申明:未经书面授权请勿转载本站信息!!作品版权归所属媒体与作者所有!!
发表评论: 匿名发表 用户名: 查看评论
您将承担一切因您的行为、言论而直接或间接导致的民事或刑事法律责任
留言板管理人员有权保留或删除其管辖留言中的任意内容
本站提醒:不要进行人身攻击。谢谢配合。
在本站搜索相关信息
2003-2005 Ksw123.com All Rights Reserved. - TOP
Copyright © 2006 Ksw123.com. All rights reserved.中国考题网 版权所有