Hi everyone, part of my assignment requires me to 'unfriend' my overload << >> operator and use set/get to access private variables. I've successfully converted(unfriend) my overloaded << >> operators for most of my classes. However I'm at loss at accessing private object array. Below is the classes with successful 'unfriended'fstream operators.
#include "result.hpp"
RESULT::RESULT()
{
count = 0;
}
unsigned RESULT::GetCredits() const
{
// a loop to add up the credits and return total credits.
unsigned sum = 0;
for(unsigned i = 0; i < count; i++)
sum += courses[i].GetCredits();
return sum;
}
ostream & operator <<( ostream & os, const RESULT & R )
{
for(unsigned i = 0; i < R.GetCount(); i++)
os <<'\n' << R.courses[i] << " Mark: " << R.mark[i] << '\n'
<< R.date[i] << endl; //input sequence for courses into array, then mark.
return os;
}
istream & operator >>( istream & input, RESULT & R )
{
unsigned c;
input >> c; // take in the count from rinput.doc
R.SetCount(c);
for(unsigned i = 0; i < R.GetCount(); i++) //use the count to set the limit for loop.
input >> R.courses[i] >> R.mark[i] >> R.date[i]; // input sequence for the file.
return input;
}
unsigned RESULT::GetCount() const
{
return count; // for the loop in istream and ostream for number of times required to loop.
}
how can I create a get/set for courses[MaxCourses], mark[MaxResults] and date[MaxDate] and use the get/set in the for loop?
I have a sensing that pointer is needed for this. But I have no idea how do I design the function to access and set it.
Could return the pointer as well, but, as I understand it, it's frowned upon in modern C++. Prefer STL containers like vector for variable-sized arrays and array for static-sized arrays.
#include <iostream>
using std::cout;
using std::endl;
class Result
{
public:
staticconstunsigned MAX_RESULTS = 3;
Result()
{
for (int i=0; i<MAX_RESULTS; ++i)
mark_[i] = 0.2f;
}
float* const MarkPtr()
{
return mark_;
}
private:
float mark_[MAX_RESULTS];
};
int main()
{
Result r;
float* const markptr = r.MarkPtr();
for (int i=0; i<r.MAX_RESULTS; ++i)
cout << markptr[i] << ' ';
cout << endl << endl;
markptr[1] = 99.55f;
for (int i=0; i<r.MAX_RESULTS; ++i)
cout << markptr[i] << ' ';
cout << endl;
return 0;
}
0.2 0.2 0.2
0.2 99.55 0.2
If declared with stack memory, as in my example, both mark_ and &mark_[0] should be the same.
On a somewhat unrelated note, the pointer coming back doesnt appear to be constant (can actually go ahead and reassign it to something else). Was an issue with auto removing constness -- corrected the example to have caller as float* const instead of auto.
Hi Thomas1965, thank you so much for your suggestion. I understand that the data type for 'mark' is float. Thus im able to use the 'float' data type when declaring the function. But what if its a case of another class object? What data type should i use to replace.?
e.g.
Unit Result::getUnit(int index);
And with regards to the set functions. Which parameter can i put to pass the array into? E.g.
1 2 3 4 5 6
float Result::setMark(what goes in here?)
{
Mark[i] = whatever in parameter;
}
I apologise in advance for not posting any workable code for this. Im trying to grasp the logic by implementing what i have used(set/get methods) for the previous class on the current case.
Thanks in advance if anyone can provide learning materials that i can read upon or any keyword for this type of problem which i can search around for.
edit::
I've found related article on returning array thus I've used the recommended method in the article to create my get/set as shown below.
constunsigned MaxCourses = 10;
constunsigned MaxResults = 10;
constunsigned MaxDate = 20;
class RESULT
{
public:
RESULT ();
unsigned GetCredits() const;
unsigned GetCount() const;
void SetCount(int c);
float GetMark(int index);
void SetMark(int index, int n);
Unit GetCourse(int index);
void SetCourse(int index, Unit n);
Date GetDate(int index);
void SetDate(int index, Date d);
private:
float mark[MaxResult]; // array of marks
Unit courses[MaxCourses]; // array of courses
unsigned count; // number of courses
Date date[MaxDate];
};
ostream & operator <<( ostream & os, RESULT & R );
istream & operator >>( istream & input, RESULT & R );
at these point everything seems to be alright to me (please point out if theres any problem with this) but my program still couldn't work and I suspect it gotta do with the loop function that read in data and store into each array object.