Help with my abstract class

From my code below I'm trying to create a Course grading system. So, it will store the type of assignment, its weightage and the achieved grade. I'm trying to get it to print the raw score (which is the grade entered for example: "B-" or 85.50; depending on whether it is an exam or quiz).

I have read about "casting" which should resolve the issue of using void* as a pointer but I am still unclear as to how I can implement it. I tried static_cast<class Grading*>(dt.work.at(0))(commented out in code) but it still returned the address of the value, instead of the value itself. Any help will be appreciated. Thanks in advance!


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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <iomanip>


using namespace std;


/* -------------------------------------------------------- */
/* ---------------------- Grading Class ------------------- */
/* -------------------------------------------------------- */
class Grading
{
public:
    string name;
    int percentage;
    virtual ~Grading() {}
    virtual void* get_raw_score(){return 0;}
    void* get_adj_score(){return 0;}
};

/* -------------------------------------------------------- */
/* ---------------------- Assignment Class ---------------- */
/* -------------------------------------------------------- */
class Assignment : public Grading
{
protected:
    int score;

    virtual void* get_raw_score() {return &score;}
};

/* -------------------------------------------------------- */
/* ---------------------- Exam Class ---------------------- */
/* -------------------------------------------------------- */
class Exam : public Grading
{
public:
	int score;

    Exam(string n, int g, int s) {
        name = n;
        percentage = g;
        score = s;
    }
    virtual void* get_raw_score() {return &score;} // is implicitly virtual
};



/* -------------------------------------------------------- */
/* ------------------- Project Class ---------------------- */
/* -------------------------------------------------------- */
class Project : public Assignment
{
public:

    string letter_grade;

    Project(string n, int g, string l_g) {
        name = n;
        percentage = g;
        letter_grade = l_g;
    }
    virtual void* get_raw_score(){return &letter_grade;}
};

/* -------------------------------------------------------- */
/* ---------------------- Quiz Class ---------------------- */
/* -------------------------------------------------------- */

class Quiz : public Grading
{
public:

    string letter_grade;

    Quiz(string n, int g, string l_g)
    {
        name = n;
        percentage = g;
        letter_grade = l_g;
    }
    virtual void* get_raw_score(){return &letter_grade;}
};

/* -------------------------------------------------------- */
/* ---------------------- CourseWork class ---------------- */
/* -------------------------------------------------------- */

class CourseWork {

public:
    CourseWork() {}

    void push_back( Grading* a )
    {
        work.push_back(a);
    }

    // print the data and sort by name
    void sort_name() {}

    void sort_score() {}

    friend ostream& operator<<(ostream& os, const CourseWork dt) {
            os << dt.work.at(0)->name << dt.work.at(0)->percentage << dt.work.at(0)->get_raw_score() << endl;

        return os;
    }


private:
    vector<Grading*> work;
};

/* -------------------------------------------------------- */
/* ---------------------- MAIN ---------------------------- */
/* -------------------------------------------------------- */

int main () {
    CourseWork c;

    c.push_back(new Quiz("Quiz", 5, "B-"));
        c.push_back(new Quiz("Quiz", 5, "C+"));
        c.push_back(new Quiz("Quiz", 5, "A"));
        c.push_back(new Exam("Midterm", 10, 50));
        c.push_back(new Exam("Final", 30, 85.5));
        c.push_back(new Project("Project", 5, "A-"));
        c.push_back(new Project("Project", 15, "B-"));
        c.push_back(new Project("Project", 15, "B-"));
        c.push_back(new Project("Demo", 10, "C"));

        cout << "** Showing populated data..." << endl;
        cout << c << endl << endl;;

        c.sort_name();
        cout << "** Showing sorted by name..." << endl;
        cout << c << endl << endl;

        c.sort_score();
        cout << "** Showing sorted by score..." << endl;
        cout << c << endl;

        return 0;

}



Last edited on
I think you should re-work your OOD patterns. I am seeing lots of encapsulation issues. Then once that is complete overloading << should be trivial.
Last edited on
Also, I see no need for you using void pointers at all. That is a C thing that should generally be avoided in c++
Hi,

Shouldn't need to use new either :+) The STL containers like std::vector implicitly put their data on the heap. I can see you may have used to create a pointer for the container, but you could either take the address with &, or look into using smart pointers.

Btw, don't create your own function with the same name as an STL one: push_back. Especially in combination with having using namespace std; that could be just asking for trouble. Avoid using namespace std; , just put std:: before every std thing, believe me - it's the easiest and best way in the end. All the experienced coders do it, so you may as well get ahead and start doing it too :+)
Topic archived. No new replies allowed.