Class inheritance from another class.

Pages: 123
How to implement this UML diagram is confusing me quite a bit, and I don't think I did it entirely correctly. However system, I had to make some edits since what you're doing in some parts isn't what my professor wants. I'm pretty sure mine is off a bit too...but the way he set this up is confusing me.

Got three errors.

https://s7.postimg.org/v0i3bjk5n/Screenshot_38.png

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
// Ashton Dreiling
//Essay exercise program
#include <iostream>
#include "Essay class.h"
#include <string>
#include <stdlib.h>

const int NUM_FOR_CAL1=0;
using namespace std;

void userInput(double &grammar, double &spelling, double &CL, double &content);

int main ()
{
   // Some variables
    double grmmr;
    double splling;
    double correctlength;
    double con;

    userInput(grmmr, splling, correctlength, con);

    Essay essayObject(grmmr, splling, correctlength, con);
    // Displaying the scores that were inputed
    cout << "The recorded scores are:" << endl;
    cout << "Grammar : " << essayObject.getGrammar()  << endl;
    cout << "Spelling : " << essayObject.getSpelling() << endl;
    cout << "Correct Length : " << essayObject.getCorrectLength() << endl;
    cout << "Content : " << essayObject.getContent() << endl;

    Essay gradedObject();
    cout << "The grade for this essay is : " << gradedObject.getGrade() << endl;

    system("Pause");
    return 0;
}//end main

void userInput(double &grmmr, double &splling, double &correctlenght, double &con)
{

    //input from user to enter points
    cout << "Enter the points for an essay." << endl;
    cout << "Grammar (must be 30 or less points)." << endl;
    cin >> grmmr;
    while (grmmr>30 || grmmr<NUM_FOR_CAL1)
    {
        cout << "Grammar must be 30 or less points." << endl;
        cin >>grmmr;
    }//end while loop
    cout << "Spelling (must be 20 or less points)." << endl;
    cin >> splling;
    while(splling>20 || splling<NUM_FOR_CAL1)
    {
        cout << "Spelling must be 20 or less points." << endl;
        cin>>splling;
    }//end while loop
    cout << "Correct length (must be 20 or less points)." << endl;
    cin>>correctlenght;
    while(correctlenght>20 || correctlenght<NUM_FOR_CAL1)
    {
        cout << "Correct length must be 20 or less points." << endl;
        cin >> correctlenght;
    }//end while loop
    cout << "Correct content (must be 30 or less points)." << endl;
    cin>>con;
    while(con>30 || con<NUM_FOR_CAL1)
    {
        cout << "Content must be 30 or less points." << endl;
        cin >> con;
    }//end while loop
}// end user input 


Header

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
//Ashton Dreiling
//Essay header
#ifndef ESSAY_CLASS_H_INCLUDED
#define ESSAY_CLASS_H_INCLUDED
#include <string>

using namespace std;

class Essay
{

//private fields to hold data
private:
    double grammar;
    double spelling;
    double correctLength;
    double content;

    double total; //  (***) A variable is needed
    string grade;

//public methods for outside code
public:
    Essay(double G, double S, double corlen, double C)
    {
        grammar = G;
        spelling = S;
        correctLength = corlen;
        content = C;
    }//end EssayClass constructor

    void setGrammar(double aGrammar)
    {
        grammar=aGrammar;
    }

    double getGrammar()
    {
        return grammar;
    }

    void setSpelling(int aSpelling)
    {
        spelling=aSpelling;
    }

    double getSpelling()
    {
        return spelling;
    }

    void setCorrectLength(double aCorrectLength)
    {
        correctLength=aCorrectLength;
    }

    double getCorrectLength()
    {
        return correctLength;
    }

    void setContent(double aConent)
    {
        content=aConent;
    }

    double getContent()
    {
        return content;
    }

    void displayPoints()
    {

    }
};

class Essay : public GradedActivity
{
private:
    double score;
    string myGrade;
public:
    void setScore()
    {
        score = grammar + spelling + correctLength + content;
    }
    double getScore()
    {
        return score;
    }
    void getGrade()
    {
        if(score >= 95)
            myGrade ="A+";
        else if(score >= 90)
            myGrade = "A";
        else if(score >= 75)
            myGrade = "B";
        else if(score >= 60)
            myGrade = "C";
        else if(score >= 45)
            myGrade = "D";
        else if(score < 45)
            myGrade = "F";
    }

    string getGrade()
    {
        return myGrade;

    }
};

#endif // ESSAY_CLASS_H_INCLUDED 
Last edited on
I hope this is what you want :
The header :
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
//Ashton Dreiling
//Essay header
#ifndef ESSAY_CLASS_H_INCLUDED
#define ESSAY_CLASS_H_INCLUDED
#include <string>

using namespace std;

