关系关系(概念) [英] Association Relationship (concept)

查看:122
本文介绍了关系关系(概念)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Guys ..



我正在写一个例子来理解关联的概念..



这是非常简单的代码..



我在getName函数中遇到问题.. ??你能帮助我吗,我怎么能得到相关人员的名字,这只是一个课堂对话,我正在努力实施,这对我的项目有帮助..



另外我如何实现双向关联.. ??!

任何建议都将受到赞赏..



Hello Guys ..

I'm writing an example to understand the concept of association ..

This is very simple code ..

i'm having problem in getName function .. ?? can you please help me i.e how can i get the name of the associated person, well this is just a class conversation, that i'm trying to implement, that would help in my project ..

Also how can i implement 2 way association .. ??!
Any suggestions would be appreciated ..

#include <iostream>
using namespace::std;

class Person
{
public:
	Person()
	{}
	Person(string GF)
	{married = NULL;}
 //   getName(){ return ??; } ??!

	void marry(Person *p) {married = p;}
	void divorse() {married = NULL;}
	void spouse()
	{if(married == NULL) cout << "Not Married.." << endl;
	else
		cout << "Married With -> " << "what ever ? " << endl; // ??!
	}

private:
	Person *married;
};

int main()
{
	Person *p1, *p2;
	p1 = new Person("John");
	p2 = new Person("Helan");
	p1->spouse();

	p1->marry(p2);
	p1->spouse();
	p2->spouse();


	return 0;
}





ok ..更新版本HERE:





ok .. updated version HERE:

#include <iostream>
using namespace::std;

class Person
{
public:
	Person()
	{ }
	Person(string Girl)
	{
		name = Girl;
		married = NULL;
	}

	string getName();
	void marry(Person *p);
	void divorce();
	void spouse();
private:
	Person *married;
	string name;
};

string Person::getName()
{
	return name;
}

void Person::marry(Person *p)
{
	if (married == NULL)
	{
		married  = p;
	//	p->married = this; // for 2way association..!!
	}
	
}
void Person::spouse()
{
	if (married == NULL)
		cout << "Not Married " << endl;
	else
	cout << "Married To: " << married->getName() << endl; // error here
}
void Person::divorce()
{
	if (married == NULL)
	{
		return;
	}
	else
	{
		married->married = NULL;
		married = NULL;
	}
}


int main()
{
	Person *p1, *p2;
	p1 = new Person("John");
	p2 = new Person("Helan");
	p1->spouse();

	p1->marry(p2);
	p1->spouse();
	p2->spouse();


	return 0;
}

推荐答案

此关联已经是双向的。只需添加:

This association is already two-way. Just add:
void marry(Person *p) {
    married = p;
    p->married = this;
}
void divorce() // spelling corrected :-)
{
   if (married == NULL) return;
   married->married = NULL;
   married = NULL;
}

// and of course, if you add the field "name" (could be private)

Person mySpouseName = spouse->name; // will be accessible because
                                       // both objects are of the same class





当然,你需要添加null的检查和其他类似的额外算法。在没有垃圾收集的C ++和其他语言中,关联关系与组合明显不同,组合也负责组合对象的生命周期。因此,使用关联,您可以假设某些其他对象持有您的对象并最终将其解除分配。例如,你可以有一个容器对象,其中包含所有人的列表。



你错误地将家庭关系称为树。尽管有家谱的常见概念(这是一个用词不当!显然,它有一些家长式的根源,女性成员并未被认真对待:-)),它根本不是一棵树。在树中,根据定义,每个人只有一个父母,并且可能有一些孩子 - 树是没有周期的图。家庭或人与其他动物之间的本土关系比树更复杂。



-SA


这篇关于关系关系(概念)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