Solved

MANY TO MANY ASSOCIATION

Posted on 2003-11-14
8
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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
 

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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

623 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