class GradedActivity
{
private:
    double score;
    string myGrade;
public:
    double getScore()
    {
        return score;
    }

    void setScore(double score2)
    {
        score = score2;
    }

    void findGrade()
    {
        if(score >= 95)
            myGrade ="A+";
        else if(score >= 90)
            myGrade = "A";
        else if(score >= 75)
            myGrade = "B";
        else if(score >= 60)
            myGrade = "C";
        else if(score >= 45)
            myGrade = "D";
        else if(score < 45)
            myGrade = "F";
    }

    string getGrade()
    {
        return myGrade;
    }
};

class Essay : public GradedActivity
{

//private fields to hold data
private:
    double grammar;
    double spelling;
    double correctLength;
    double content;

    double total;
    string grade;

//public methods for outside code
public:
    Essay(double G, double S, double corlen, double C)
    {
        grammar = G;
        spelling = S;
        correctLength = corlen;
        content = C;
    }//end EssayClass constructor

    void setGrammar(double aGrammar)
    {
        grammar=aGrammar;
    }

    double getGrammar()
    {
        return grammar;
    }

	void setScore()
    {
		GradedActivity::setScore(grammar + spelling + correctLength + content);
    }

    void setSpelling(int aSpelling)
    {
        spelling=aSpelling;
    }

    double getSpelling()
    {
        return spelling;
    }

    void setCorrectLength(double aCorrectLength)
    {
        correctLength=aCorrectLength;
    }

    double getCorrectLength()
    {
        return correctLength;
    }

    void setContent(double aConent)
    {
        content=aConent;
    }

    double getContent()
    {
        return content;
    }

    void displayPoints()
    {

    }
};


#endif // ESSAY_CLASS_H_INCLUDED  
main.cpp
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
// Ashton Dreiling
//Essay exercise program
#include <iostream>
#include "Essay class.h"
#include <string>
#include <stdlib.h>

const int NUM_FOR_CAL1=0;
using namespace std;

void userInput(double &grammar, double &spelling, double &CL, double &content);

int main ()
{
   // Some variables
    double grmmr;
    double splling;
    double correctlength;
    double con;

    userInput(grmmr, splling, correctlength, con);

    Essay essayObject(grmmr, splling, correctlength, con);
    // Displaying the scores that were inputed
    cout << "The recorded scores are : " << endl;
    cout << "+ Grammar : " << essayObject.getGrammar()  << endl;
    cout << "+ Spelling : " << essayObject.getSpelling() << endl;
    cout << "+ Correct Length : " << essayObject.getCorrectLength() << endl;
    cout << "+ Content : " << essayObject.getContent() << endl;

	essayObject.setScore();
    essayObject.findGrade();
    cout << "The grade for this essay is : " << essayObject.getGrade() << endl;

    system("Pause");
    return 0;
}//end main

