Solved

MANY TO MANY ASSOCIATION

Posted on 2003-11-14
8
210 Views
Last Modified: 2012-05-04
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
Comment
Question by:zitaescobar
8 Comments
 
LVL 5

Expert Comment

by:g0rath
ID: 9747666
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
 

Expert Comment

by:fester4
ID: 9748330
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
 
LVL 17

Expert Comment

by:rstaveley
ID: 9748480
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
 

Author Comment

by:zitaescobar
ID: 9750385
# 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:zitaescobar
ID: 9750414
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
 
LVL 17

Expert Comment

by:rstaveley
ID: 9751916
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
 
LVL 9

Expert Comment

by:tinchos
ID: 10249202
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
 

Accepted Solution

by:
modulo earned 0 total points
ID: 10308087
PAQed - no points refunded (of 125)

modulo
Community Support Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now