Inheritance

Apr 29, 2015 at 1:32pm
I am just lost on trying to fix this code. I changed everything i thought possible but i still get an error saying UndergradStudent has not been declared and expected identifer before public and expeceted unqulified id before public.

#ifndef Student_H
#define Student_H
#ifndef UndergradStudent_H
#define UndergradStudent_H

#include <iostream>
using namespace std;

class Student
{
protected:
int ID;
float mathScore;
float chemScore;
public:
Student (int = 0, float = 0.0f, float = 0.0f);
void getGPA();
};

Student::Student (int id, float ms, float cs)
{
ID = id;
mathScore = ms;
chemScore = cs;
}

void Student::getGPA ()
{
float gpa = (mathScore + chemScore)/2.0;
cout << "ID = " << ID << " PGA = " << gpa << endl;
#endif
}
class UndergradStudent::public Student
{
protected:
float engScore;
float bioScore;
public:
Student(int id = 0, float = 0.0f, float = 0.0f) : Student(id), mathScore(ms), chemScore(cs) {}
void getGPA();
};

void UndergradStudent::getGPA ()
{
float gpa = (mathScore + chemScore + engScore + bioScore)/4.0;
cout << "ID = " << ID << " PGA = " << gpa << endl;
}
#endif
int main()
{
Student student = Student(777,70.5,99.8);
UndergradStudent undergradStudent = UndergradStudent(999,80,55.6,70,44.9);

student.getGPA();
undergradStudent.getGPA();

return 0;
}
Apr 29, 2015 at 1:33pm
--- Given a parent class below (Student), derive a child class (UndergradStudent).
The UndergradStudent should have two new members: float engScore, float bioScore.
The UndergradStudent should overwrite getGPA() to take into consideration of two new members.
--- In the main function, create one object of Student and another object of UndergradStudent and
let the two objects to call their own getGPA().

that is the question
Apr 29, 2015 at 2:09pm
class UndergradStudent::public Student{

the colon should be one not two.

Aceix.
Apr 29, 2015 at 2:18pm
I changed that and I get all of these errors

39:9: error: expected unqualified-id before 'int'
39:9: error: expected ')' before 'int'
In function 'int main()':
52:74: error: no matching function for call to 'UndergradStudent::UndergradStudent(int, int, double, int, double)'
52:74: note: candidates are:
33:8: note: UndergradStudent::UndergradStudent()
33:8: note: candidate expects 0 arguments, 5 provided
33:8: note: constexpr UndergradStudent::UndergradStudent(const UndergradStudent&)
33:8: note: candidate expects 1 argument, 5 provided
33:8: note: constexpr UndergradStudent::UndergradStudent(UndergradStudent&&)
33:8: note: candidate expects 1 argument, 5 provided
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#ifndef Student_H
 #define Student_H
 #ifndef UndergradStudent_H
 #define UndergradStudent_H 

#include <iostream>
 using namespace std;

 class Student
 {
 protected:
 int ID;
 float mathScore;
 float chemScore;
 public:
 Student (int = 0, float = 0.0f, float = 0.0f);
 void getGPA();
 };

 Student::Student (int id, float ms, float cs)
 {
 ID = id;
 mathScore = ms;
 chemScore = cs;
 }

 void Student::getGPA ()
 {
 float gpa = (mathScore + chemScore)/2.0;
 cout << "ID = " << ID << " PGA = " << gpa << endl;
 #endif 
}
 class UndergradStudent::public Student
 {
 protected:
 float engScore;
 float bioScore;
 public: 
Student(int id = 0, float = 0.0f, float = 0.0f) : Student(id), mathScore(ms), chemScore(cs) {}
 void getGPA();
 };

 void UndergradStudent::getGPA ()
 {
 float gpa = (mathScore + chemScore + engScore + bioScore)/4.0;
 cout << "ID = " << ID << " PGA = " << gpa << endl; 
}
 #endif
 int main()
 {
 Student student = Student(777,70.5,99.8);
 UndergradStudent undergradStudent = UndergradStudent(999,80,55.6,70,44.9);

 student.getGPA();
 undergradStudent.getGPA();

 return 0;
 } 
Apr 29, 2015 at 2:36pm
i dont get what youre trying to do on line 39.
Apr 29, 2015 at 2:46pm
closed account (z05DSL3A)
try something more like:
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
using namespace std;

/*****************************************************************************/
class Student
{
protected:
    int ID;
    float mathScore;
    float chemScore;
public:
    Student(int = 0, float = 0.0f, float = 0.0f);
    void getGPA();
};

Student::Student(int id, float ms, float cs)
{
    ID = id;
    mathScore = ms;
    chemScore = cs;
}

void Student::getGPA()
{
    float gpa = (mathScore + chemScore) / 2.0;
    cout << "ID = " << ID << " PGA = " << gpa << endl;

}
/*****************************************************************************/
class UndergradStudent : public Student
{
protected:
    float engScore;
    float bioScore;
public:
    UndergradStudent(int id = 0, float ms = 0.0f, float cs = 0.0f, float es = 0.0f, float bs = 0.0f) 
        : Student(id, ms, cs), engScore(es), bioScore(bs){}
    void getGPA();
};

void UndergradStudent::getGPA()
{
    float gpa = (mathScore + chemScore + engScore + bioScore) / 4.0;
    cout << "ID = " << ID << " PGA = " << gpa << endl;
}
/*****************************************************************************/
int main()
{
    Student student = Student(777, 70.5, 99.8);
    UndergradStudent undergradStudent = UndergradStudent(999, 80, 55.6, 70, 44.9);

    student.getGPA();
    undergradStudent.getGPA();

    return 0;
}
Apr 29, 2015 at 7:06pm
thank you so much for helping!
Topic archived. No new replies allowed.