Access to a private member of a class

This is part of my class:

1
2
3
4
5
6
7
8
9
10
class course
{
  char name[12];
  char term[8];
  int units;
  char grade;
  course*next;//I don't know how to access to this one
public:
  . . .
}


This is the part of .cpp where I need to access to the private:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int main()
{

...
char name[12];
char term[8];
char grade;
int units;
course*head=0;
course*c = new course(name, term,  grade, units);//make the temporary objects 
     
cout<<"Enter your course name. (e.g. COMSC-110 or COMSC-155H)"<<endl;
cin.getline(name, 12);

cout<<"Enter the Term. (e.g. FA2010) "<<endl;
cin.getline(term, 8);

cout<<"Enter the unit of that course. (0-4)"<<endl;
cin>>units;
cin.ignore(1000,10);

cout<<"Enter the grade of the course. (A, B, C, D, E, F)"<<endl;
cin>>grade;
cin.ignore(1000,10);

c->next=head;//i need to make a temporary object for course*next or just need any other method, but how to do that?

...
}
Last edited on
Just "friend" with that class / function

something like
 
friend class class_name;


http://www.cplusplus.com/doc/tutorial/inheritance/
If you don't want to have it private, then just don't make it private.
Friends are rarely a good idea.
Thank you for help!

@rmxhaha @Athar

I have to put it into private b/c of requirement of the assignment...

Any way to do that if it's in the private?
Methods have access to private data, so you can add getters and setters.
I am learning a basic OPP right now, the thing I only know about setters and getters are for functions....

I still didnt get how to use getter for course* next :(

could u give some hints?
did u know what you trying to do?
you are trying to use the single linked list

did u know how to single linked list work?
you can add new node for your linked list.

try search some reference or try to use your concept.
will be here to help.

you can create a course* head for your structure also

for example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void add(){
      course *tmp;
      course *curr;
      tmp = new course;
      tmp->next = NULL;
      if( head != NULL ){
             curr = head;
             while( curr->next ){
                        curr = curr->next;
             }
             curr->next = tmp;
      }
      else
             head = tmp;
}


this is the code that for your *next to do;
but then i still hope u get understand from other tutorial =) hope helps
Thank you very much! @Felicia123

So is making a getnext function the best way to do it?
And how do i call it ?

head->add();?

And where the head from?
Should it be this
Last edited on
the head should be declare inside your course

actually i hope u do for another structure for it since you got use linked list.
i will try to help as well

for example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct CourseRecord{
       char name[12];
       char term[8];
       int unit;
       char grade;
       CourseRecord *next;
};

class Course{
private:
       CourseRecord* head;
public:
       void add( //parameters here );
};


what should your add function have? just tell here should pass or try to do it .
Typically the idea behind this type of program is to make sure you understand how objects are created, how object pointers work, and a basic list. Your object is already created, you will need to make some minor modifications to it, but you're on the right track. In your constructors, ctors, make sure that you're setting next = NULL. This will make it seem like your object is the last one regardless of where it's at. Now, there is an easy way to change this without needing a "getter" but more on that later.

Since your class is fine for the moment, we'll move on to the next step, getting the list designed. As Felicia said, and as I believe you already implemented, you need a Course* head. This will always point at your very first Course object (the great thing about lists is that our variables don't need names). Also, you need another Course* which you declared as c. This will allow you to move through your array as needed.

Now, let's work on how this should work. head and c should both equal NULL, they don't have objects yet. You can now create a new object and return the pointer to c. You will have to work out how exactly you want to create objects, but it's typically best to put it in some style of loop asking the user for inputs. After c has a value, which it will when you c = new Course(...);, you should check to see if head has a value as well. If head == NULL, it's empty and should equal c. After all, if head is NULL, c contains your first object.

Once you understand the concept of the pointers, you need to move on to linking your list together. This is where the Course* next comes in. This should point to the next Course in the list. You have two options, you can make this public, which is easiest, but also possibly the worst thing you can do, or you can create a new member function called something like AddAfterMe that accepts a Course pointer. This will effectively allow you to link your list together.

Now, on to how to complete everything. c should, more or less, be your temp pointer that holds the new courses. You should create a new Course pointer and call it something like prev, p, whatever. This will allow you to know what the most recently added element to your list was since the list itself doesn't know what was prior. Some basic pseudo code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Course *head = NULL, *c = head, *prev = head; // Initialize all three as NULL

// Define local variables for use
courseName, courseTerm, courseUnit, courseGrade, again;

do {
   What is the course name?
   input courseName;
   What is the course term?
   input courseTerm;
   What is the course Unit?
   input courseUnit;
   What is the course Grade?
   input courseGrade;

   c = new Course with above values

   if (!head) // head is still NULL
      head = c;

   if (prev) // prev is pointing at something
      prev->AddAfterMe(c);

   prev = c; // Work is done, store the next item.

   Would you like to enter another course?
   input again;
} while (again == yes);


At this point, head points to your first item, and c and prev both point to your last. You can do a bunch of things with all of your pointers, but I strongly urge against changing head EVER unless it's NULL, otherwise you will lose elements in your list.

I hope all of this has helped you understand what you're doing it, why I wrote the code I did, and what all you can do with this.

Also, you may want to create a member function called NextCourse() it should return the pointer. You can test against this pointer returned and if it equals NULL, you've gone to the end of the list.

Hint: You'll probably be using code like this quite often in this program:
1
2
 if (p->NextCourse())
   p = p->NextCourse();


Good luck and sorry about the long winded reply.
Oooh! Get it!
Woo, thank you very much for taking time type all these information!! They are really helping me to understand this part! :D

You could also overload the NextCourse() function, one returns a Course* and accepts no parameters, the other should return void and accept a Course*. This may make it clearer, or it might confuse you. It really depends on your reasoning skills. You could also name them GetNextCourse and SetNextCourse if you think you might get confused.
Topic archived. No new replies allowed.