Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MANY TO MANY ASSOCIATION

Posted on 2003-11-14
8
Medium Priority
?
216 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

688 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