accessing a object array

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.

course.h
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
#ifndef course_hpp
#define course_hpp

#include <stdio.h>
#include <iostream>
#include <string.h>  // C string library
#include <string>

using namespace std;


class Unit {
public:
    Unit();
    Unit( string nam, string uId, unsigned cred );
    
    //getters
    unsigned GetCredits() const;
    string GetUnitId() const;
    string GetUnitName() const;

    //setters
    void SetCredits( unsigned cred );
    void SetUnitId(string uId);
    void SetUnitName(string uName);
    
    
private:
    string unitName;
    string unitId;
    int  credits;
    
    
};

istream & operator >>( istream & input, Unit & C);
ostream & operator <<( ostream & os, const Unit & C );




#endif 


course.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
#include "course.hpp"

Unit::Unit()
{

}

Unit::Unit( string nam, string uId, unsigned cred )
{
    unitName = nam;
    unitId = uId;
    credits = cred;
}

istream & operator >>( istream & input, Unit & C)
{
    string id;
    string uName;
    int cred;
    
    input >> id;
    C.SetUnitId(id);
    
    getline(input, uName, ',');
    //input >> uName;
    C.SetUnitName(uName);
    
    input >> cred;
    C.SetCredits(cred);
    return input;
}


ostream & operator <<( ostream & os, const Unit & C )
{
    os << "  Unit ID:  " << C.GetUnitId() << '\n'
    << "  Unit Name: "  << C.GetUnitName() << '\n'
    << "  Credits: " << C.GetCredits() << '\n';
    
    return os;
}


//getters function
unsigned Unit::GetCredits() const
{
    return credits;
}
string Unit::GetUnitId() const
{
    return unitId;
}
string Unit::GetUnitName() const
{
    return unitName;
}

//setters function
void Unit::SetCredits( unsigned cred )
{
    credits = cred;
}
void Unit::SetUnitId(string uId)
{
    unitId = uId;
}
void Unit::SetUnitName(string uName)
{
    unitName = uName;
}


regist.h
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
#ifndef regist_hpp
#define regist_hpp

#include <stdio.h>
#include <iostream>

using namespace std;


class Registration {
public:
    Registration();

    //getter
    long GetStudentId() const;
    unsigned GetSemester() const;
    
    //setter
    void SetStudentId(long sId);
    void SetSemester(unsigned sem);
    
    
private:
    long studentId;             // student ID number
    unsigned semester;          // semester year, number
};

ostream & operator <<( ostream & os, const Registration & R);
istream & operator >>( istream & input, Registration & R );


#endif 


regist.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
#include "regist.hpp"

Registration::Registration()
{
    
}

istream & operator >>( istream & input, Registration & R )
{
    long id;
    unsigned sem;
    
    input >> id >> sem;
    R.SetStudentId(id);
    R.SetSemester(sem);
    
    return input;
}

ostream & operator <<( ostream & os, const Registration & R )
{
    os << "Student ID: " << R.GetStudentId() << '\n'
    << "Semester:   " << R.GetSemester() << '\n';
    
    return os;
}

//getter
long Registration::GetStudentId() const
{
    return studentId;
}
unsigned Registration::GetSemester() const
{
    return semester;
}

//setter
void Registration::SetStudentId(long sId)
{
    studentId = sId;
}
void Registration::SetSemester(unsigned sem)
{
    semester = sem;
}


date.h
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
#ifndef Date_hpp
#define Date_hpp

#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;

class Date {
public:
    Date();
    Date(unsigned int d, string m, unsigned int y);
    
    //getter
    unsigned int GetDay() const;
    string GetMonth() const;
    unsigned int GetYear() const;
    
    //setter
    void SetDay(unsigned int d);
    void SetMonth(string m);
    void SetYear(unsigned int y);
    
private:
    unsigned int day;
    string month;
    unsigned int year;
    
};

ostream & operator <<( ostream & os, const Date & D );
istream & operator >>( istream & input, Date & D );

#endif  


date.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
#include "Date.hpp"


Date::Date()
{
    
    
}

