help me! (coding)

PBarnum

New Adventurer
MSS Developer
MSC Developer
RiP
Joined
Jun 14, 2006
Messages
3,031
Reaction score
4
I have this...

Code:
class Student
{
public:
  (blah blah blah)
private:
  string studentName;
  int strudenAge;
  string studentId;
};

class GradStudent : public Student
{
public:
  (blah blah blah)
private:
  string academicMajor;
  float grantAmt;
  bool teachAssist;
};

(where the errors are)

void GradStudent::SetGradInfo(string major, float grant, bool ta, string nm, int ag, string stuId)
{
  academicMajor = major;
  grantAmt = grant;
  teachAssist = ta;

(error: illegal access to protected/private member)
  studentName = nm;

(error: illegal access to protected/private member)
  studentAge = ag;

(error: illegal access to protected/private member)
  studentId = stuId;
}

incase you are wondering, this is just a little project for my CS1C class.
 

The Man In Black

Administrator
Staff member
Administrator
Moderator
RiP
Joined
Jul 9, 2006
Messages
6,904
Reaction score
71
It's because they're private..

Public - Can be accessed by all.
Private - Can't be acessed by anything but the class itself
Protected - In between public and private. Protected members can only be accessed by the class or a "child" class.

You want those ones to be protected.
 

PBarnum

New Adventurer
MSS Developer
MSC Developer
RiP
Joined
Jun 14, 2006
Messages
3,031
Reaction score
4
so how do I get it to do what I want it to do (if you can tell from the blip)
 

The Man In Black

Administrator
Staff member
Administrator
Moderator
RiP
Joined
Jul 9, 2006
Messages
6,904
Reaction score
71
I haven't really looked at protected members in C++, but I'm gonna assume you declare it like public/private
protected:
string academicMajor;
float grantAmt;
bool teachAssist;
 

The Man In Black

Administrator
Staff member
Administrator
Moderator
RiP
Joined
Jul 9, 2006
Messages
6,904
Reaction score
71
Oh, and if that doesn't work, you can do what most programmers do. Set up the parent class with Getters and Setters (You should always have them anyway)

If you don't know what getters/setters are..

(Java)

public int getAge()
{
return age;
}

public void setAge(int newAge)
{
age = newAge;
}

All private data members that need to be affected outside of the class should have getters/setters. It makes it so that you can make sure things are properly formated before going in/out (Among other things. Go search teh intern0rts for encapsulation)
 

PBarnum

New Adventurer
MSS Developer
MSC Developer
RiP
Joined
Jun 14, 2006
Messages
3,031
Reaction score
4
I do have getters and setters but the problem is with this function. I emailed my teacher telling her the problem and she will get back to me probably tommorrow which means Ill post what went wrong here
 

The Man In Black

Administrator
Staff member
Administrator
Moderator
RiP
Joined
Jul 9, 2006
Messages
6,904
Reaction score
71
If you have them.. why aren't you using them..
 

PBarnum

New Adventurer
MSS Developer
MSC Developer
RiP
Joined
Jun 14, 2006
Messages
3,031
Reaction score
4
my teacher said:
To access the private fields in Student from GradStudent you must use the "setters" and
"getters" in the Student class. Inheritance does not imply accessibility.

We just learned classes and inheritance last tuesday so this is all new to me. Im going to do that and hopefully get a successful compile
 

The Valorous

New Adventurer
Joined
Dec 29, 2005
Messages
270
Reaction score
0
Age
37
Location
Pacific Northwest
Oooh... debugging. My prof. pretty much brainwashed us into thinking that it's not fulfilling to get a flawless program, and that all the fun's in debugging.

MiB got it right with the private thing, but past that I can't really be of much more help. Maybe you missed a semi-colon or something... :p (doubt it)
 

PBarnum

New Adventurer
MSS Developer
MSC Developer
RiP
Joined
Jun 14, 2006
Messages
3,031
Reaction score
4
well i got my getters in there but now when I comepile, I get 1 error. "Not an lvalue"

... oook

I had that problem before but forget how to fix it. it happens when at:

Code:
Student::GetName() = nm;
Student::GetAge() = ag;  <--- underlines the semi-colon
Student::GetId() = stuID;
 

The Man In Black

Administrator
Staff member
Administrator
Moderator
RiP
Joined
Jul 9, 2006
Messages
6,904
Reaction score
71
Well there are two things that are potentially wrong with that code, and both burn my eyes.

1. If you're trying to set nm equal to the return of the function GetName(), you have it backwards. nm = Student::GetName();

2. If you're actually trying to set up the function like that.. I may have to stab you..

(blah) GetName()
{
return name
}
 
Top