Can't figure out how to create this constructor

May 7, 2017 at 3:38am
So my goal is to create a class called scoreRecord that will track scores for students received in their course. I have defined the class as following:
[code]
class scoreRecord{
private:
string courseName;
int scoreAmt; //total number of scores to be added to dynamic array
int * scores; //points to scores to be stored inside of dynamic array
public:
scoreRecord(string courseName, int scoreAmt);
};
// This is where I am having issues is making the constructor
// With a dynamic array pointed to by scores.

scoreRecord::scoreRecord(string cName, int sAmt){
courseName = cName;
scoreAmt = sAmt;
new arrY[scoreAmt];
}
[code/]
Last edited on May 7, 2017 at 4:11am
May 7, 2017 at 3:53am
Below is one way you could do it however acquiring resources through the ctor is a detailed exercise that requires deleting the resources before the objects go out of scope and applying the rule of three/five (google if required). So I've also shown an alternative way of doing it using std::vector<int>:
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
class scoreRecord
{
    private:
        std::string m_courseName;
        size_t m_scoreAmt; //total number of scores to be added to dynamic array
        int * m_scores; //points to scores to be stored inside of dynamic array
        //std::vector<int> m_scores;
    public:
        scoreRecord(const std::string& courseName, const int scoreAmt)
            : m_courseName(courseName), m_scoreAmt(scoreAmt)
        {
            m_scores = new int[m_scoreAmt];
            for (size_t i = 0; i < m_scoreAmt; ++i)
            {
                std::cout << "Enter score " << i + 1 << " of " << m_scoreAmt;
                std::cin >> m_scores[i];
            }
          /*  for (size_t i = 0; i < m_scoreAmt; ++i)
            {
                std::cout << "Enter score " << i + 1 << " of " << m_scoreAmt;
                int temp{};
                std::cin >> temp;
                m_scores.push_back(temp);
            }*/
        }
};

Edit:
(a) though the standard allows it, it is confusing to use the same variable name for ctor arguments and class data-members, so I've added a 'm_' for the members;
(b) if you are familiar with smart pointers like std::unique_ptr you could use that instead of the C-style pointer and the smart pointer usually cleans up after itself so to speak and so memory management is more straightforward
Last edited on May 7, 2017 at 4:17am
May 7, 2017 at 4:39am
This program is killing me never struggled with any assignments in this course before, ever. I came up with about the same as you posted, I just formatted different. (minus the better variables, that is a good pointer thank you for that) So basically, I'm trying to create overloaded extraction, and insertion operators to perform the tasks of prompting for scores, and displaying them. I have been searching online for examples of this to no avail. The main driver I have created is:
[code]int main()
{
scoreRecord sr1("Algebra", 4);
scoreRecord sr2("Environmental Science", -1); //The constructor should change -1 to 2
scoreRecord sr3("TBA", 2);

cout << "\nGetting Student 1 Scores...\n";
cin >> sr1;

cout << "\nGetting Student 2 Scores...\n";
cin >> sr2;

cout << "\nDisplaying Student 1 Scores...\n";
cout << sr1;

cout << "\nDisplaying Student 2 Scores...\n";
cout << sr2;

cout << "\nAssigning Student 1 data to Student 3...\n";
sr3 = sr1; // Trying to create an overloaded = operator that will assign one scoreRecord
//equal to the other. (direct copy)

cin.ignore(100, '\n');
cin.get();

return 0;
}[code/]

I want to change the m_scoreAmt to 2 if the input size is less than that.
I am doing an include of the scoreRecord.h class as a header.
Last edited on May 7, 2017 at 4:56am
May 7, 2017 at 4:58am
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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

class scoreRecord
{
    private:
        std::string m_courseName;
        int m_scoreAmt; //total number of scores to be added to std::vector<int>
        std::vector<int> m_scores;
    public:
        scoreRecord(const std::string& courseName, const int scoreAmt)
            : m_courseName(courseName)
            {
                m_scoreAmt = std::max(2, scoreAmt);
                for (size_t i = 0; i < m_scoreAmt; ++i)
            {
                std::cout << "Enter score " << i + 1 << " of " << m_scoreAmt << " for " << m_courseName << ": ";
                int temp{};
                std::cin >> temp;
                m_scores.push_back(temp);
            }
        }
        friend std::ostream& operator << (std::ostream& os, const scoreRecord& s);

};
std::ostream& operator << (std::ostream& os, const scoreRecord& s)
{
    os << s.m_courseName << " has " << s.m_scoreAmt << " scores, which are: \n";
    for (const auto& elem : s.m_scores)os << elem << " "; os << "\n";
    return os;
}

int main()
{
    scoreRecord sr1{"Algebra", 4};
    scoreRecord sr2{"Environmental Science", -1};

    std::cout << sr1 << "\n" << sr2;

}
Last edited on May 7, 2017 at 4:59am
May 7, 2017 at 5:17am
I see how you can construct the overloaded operators now, thank you so much. I couldn't figure out the initial syntax, but I should be able to create the other overloaded operators using the concepts of how you constructed yours. I really appreciate you taking the time to help me.
Last edited on May 7, 2017 at 5:31am
May 7, 2017 at 6:13am
So I am trying to do what you did inside of the constructor inside of an >> operator overload. I want to be able to read in the class objects, but I can't figure out how to do this. I keep getting out of scope errors. I rewrote the << operator overload to:
[code]ostream& operator << (ostream& os, const scoreRecord& s)
{
os << s.m_courseName << " has " << s.m_scoreAmt << " scores, which are: \n";
for (size_t i = 0; i < s.m_scoreAmt; i++){
cout << s.m_scores[i] << "\n";
}
return os;
}[code/] How you did it didn't work for my version of c++. But do you have any idea how you can display, and read in the information the way you did in the constructor inside of a >> overload. All I get are out of scope errors, I am also trying to set up a limit on the scores of 0-100, so I guess being inside of the if statements is making it go out of scope?

This is what I have made so far, but I get a error can't figure out how to read in & pass score values
[code]ostream& operator >> (ostream& in, const scoreRecord& s)
{
for(size_t i=0; i< s.m_scoreAmt; ++i)
{
cout << "Enter score " << i+1 << " of " << s.m_scoreAmt << " for " << s.m_courseName << ": ";
int temp{};
in >> temp;
s.m_scores.push_back(temp);
}
}[code/]
Last edited on May 7, 2017 at 7:14am
Topic archived. No new replies allowed.