Note that there are some explanatory texts on larger screens.

plurals
  1. POAccess an Array in Classes C++
    text
    copied!<p>So I'm busy with my assignment, and I wrote a piece of code that works perfectly, but, its not ready yet. What I can figure out is how to use an array in a class. or how to get and set the items in the array, and where do I put the loop to prompt the user for the marks.</p> <p>So I want to prompt the user to enter the assignment number and marks of 3 assignments, that is stored in the array and then output the assignment and the mark. I know it sounds stupid but it will help me understand arrays in classes.</p> <p>My class is in an external file here it is </p> <pre><code> #ifndef ASSIGNMENTS_H_INCLUDED #define ASSIGNMENTS_H_INCLUDED #include&lt;iostream&gt; using namespace std; class assignments { public: assignments(); // default constructor void setAssNum(int k); // mutator int getAssNum(); // accessor void setAssMark(double m); // mutator double getAssMark(); // accessor private: int AssNum; double AssMark; }; assignments::assignments() { AssNum = 0; AssMark = 0.0; } void assignments::setAssNum(int k) { AssNum = k; } void assignments::setAssMark(double m) { AssMark = m; } int assignments::getAssNum() { return AssNum; } double assignments::getAssMark() { return AssMark; } #endif // ASSIGNMENTS_H_INCLUDED </code></pre> <p>And here is the main</p> <pre><code>int main() { int AssNum; double AssMark; assignments c; cout &lt;&lt;"Enter Assignment Number: "; cin &gt;&gt; AssNum; cout &lt;&lt;"Enter Assignment Mark in Percentage: "; cin &gt;&gt; AssMark; c.setAssNum(AssNum); c.setAssMark(AssMark); cout &lt;&lt;"The Mark for assignment "&lt;&lt; c.getAssNum() &lt;&lt;" is: " &lt;&lt; c.getAssMark()&lt;&lt;"%"; } </code></pre> <p>I don't expect anyone to rewrite the code, would just like some clarity, how to use arrays in this context.</p> <p>Thank you</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload