I am new to C++ programming and I have working with program for days now. This program using inheritance techniques. The asks the user to "Enter 1 for undergrad or 2 for gard student and than displays input information according. In advance, thank you for any help that someone can provide.
You have a quite a few problems here. For example:
Compare line 18 (member function declaration) void setName(string);
with line 46 (member function definition) void Student::setName(char name2)
The arguments to the function need to be the same - this is an easy mistake to make, but it's also easy to diagnose once you get used to reading the compiler error messages & looking at the code the message is complaining about.
The majority of your problems boil down to declaring things as char when you seem to actually want to be using a string . Remember that a char is a single character, whereas a string is a container for many characters.
My advice would be to look at every instance of char in you code and ask yourself, Do I mean this to be a single character, or do I want to store many characters?
Line 30,47: You can't use the C strcpy function to copy C++ std::string objects. Simply use the assignment operator.
name = StudentName;
Line 52: Really not recommended to return a pointer to a string. Either return a copy of the string, or a reference to it. getName() should be const. There is no reason for a getter to modify the object. That also means get rid of namePtr.
1 2 3
string Student::getName() const
{ return name;
}
line 163: The correct form of getline is:
getline(cin, StudentName);
Line 167: Why are you calling a getter here. Didn't you mean to call a setter?
Line 171: setCreditHrs is not a member of Student. a is type Student, even though you did a new UnderGradStudent.
You can do this instead:
1 2 3 4 5
UnderGradStudent * ug;
ug = new UnderGradStudent;
a = ug;
...
ug->setCreditHrs(CreditHrs);
Line 187: You want the setter here, not the getter.
I have been at this program for four hours now. And still can not get it to work, I think the reason why is because I have a limited understanding of inheritance.
Then post your current code and the list of errors you're getting.
The code I posted compiled fine for me.
From the code above, you're assigning new UnderGradStudent to a. That's legal, but the compiler does an automatic downcast to type Student since that is the type of a. Once that happens, members unique to UnderGradStudent are no longer visible since they are not part of Student. This why I created a pointer called ug of type UnderGradStudent *. By doing that, all members of UnderGradStudent are available. I then did a downcast of ug to Student * by assigning it to a. Of course, the same applies to GradStudent.
By downcasting UnderGradStudent or GradStudent to Student *, that allows your cout at line 204 (original snippet) to reference members of the common base class.
Line 164 E:\InheritStudent.cpp [Error] invalid new-expression of abstract class type UnderGradStudent'
Line 98 E:\InheritStudent.cpp [Note] because the following virtual functions are pure within 'UnderGradStudent':
Line 137 E:\InheritStudent.cpp [Note] virtual void UnderGradStudent::setQualityPts(int)
Line 190 E:\InheritStudent.cpp [Error] invalid new-expression of abstract class type 'GradStudent'
Line 68 E:\InheritStudent.cpp [Note] because the following virtual functions are pure within 'GradStudent':
Line 22 E:\InheritStudent.cpp [Note] virtual void Student::setQualityPts(int)
Line 204 E:\InheritStudent.cpp [Error] 'class Student' has no member named 'setStatus'
Line 164 E:\InheritStudent.cpp [Error] invalid new-expression of abstract class type UnderGradStudent'
Line 106: Why did you make this a pure virtual function? Pure virtual implies you have a class derived from UnderGradStudent and the function is implemented in the derived class. You have no class derived from UnderGradStudent. Remove the virtual and the =0.
Line 204 E:\InheritStudent.cpp [Error] 'class Student' has no member named 'setStatus'
Line 189: What I explained about UnderGradStudent also applies to GradStudent.
Line 106: Why did you make this a pure virtual function? Pure virtual implies you have a class derived from UnderGradStudent and the function is implemented in the derived class. You have no class derived from UnderGradStudent. Remove the virtual and the =0.
Line 21: This line does not belong in Student. Delete it.
AbstractionAnon wrote:
Line 189: What I explained about UnderGradStudent also applies to GradStudent.
I'm repeating what I told you before. I don't know how I can make it any more clear.
Line 203: Your capitalization is incorrect. Should be SetStatus.