bool functions inside a class

I'm in search for advice on my assignment problem

So I have two classes

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
class Student { 
	//Student class declaration
public:
    //default constructor  to set  a studen data to: "NONE", "NONE",-1,'D'
    Student(string FirstName="None", string LastName="None", int Id=-1, char Type = 'D');
    //set and get students first name of string type
    void setFirstName(const string&);
    string getFirstName() const;
    //set and get students last name of string type
    void setLastName(const string&);
    string getLastName() const;
    //set and get students id number of int type
    void setId(int);
    int getId() const;
    //set and get students type 'D' char for domestic, 'I' char for international
    void setType(char);
    char getType() const;
    
private:
    string mFirstName; // the first (given) name of the student
    string mLastName; // the last(family) name of the student
    int mId; // the student ID value
    char mType; // student type -- D=domestic or I = // International)
};

class Course{ 
 //--Course class declaration 
public:
    //default constructor to set a course data to: 0,"NOT-SET", 0, 0
    Course(size_t Id=0, string Title= "NOT-SET", size_t Credits=0, size_t NrStds=0);
    
    //set and get for course id number of size_t type
    void setId(size_t);
    size_t getId() const;
    
    //set and get the number (size_t type) of students in this course
    void setNrStds(size_t);
    size_t getNrStds() const;
    
    //set and get of course name (of string type)
    void setTitle(const string&);
    string getTitle() const;
    
    //set and get of credits (of size_t type) for a course
    void setCredits(size_t);
    size_t getCredits() const;
    
    //takes an index and student id number
    //and sets students enrolled in the course into the mStudIds array
    void addStudent(size_t, size_t);
    
    //takes a student id (int) returns true if the student
    //is enrolled in the course, false otherwise
    bool hasStudent(int) const;
    
    //returns true if the course has no students
    //enrolled and false otherwise
    bool hasNoStudent() const;
    
    //takes an array of students and its size and
    //returns true if this course contains at least one student that //is not in the students array
    bool hasNonExistentStud(Student[], size_t) const;
    
private:
    size_t mId; //course id, like 158753
    string mTitle; //course name, like Automata-theory
    size_t mCredits; //credits for this course, like 15
    size_t mNrStds; //total number of students enrolled in this course
    int mStudIds[MAX_STUDENTS];//array of ids of students enrolled in this course
};


and I have a problem with those two bool functions of Course 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
//takes a student id (int) returns true if the student     
//is enrolled in the course, false otherwise
bool Course::hasStudent(int id) const{
	for(size_t i=0; i<mNrStds; ++i) {
		if (id==mStudIds[i]) {
			return true;
			break;
		}
	}
	return false;    
}

//takes an array of students and its size and     
//returns true if this course contains at least one student that     
//is not in the students array 
bool Course::hasNonExistentStud(Student[], size_t size) const{
	bool studentExists;
	for(size_t i=0;i<mNrStds; ++i) {
		studentExists = false;
		for (size_t s=0; s<size; s++) {
			if (Student[s]==mStudIds[i]) {
				studentExists=true;
				break;
			}
		}
		if (!studentExists)
			return true;
	}
	return false;
}


I am 100% sure they are wrong, but I have no idea how to fix them. Do I have to pull private data from Student class to make them work? If yes, then how? Do I have to use friendship relations? Thanks
Course::hasStudent looks okay. Are you saying it doesn't work correctly? You don't need the break, though, since you have the return there.

1
2
3
4
5
6
bool Course::hasStudent(int id) const {
	for (size_t i = 0; i < mNrStds; ++i)
		if (id == mStudIds[i])
			return true;
	return false;    
}

Course::hasNonExistentStud is almost correct. You just need to give the Student array a name and access the ID with getId().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool Course::hasNonExistentStud(Student student[], size_t size) const {
	for (size_t i = 0; i < mNrStds; ++i) {
		bool studentExists = false;
		for (size_t s = 0; s < size; s++) {
			if (student[s].getId() == mStudIds[i]) {
				studentExists = true;
				break;
			}
		}
		if (!studentExists)
			return true;
	}
	return false;
}

Last edited on
for the first bool I don't understand how do we pass student id from Student class since it's the Course function- because it is the Student class that has all existing IDs, and Course can have ids that repeat or those ids of non-existing students

And for the second- I do not understand how do we pass an array of Students which is also a thing from Student class. the getId function gets ID of a subject, not a student. I hope I'm making sense here.
I hope I'm making sense here.

Not really.

You haven't shown anything about how you are using these functions, so how would I know how you will pass a student ID or array of student IDs to them? That's up to you. Maybe you ask the user to enter a student ID and get it from there. An ID is just an integer.

An "array of Students" is not something "from the Student class". The Student class is for one student. Again, I have no idea how you are creating that array. That's up to you. Presumably it happens elsewhere in the full program.