void userInput(double &grmmr, double &splling, double &correctlenght, double &con)
{

    //input from user to enter points
    cout << "Enter the points for an essay." << endl;
    cout << "Grammar (must be 30 or less points) : ";
    cin >> grmmr;
    while (grmmr>30 || grmmr<NUM_FOR_CAL1)
    {
        cout << "Grammar must be 30 or less points. Enter again : ";
        cin >>grmmr;
    }//end while loop
    cout << "Spelling (must be 20 or less points) : ";
    cin >> splling;
    while(splling>20 || splling<NUM_FOR_CAL1)
    {
        cout << "Spelling must be 20 or less points. Enter again : ";
        cin>>splling;
    }//end while loop
    cout << "Correct length (must be 20 or less points) : ";
    cin>>correctlenght;
    while(correctlenght>20 || correctlenght<NUM_FOR_CAL1)
    {
        cout << "Correct length must be 20 or less points. Enter again : ";
        cin >> correctlenght;
    }//end while loop
    cout << "Correct content (must be 30 or less points) : ";
    cin>>con;
    while(con>30 || con<NUM_FOR_CAL1)
    {
        cout << "Content must be 30 or less points. Enter again : ";
        cin >> con;
    }//end while loop
}// end user input  
closed account (z05DSL3A)
FBHSIE, have you been told to implement the UML as is or just use it as a guide and add what you think you need?
We've been told to follow the UML, but I'm sure as long as we follow most of it, he won't mind a few changes.
Last edited on
closed account (z05DSL3A)
If you are not meant to be changing it you need to take a closer look at your code...
1
2
3
4
5
6
7
8
9
10
11
class GradedActivity
{
public:
    void setScore(double s);
    double getScore();
    std::string getGrade();
    
private:
    std::string score;
    
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Essay :GradedActivity
{
public:
    Essay();
    
    void setGrammer(double aGrammer);
    double getGrammer();
    double setSpelling(int aSpelling);
    double getSpelling();
    void setCorrectLength(double aCorrectLength);
    double getCorrectLength();
    void setContent(double aContent);
    double getContent();
    void setScore();
    void displayPoints();
    
    
private:
    double grammer;
    double spelling;
    double correctLength;
    double content;
};

NB: I don't know what - correctLength:double : int is meant to be in the UML, you may need to check that.
That's a mistake. We'll use double.

Is this UML makes sense to you? I'm just curious.
closed account (z05DSL3A)
Apart from the two type definitions on correctLength it makes sense. Storing the score in GradedActivity as a string seems odd but that could be a test or to force you to write converters in the setter and getter.

setSpelling may also be wrong in the UML but that is more a semantic issue instead of syntactic.
Last edited on
I'm pretty sure GradedActivity was meant to be a double. I'm not sure about the spelling. What else needs to be done?
closed account (z05DSL3A)
Write the bodies for all the functions, I haven't looked to closely at the code you have posted but it looks like with a bit of refactoring you have all you need.

I would recommend that you clearly comment assumptions that you make about how you interpret the assignment. If you do interpret it wrong at least it shows you are thinking about it.
I assume firstly in main I enter the scores of an essay, make sure the input is valid according to the homework instructions, and then make the Essay object which instantly calls the constructor which then passes the values inputted in the userInput into the variables in the set methods in the essay and gradedActivity class. From there, I expected the the score to be calculated in the setScore method in gradedActivity and for the gradedActivity to set the grade according to the score variable which used the input from main to get a value.

From there, I expected the classes to be done with their job (although I still have no idea what the displayPrint is for) but I'm just trying to form pieces of the puzzle.

Then, in main, as you can see, I wanted to show the values using my return methods in the class. That's it.

This is my code

Errors

http://prntscr.com/c2rg6i
Main

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
// Ashton Dreiling
//Essay exercise program
#include <iostream>
#include "Essay class.h"
#include <string>
#include <stdlib.h>

const int NUM_FOR_CAL1=0;
using namespace std;

void userInput(double &grammar, double &spelling, double &CL, double &content);

int main ()
{
   // Some variables
    double grmmr;
    double splling;
    double correctlength;
    double con;

    userInput(grmmr, splling, correctlength, con);

    Essay essayObject(grmmr, splling, correctlength, con);
    // Displaying the scores that were inputed
    cout << "The recorded scores are:" << endl;
    cout << "Grammar : " << essayObject.getGrammar()  << endl;
    cout << "Spelling : " << essayObject.getSpelling() << endl;
    cout << "Correct Length : " << essayObject.getCorrectLength() << endl;
    cout << "Content : " << essayObject.getContent() << endl;

    Essay gradedObject();
    cout << "The grade for this essay is : " << gradedObject.getGrade() << endl;

    system("Pause");
    return 0;
}//end main

void userInput(double &grmmr, double &splling, double &correctlenght, double &con)
{

    //input from user to enter points
    cout << "Enter the points for an essay." << endl;
    cout << "Grammar (must be 30 or less points)." << endl;
    cin >> grmmr;
    while (grmmr>30 || grmmr<NUM_FOR_CAL1)
    {
        cout << "Grammar must be 30 or less points." << endl;
        cin >>grmmr;
    }//end while loop
    cout << "Spelling (must be 20 or less points)." << endl;
    cin >> splling;
    while(splling>20 || splling<NUM_FOR_CAL1)
    {
        cout << "Spelling must be 20 or less points." << endl;
        cin>>splling;
    }//end while loop
    cout << "Correct length (must be 20 or less points)." << endl;
    cin>>correctlenght;
    while(correctlenght>20 || correctlenght<NUM_FOR_CAL1)
    {
        cout << "Correct length must be 20 or less points." << endl;
        cin >> correctlenght;
    }//end while loop
    cout << "Correct content (must be 30 or less points)." << endl;
    cin>>con;
    while(con>30 || con<NUM_FOR_CAL1)
    {
        cout << "Content must be 30 or less points." << endl;
        cin >> con;
    }//end while loop
}// end user input 


Header


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
//Ashton Dreiling
//Essay header
#ifndef ESSAY_CLASS_H_INCLUDED
#define ESSAY_CLASS_H_INCLUDED
#include <string>

using namespace std;

class Essay:GradedActivity
{

//private fields to hold data
private:
    double grammar;
    double spelling;
    double correctLength;
    double content;

//public methods for outside code
public:
    Essay(double G, double S, double corlen, double C)
    {
        grammar = G;
        spelling = S;
        correctLength = corlen;
        content = C;
    }//end EssayClass constructor

    void setGrammar(double aGrammar)
    {
        grammar=aGrammar;
    }

    double getGrammar()
    {
        return grammar;
    }

    void setSpelling(int aSpelling)
    {
        spelling=aSpelling;
    }

