Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

MANY TO MANY ASSOCIATION

hello experts ,

I dont know how to implement two way association in c++ for MANY to MANY association for:

        TEACHERS<----------->STUDENTS

CLASS TEACHER{
PUBLIC:
VOID TEACHER(){
COUT<<"ENTER NAME";
CIN>>NAME;
}
CHAR *NAME;

}

CLASS STUDENT{
VOID SUDENT();
}


========================================


COULD YOU PLZ HELP ME
BECAUSE I HAVE MY FINAL TERM IN NEXT FEW DAYS
THANKS

0
zitaescobar
Asked:
zitaescobar
1 Solution
 
g0rathCommented:
We do not do homework, but we can assit if you have made an attempt. More then the code that you've written so far....plan what you want, write some code and then we can help direct you down the right path short of actually writing the code
0
 
fester4Commented:
If you are having trouble visualizing a simple way to do this I can help you out, but this will be psuedo code that is all untested.

For a many to many relationship between Students and Teacher.
What you are saying is the each student can be affiliated with many teachers.
Each teacher can be affiliated with many students.
For this you will need to create a data structure. The easiest to understand and implement, though not the most efficient for this would be using a linked list to implement the relationships.
Build the two classes like this: (I like structs but you can make it all in the class

struct Relation
{
Student *Student;
Student *NextStudent;
Teacher *Teacher;
Teacher *NextTeacher;
}

struct Student
{
Relation *Teacher;
}

struct Teacher
{
Relation *Student;
}

This is by far the fastest way, but what you do is fill in the structs as neccessary and what you will build for each is something as follows:
Student A:
Teacher->Teacher->Teacher->Teacher
(and you would be able to traverse this list)

Teacher B
Student->Student->Student->Student
(traverseable as well)

if you need more help I can give you some better stuff to get started.
0
 
rstaveleyCommented:
Use a "real world" analogy to think it through. Consider a filing cabinet in which we had dossiers for students and dossiers for teachers.

First off we start with a bad idea. We say that each student's dossier should contain a dossier for each teacher he is instructed by. We also say that each teacher's dossier should contain a dossier for each student she instructs. It is a bad idea because you can see that we have lots of similar copies of records with dossiers inside dossiers inside dossiers... we realise that we are dealing with an infinite sized storage problem.

Now we decide to use two separate draws in the filing cabinet: one for student dossiers and one for teacher dossiers. Each dossier is given an identifier - perhaps the name or a unique identification number. Each student's dossier contains a list of teachers he is instructed by. Each teacher's folder contains a list of students she instructs. The lists are references to dossiers.

A dossier is analogous to an instance of a class and a list is analogous to... a list!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
zitaescobarAuthor Commented:
# include<iostream.h>
#include<string.h>
class teacher{
public:
teacher(char *n)
{
      strcpy(name,n);
}
void print()
{
cout<<"name is "<<name<<endl;
}
private:
char name[10];
};

class student{
public:
student(char *n)
{
      strcpy(name,n);
}
void print()
{
cout<<"name is "<<name<<endl;
}
void assign(teacher *tptr)
{
      stdteach=tptr;
}

public:
char name[10];
teacher *stdteach;
};
0
 
zitaescobarAuthor Commented:
hello every one ,
here is the code i have tried for one to one association .
now plz help me convert it in to many to many association .I will appreciate it.
thanks


========================================================
# include<iostream.h>
#include<string.h>
class teacher{
public:
teacher(char *n)
{
      strcpy(name,n);
}
void print()
{
cout<<"name is "<<name<<endl;
}
private:
char name[10];
};

class student{
public:
student(char *n)
{
      strcpy(name,n);
}
void print()
{
cout<<"name is "<<name<<endl;
}
void assign(teacher *tptr)
{
      stdteach=tptr;
}

public:
char name[10];
teacher *stdteach;
};
0
 
rstaveleyCommented:
The teacher needs to have a list of students. The student has a pointer to only one teacher. It would be better if teachers and students both had lists.

Rather than using pointers, why not give teachers and students a numeric identifier. You will then need to think of how you can add a list of numeric identifiers.
0
 
tinchosCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

PAQ - no points refunded

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0
 
moduloCommented:
PAQed - no points refunded (of 125)

modulo
Community Support Moderator
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now