Click here to Skip to main content
15,881,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ..

C++
#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 .. updated version HERE:

C++
#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;
}
Posted
Updated 16-Jan-14 18:44pm
v3
Comments
Usman Hunjra 15-Jan-14 15:57pm    
Also I'm unable to determine the return type of the function,
Also there is no need to write the BIG THREE..
As I do not manage a resource here ..
Please have an eye .. :?

1 solution

This association is already two-way. Just add:
C++
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


Of course, you would need to add checks for null and other extra arithmetic like that. In C++ and other languages without garbage collection, association relationship is distinctly different from composition, which is also responsible for life time of the composed objects. So, working with associations, you can assume that some other object is holding your objects and will eventually deallocate them. For example, you could have a container object holding a list of all people.

And you mistakenly call the family relationship a "tree". Despite the common notion of the "family tree" (what a misnomer! apparently, it has some paternalistic roots, where female members were not taken seriously :-)), it is not a tree at all. In a tree, by definition, each person would have only one parent, and may have some children — a tree is a graph without cycles. Family, or native relationship between people and other animals is a more complex graph then the tree.

—SA
 
Share this answer
 
v2
Comments
Ron Beyer 15-Jan-14 19:18pm    
+5!
Sergey Alexandrovich Kryukov 15-Jan-14 23:07pm    
Thank you, Ron.
—SA
Peter Leow 15-Jan-14 21:29pm    
Excellent explanation, +5!
Sergey Alexandrovich Kryukov 15-Jan-14 23:07pm    
Thank you, Peter.
—SA
Usman Hunjra 16-Jan-14 0:58am    
Thank u so much sir for Great Explanation. :)
i want to use getName() function in the spouse() function to get the name of the associated person .. ???!
like thi: {if(married == NULL) cout << "Not Married.." << endl;
else
cout << "Married With -> " << getName() << endl; // ??!
}
But unable to do that, (errors)
how can i achieve that ..
Thanks For Your Consideration ... !!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900