    double getSpelling()
    {
        return spelling;
    }

    void setCorrectLength(double aCorrectLength)
    {
        correctLength=aCorrectLength;
    }

    double getCorrectLength()
    {
        return correctLength;
    }

    void setContent(double aConent)
    {
        content=aConent;
    }

    double getContent()
    {
        return content;
    }

    void displayPoints()
    {

    }
};

class GradedActivity
{
private:
    double score;
    string myGrade;
public:
    void setScore()
    {
        score = grammar + spelling + correctLength + content;
    }
    double getScore()
    {
        return score;
    }
    void getGrade()
    {
        if(score >= 95)
            myGrade ="A+";
        else if(score >= 90)
            myGrade = "A";
        else if(score >= 75)
            myGrade = "B";
        else if(score >= 60)
            myGrade = "C";
        else if(score >= 45)
            myGrade = "D";
        else if(score < 45)
            myGrade = "F";
    }

    string getGrade()
    {
        return myGrade;

    }
};

#endif // ESSAY_CLASS_H_INCLUDED 

Last edited on
closed account (z05DSL3A)
In your header, look at line 9 and 75 and compare it to what I posted here: http://www.cplusplus.com/forum/beginner/195531/3/#msg940013

Spotted the differences. Corrected them. I updated my code with the new list or errors (scroll up).

At least it fixed most of my issues in main.
> At least it fixed most of my issues in main.
Is there any output sample? Do you need to strickly follow it?

Why didn't you choose my output? My program output looks neater, at least.
Oh. It didn't quite match the UML diagram. I think that's what we're all trying to figure out. Btw, get on slack.
> Btw, get on slack
Wait for me for three hours (until morning). It is still bedtime. I am not near my computer now.
closed account (48T7M4Gy)
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
#include <iostream>
#include <string>

using namespace std;

class GradedActivity
{
private:
    double score;
    string myGrade;
    
public:
    void setScore(double aScore)
    {
        score = aScore;
        
        if(score >= 95)
            myGrade ="A+";
        else if(score >= 90)
            myGrade = "A";
        else if(score >= 75)
            myGrade = "B";
        else if(score >= 60)
            myGrade = "C";
        else if(score >= 45)
            myGrade = "D";
        else if(score < 45)
            myGrade = "F";
    }
    
    double getScore()
    {
        return score;
    }
    
    string getGrade()
    {
        return myGrade;
    }
};

class Essay:public GradedActivity
{
private:
    double grammar;
    double spelling;
    int correctLength;
    double content;
    
public:
    Essay(double G, double S, double corlen, double C)
    {
        grammar = G;
        spelling = S;
        correctLength = corlen;
        content = C;
        
        GradedActivity::setScore(grammar + spelling + correctLength + content);
    }
    
    void setGrammar(double aGrammar)
    {
        grammar = aGrammar;
    }
    
    double getGrammar()
    {
        return grammar;
    }
    
    void setSpelling(int aSpelling)
    {
        spelling = aSpelling;
    }
    
    double getSpelling()
    {
        return spelling;
    }
    
    void setCorrectLength(int aCorrectLength)
    {
        correctLength = aCorrectLength;
    }
    
    int getCorrectLength()
    {
        return correctLength;
    }
    
    void setContent(double aContent)
    {
        content = aContent;
    }
    
    double getContent()
    {
        return content;
    }
    
    void setScore()
    {
        GradedActivity::setScore(grammar + spelling + correctLength + content);
    }
};

int main ()
{
    int no_of_students = 3;
    
    Essay essayObject[] = { {22,23,18,12}, {13,15,7,18}, {21,18,25,22} };
    
    for(int i = 0; i < no_of_students; i++)
    {
        // Displaying the scores that were inputed
        cout << "      The recorded scores are:" << endl;
        cout << "                     Grammar : " << essayObject[i].getGrammar()  << endl;
        cout << "                    Spelling : " << essayObject[i].getSpelling() << endl;
        cout << "              Correct Length : " << essayObject[i].getCorrectLength() << endl;
        cout << "                     Content : " << essayObject[i].getContent() << endl;
        
        cout << " The grade for this essay is : " << essayObject[i].getGrade() << endl << endl;
    }
    
    return 0;
}

      The recorded scores are:
                     Grammar : 22
                    Spelling : 23
              Correct Length : 18
                     Content : 12
 The grade for this essay is : B

      The recorded scores are:
                     Grammar : 13
                    Spelling : 15
              Correct Length : 7
                     Content : 18
 The grade for this essay is : D

      The recorded scores are:
                     Grammar : 21
                    Spelling : 18
              Correct Length : 25
                     Content : 22
 The grade for this essay is : B

Program ended with exit code: 0
Topic archived. No new replies allowed.
Pages: 123