• 0

How can I connect two classes together?


Question

i have two classes

student

and courses

in my code i add any number of student with numbers to everyone

and add any number of courses with numbers to every course.

i do it by arrays >>>>> student stu[] ;

after add that ... i want to asign number of student to number of course

how can i do that ??? linking two classes ???

in C++ of course

visual studio 2003

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0
i have two classes

student

and courses

in my code i add any number of student with numbers to everyone

and add any number of courses with numbers to every course.

i do it by arrays >>>>> student stu[] ;

after add that ... i want to asign number of student to number of course

how can i do that ??? linking two classes ???

in C++ of course

visual studio 2003

any one please :)

Link to comment
Share on other sites

  • 0

I'm not quite sure I understand the question. Can you try posting your current class structure, and what you're trying to achieve?

Link to comment
Share on other sites

  • 0
I'm not quite sure I understand the question. Can you try posting your current class structure, and what you're trying to achieve?

ok my classes has

an student_id and student _name

and set and get to Id and name

and same in courses

after i add names and ids for student and courses i want to link the id of student with at least one id from courses

Link to comment
Share on other sites

  • 0

You could store a pointer to your courses class in the student class which would then allow you to 'message' the course class and access its functions. I can give a more fuller answer but I'm a bit busy right now. Post back if you want more info.

Posting your code will help too!

Link to comment
Share on other sites

  • 0
You could store a pointer to your courses class in the student class which would then allow you to 'message' the course class and access its functions. I can give a more fuller answer but I'm a bit busy right now. Post back if you want more info.

Posting your code will help too!

fine please expalin

and this is my classes

class Course

{

private:

//int C_Id;

//std::string C_name;

public:

int C_Id;

std::string C_name;

Course(void);

~Course(void);

void set_C_Id(int x);

int get_C_Id();

void set_C_name(std::string n);

std::string get_C_name();

};

class Student

{

private:

//int S_Id;

std::string S_name;

public:

Course C_A_S[50];

Prof P_A_S[50];

Student(void);

~Student(void);

void set_S_Id(int x1);

int get_S_Id();

void set_S_name(std::string n);

std::string get_S_name();

void get_S_by_Id(int x2);

int S_Id;

std::string me(int w);

};

watting you ... :)

Link to comment
Share on other sites

  • 0

OK your header files so far look like this:

class Course{
	 private:
		 //int C_Id;
		 //std::string C_name;

	 public:

		 int C_Id;
		 std::string C_name;
		 Course(void);
		 ~Course(void);
		 void set_C_Id(int x);
		 int get_C_Id();
		 void set_C_name(std::string n);
		 std::string get_C_name();
 };

 class Student{
	 private:
		 //int S_Id;
		 std::string S_name;

	 public:

		 Course C_A_S[50];
		 Prof P_A_S[50];

		 Student(void);
		 ~Student(void);
		 void set_S_Id(int x1);
		 int get_S_Id();
		 void set_S_name(std::string n);
		 std::string get_S_name();
		 void get_S_by_Id(int x2);
		 int S_Id;
		 std::string me(int w);
 };

What I was suggesting was that you have a pointer to the Course class within the Student class.

class Student{
	 private:
		 Course *courseptr=NULL;

	 public:

		 void setCourseClass(Course* ptr){courseptr = ptr;}
		 int setCourse(int course_id);
 };

 int setCourse(int course_id)
 {
	 courseptr->set_C_Id(course_id);
 }

 int getCourse()
 {
	 return courseptr->get_C_Id();
 }

You will also have to ensure you add error checking to these functions to make sure courseptr is not NULL.

So in your main you could do something like this:

int main(void)
{
	// Create a new Student
	Student student1(// etc);
	// Create a new instance of the Course class)
	Course	course1(// etc);

	// Link the two classes together so the Student class
	// now knows the location of the course class
	student1.setCourseClass(&course1);

	// Set a course
	student1.setCourse(2);

	// Display the course to ensure it has been set correctly
	cout << "Course ID:" << student1.getCourse() << endl;
}

I haven't compiled the code so it may not be 100% working, but I think its roughly right.

Hope this helps!

Link to comment
Share on other sites

  • 0

I recommend a third class, say "registrations", that holds info about which students are registered for which courses.

Each instance will contain a single ref to a student and a single ref to a course. Each time a sudent signs up for a new course, that's a new member of the class.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.