Class inheritance from another class.

Pages: 123
I'm trying to follow my professors UML diagram ------->http://prntscr.com/c2kqnh

I currently have this header written up trying to make GradedActivity inherit from Essay, but I am not putting it together correctly. I was hoping for some guidance?

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
//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;
//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 aGrammer)
    {
        grammar=aGrammar;
    }

    double getGrammar()
    {
        return grammar;
    }

    double 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 setScore(double s)
    {
        setScore=s;

    }

    void displayPoints()
    {

    }
};

class GradedActivity
{
private:
    double score;

public:

    double getScore()
    {
        return score;
    }

    string getGrade()
    {
        return grade;
    }


};

#endif // ESSAY_CLASS_H_INCLUDED 
Last edited on
GradedActivity.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// #include guard
#include <string>

class GradedActivity
{
    private: double score = 0 ;

    public:

        double getScore() const { return score ; }
        void setScore( double score ) { GradedActivity::score = score ; /* TO DO: validate score*/  }

        std::string getGrade() const
        {
            // TO DO: examine the score and return the appropriate grade
            // for instance: if( score > 80 ) return "A+" ; etc.
        }
};


Essay.h
1
2
3
4
5
6
7
// #include guard
#include "GradedActivity.h"

class Esaay : public GradedActivity
{
    // ... 
};
Hm, well we don't use const for this or std, so I'm a bit confused what you're doing. : )
Please use a different image hosting website. I can't see anything.
You are using std!

What do you think this is?

using namespace std;

And the const is just good practice. In your case you should ignore it.

Both of these details are unimportant for what you are trying to do.
We don't use std:: other than for the name space. If we try to use it for anything other than that, we lose points. We also don't use constants for this. We use constants of course, but not for this.

Here you go closed account.

https://s7.postimg.org/aaf39ydzv/7ebea326387e451e96cd30ab83685eec.png
Last edited on
It's your code and whatever you want to do is fine.

We don't use std:: other than for the name space.


Do you understand that this
1
2
3
4
5
6
7
8
#include <string>

int main() {
  std::string cat = "dad";
  return 0;
}



is the exact same thing as this?
1
2
3
4
5
6
7
#include <string>
using namespace std;

int main() {
  string cat = "dad";
  return 0;
}


saying "Using namespace std;" is like implicitly putting a "std::" between objects like "string" and "cout" and "cin".

We don't use std:: other than for the name space.

What you said is like we don't use cars except for driving. std IS the namespace.
Here you go :
GradedActivity.h
1
2
3
4
5
6
7
8
9
10
11
12
#include "Essay.h"
using namespace std;

class GradedActivity : public Essay
{
    private: 
    double score;
    public:
        double getScore(){ return score; }
       void setScore( double score2 ) {score = score2;  }
       string getGrade()  {return "Not yet handled"; }
};

What I mean is we don't use std:: in our code besides when we use using namespace std.

We never learnt anything about it. We were just told to include that namespace.

Unless you're just educating me.
I'm trying to educate you.

I'm just saying that std:: and using namespace std; are basically equivalent. I'm not saying you should be doing "std::". I think you should stick to "using namespace std;". I'm just saying they're the same thing.
Oh. Alright. Haha. Thank you!
Why are we doing

 
{return "Not yet handled"; }


The rest makes sense.
You have to evaluate the grade based on "score".
For example :
if(score >= 95) return "A+";
if(score >= 90) return "A";
if(score >= 75) return "B";
if(score >= 60) return "C";
if(score >= 45) return "D";
if(score < 45) return "F";
Last edited on
Super class

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
//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;
//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 aGrammer)
    {
        grammar=aGrammar;
    }

    double getGrammar()
    {
        return grammar;
    }

    double 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 setScore(double s)
    {
        setScore=s;

    }

    void displayPoints()
    {

    }
};

#endif // ESSAY_CLASS_H_INCLUDED 


Subclass

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
#ifndef GRADEDACTIVITY_H_INCLUDED
#define GRADEDACTIVITY_H_INCLUDED
#include "Essay class.h"

using namespace std;

class GradedActivity : public Essay
{
private:
    double score;
public:
    double getScore()
    {
        return score;
    }
    void setScore( double score2 )
    {
        score = score2;
    }
    string getGrade()
    {
        return "Not yet handled";
    }
};


#endif // GRADEDACTIVITY_H_INCLUDED 


UML diagram.
http://prntscr.com/c2kqnh


I feel like something is supposed to go in display points

1
2
3
4
5
6
7
8
9

You have to evaluate the "grade" based on "score".
For example : 
if(score >= 95) return "A+";
if(score >= 90) return "A";
if(score >= 75) return "B";
if(score >= 60) return "C";
if(score >= 45) return "D";
if(score < 45) return "F";







1
2
3
4
5
6
7
8
9
10
11
string getGrade()
    {
        string mygrade;
        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";
    return mygrade;
    }


This is just an example.
Where is your assignment question?
Oh, I'm just working through the program step by step.

1
2
3
4
 void displayPoints()
    {

    }


What about this?

Also, why is it that score is set in the super class

1
2
3
4
5
  void setScore(double s)
    {
        setScore=s;

    }


And set again in the GradedActivity
1
2
3
4
5
6
7
8
9
10
11
private:
    double score;
public:
    double getScore()
    {
        return score;
    }
    void setScore( double score2 )
    {
        score = score2;
    }


This I'm not understanding. Isn't this defeating the purpose of it inheriting from the Essay class?
Note : in the class Essay, the function setScore() has no parameter.
I am looking forward to further details of your assignment.
This was all we were given to help us. Our professor simply told us to follow the UML diagram.
Alright, here you go.

Class

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
//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;
//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;
    }

    double 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 setScore(double s)
    {
        score=s;

    }

    void displayPoints()
    {

    }
};

class GradedActivity : public Essay
{
private:
    double score;
    string mygrade;
public:
    double getScore()
    {
        return score;
    }
    void setScore( double score2 )
    {
        score = score2;
    }
    string 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";
        return mygrade;
    }
};

#endif // ESSAY_CLASS_H_INCLUDED


This is 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
72
73
74
75
76
// 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;
    double scr;
    string grde;
    userInput(grmmr, splling, correctlength, con);

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

    cout << "Total : " << grmmr + splling + correctlength 2+ con << endl;
   essayObject.addScore();
   essayObject.findGrade();
   cout << endl;
   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)." << 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


These are errors https://s8.postimg.org/ft66skrsl/Screenshot_30.png
Last edited on
(***) is my correction.

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
//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;
//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;
    }

    double 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 setScore() //  (***) Empty function, we do nothing
    {
    }

    void displayPoints()
    {

    }
};

class GradedActivity : public Essay
{
private:
    double score;
    string mygrade;
public:
    double getScore()
    {
        return score;
    }
    void setScore( double score2 )
    {
        score = score2;
    }
    string 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";
        return mygrade;
    }
};

#endif // ESSAY_CLASS_H_INCLUDED 
Pages: 123