Date::Date(unsigned int d, string m, unsigned y)
{
    day = d;
    month = m;
    year = y;
}

istream & operator >>( istream & input, Date & D)
{
    int d;
    string m;
    int y;
    
    input >> d >> m >> y;
    D.SetDay(d);
    D.SetMonth(m);
    D.SetYear(y);
    
    return input;
}


ostream & operator <<( ostream & os, const Date & D )
{
    os << "  Date:  " << D.GetDay() << " " << D.GetMonth() << " " << D.GetYear() << '\n';
    
    return os;
}

unsigned int Date::GetDay() const
{
    return day;
}

string Date::GetMonth() const
{
    return month;
}

unsigned int Date::GetYear() const
{
    return year;
}

void Date::SetDay(unsigned int d)
{
    day = d;
}

void Date::SetMonth(string m)
{
    month = m;
}

void Date::SetYear(unsigned int y)
{
    year = y;
}


So all the above classes only work with basic data types and theres no issue with the set/get

however for the next class, I have no idea how to create a get/set for a object array

result.h
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
#ifndef result_hpp
#define result_hpp

#include <stdio.h>
#include <iostream>
#include "course.hpp"
#include "Date.hpp"

using namespace std;

const unsigned MaxCourses = 10;
const unsigned MaxResults = 10;
const unsigned MaxDate = 20;

class RESULT
{
public:
    RESULT ();
    

    unsigned GetCredits() const;
    unsigned GetCount() const;
    
    
    
private:
    float mark[MaxResults]; // array of marks
    Unit courses[MaxCourses]; // array of courses
    unsigned count;             // number of courses
    Date date[MaxDate]; //array of date
};

ostream & operator <<( ostream & os, const RESULT & R );
istream & operator >>( istream & input, RESULT & R );


#endif 


result.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
#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.

Last edited on
Maybe sth. like:
1
2
3
4
5
6
7
  float RESULT::getMark(int index) 
  { 
     if (index > 0 && index < MaxResults)
        return mark[index];}
     else
     { // handle error}
  }

Same princip for getDate(int index) and getCourse(int index)
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.

Small example showing what you may be looking for

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
#include <iostream>

using std::cout;
using std::endl;

class Result
{
public:
    static const unsigned 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.
Last edited on
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.

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
// get set for array mark
float RESULT::GetMark(int index)
{
    if (index > 0 && index < MaxResults)
        return mark[index];
    else
        cout << "error" << endl;
    return 0;
}

void RESULT::SetMark(int index, int n)
{
    if (index > 0 && index < MaxResults)
        mark[index] = n;
    else
        cout << "error" << endl;
}

//get set for Unit object array
Unit RESULT::GetCourse(int index)
{
        return courses[index];
}
void RESULT::SetCourse(int index, Unit u)
{
    if (index > 0 && index < 10)
        courses[index] = u;
    else
        cout << "error" << endl;
}


//get set for date object array
Date RESULT::GetDate(int index)
{
        return date[index];
}
void RESULT::SetDate(int index, Date d)
{
    if (index > 0 && index < 10)
        date[index] = d;
    else
        cout << "error" << endl;
}


and my .h
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
const unsigned MaxCourses = 10;
const unsigned MaxResults = 10;
const unsigned 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.

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
ostream & operator <<( ostream & os, RESULT & R )
{
    for(unsigned i = 0; i < R.GetCount(); i++)
        os <<'\n' << R.GetCourse(i) << "  Mark:  " << R.GetMark(i)<< '\n'
        << R.GetDate(i) << endl;
    return os;
}

istream & operator >>( istream & input, RESULT & R )
{
    unsigned c;
    Date d[0];
    Unit u[0];
    unsigned m[0];
    
    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 >> u[i] >> m[i] >> d[i];
        R.SetCourse(i, u[i]);
        R.SetMark(i, m[i]);
        R.SetMark(i, m[i]);
    }
    
    
    return input;
}


any kind soul please help to give some pointers on how I can work around this?
Last edited on
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

Saying "it doesn't work" helps nobody. 1) What was expected behavior and 2) what actually happened ?
Topic archived. No new replies allowed.