And calling getId() on a Student gets the student's ID, not a Course ID.
Last edited on
I'm sort of getting the gist with Student getId, so thank you

we're using these functions with overloading operators

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
//oveloading output operators:
//sends Course data to the  output stream
ostream& operator<<(ostream&, const Course&);

//sends Student data to the  output stream
ostream& operator<<(ostream&, const Student&);

//oveloading input operators:
//initialize a Course with data read in from input stream 
istream& operator>>(istream&, Course &);
//initialize a Student with data read in from input stream 
istream& operator>>(istream& , Student&);

ostream& operator<<(ostream& out, const Student& aStd){
    out<<aStd.getFirstName()<<" " <<aStd.getLastName() <<" "<<aStd.getId()<<" "<<aStd.getType()<< endl;
    return out;
}

istream& operator>>(istream& in, Student& aStd){
    string FN;
    string LN;
    int id;
    char S;
	 
    in>>FN>>LN>>id>>S;
    aStd.setFirstName(FN);
    aStd.setLastName(LN);
    aStd.setId(id);
    aStd.setType(S);
    return in;
}

istream& operator>>(istream& inCourse, Course& aCrs){
    size_t id;
    string t;
    size_t cr;
    int std;
    size_t i=0;
	 size_t num=0;
	 inCourse>>id;
	// cout<<id<<endl;
	 aCrs.setId(id);
	 inCourse>>t;
	// cout<<t<<endl;
	 aCrs.setTitle(t);
	 inCourse>>cr;
	// cout<<cr<<endl;
	 aCrs.setCredits(cr);
	 inCourse>>std;
	while(std!=-1){
		 //cout<<std<<endl;
		 aCrs.addStudent(i, std);
		 i++;
		 inCourse>>std;
	 }   
	 num=i;
	 aCrs.setNrStds(num);
	 return inCourse;
}

ostream& operator<<(ostream& out, const Course& aCrs) {
	if(aCrs.hasNoStudent()!=0){
	out<<aCrs.getId()<<" "<<aCrs.getTitle()<<" "<<aCrs.getCredits()<<endl;
	}	//else if (aCrs.hasStudent()!=0
	return out;
}


for the actual program output we're supposed to show courses with no students (that's with aCrs.hasNoStudent() which I got working) and also students that dont exist in Student class but are in courses and finally students that are not enrolled in any course. We are reading from two files- courses and students, I've figured how to do that as well, it's only those last two outputs that throw me off
Last edited on
You say you've figured out how to read the files, so you must be reading the students and courses into arrays (for some reason, you don't seem to be using vectors). So that's where the student array comes from.

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
int main() {
    // read students
    int nrStuds = 0;
    Student students[MAX_STUDENTS];
    ifstream studFile("students.txt"); // or whatever its called
    while (nrStuds < MAX_STUDENTS && studFile >> students[nrStuds])
        ++nrStuds;
    studFile.close();

    // read courses
    int nrCourses = 0;
    Course courses[MAX_COURSES];
    ...  // similar to students above

    // print courses with no students
    ... // easy; just go through courses and print those with mNrStuds == 0

    // print student ids found in courses but not in student array
       // Using hasNonExistentStud:
       for (int i = 0; i < nrCourses; ++i)
           if (courses[i].hasNonExistentStud(students, nrStuds))
               print course info
    
    // print students with no courses
       // Using hasStudent:
       for (int s = 0; s < nrStuds; ++s) {
           bool found = false;
           for (int c = 0; c < nrCourses; ++c)
               if (courses[c].hasStudent(students[s].getId()) {
                   found = true;
                   break;
               }
           if (!found) // student is not in any course
               print student info
       }
}

Last edited on
yes, i do indeed read from files into array of objects of each class, the first part of your code is similar to mine.

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
int main(){
    int totalstds;
	 int totalcrs;
    Student obj[MAX_STUDENTS];
	 
    ifstream fstudents (STDS_IN);
    if (!fstudents){
        cerr<<"error - listStds.txt did not open"<<endl;
    }
    fstudents>>totalstds;
	 for (int i=0; i<totalstds; ++i) {
		 fstudents>>obj[i];
	 }
	 Course object[MAX_COURSES];
	 fstream fcourses (CRS_IN);
    if (!fcourses){
      cerr<<"error - listCrs.txt did not open" << endl;
    }
	 fcourses>>totalcrs;
	 for (int i=0; i<totalcrs; ++i) {
		 fcourses>>object[i];
	}
	for (int i=0; i<totalcrs; ++i) {
		 cout<<object[i];
	}
}


however, we are restricted to using istream and ostream for our output, and that's what makes it too difficult to grasp. How do I use ostream and bool functions with parameters in order to produce output?
EDIT: nvm figured it our myself, but your help pushed me in the right direction, thank you very much!
Last edited on
Topic archived. No new replies allowed.