Set/Get Constructors, Pointers and Classes

I have an assignment to create a gradebook program using C++. I am not allowed to use vector or list and I'm suppose to be using new and delete to dynamically allocate the amount of students and courses needed. Then add grades to them and do other stuff. I can probably do the other stuff if someone can help me on my first hurdle. So every variables in my class needs to be private. I know how to use set and get and constructors but basic knowledge of both. That goes the same with pointers and classes. I don't know how to access my private variables so I can do whatever I needed to do with them.

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
  class Student {
   public:
     //showing my knowledge i know how set and get works
     void setID(int ID) {student_id = ID; };
     int getID() {return student_id; };
   private:
     int student_id;
     int count_student;
     string lastName;
     string firstName;
     int enroll;
     int *grades;
  };

 class Course {
  public:
  private:
   int course_id;
   int count_course;
   string courseName;
   Student *students;
 }

 class Gradebook {
  public:
  private:
   Course *courses;
 };



I know C++ doesn't have a version of realloc so what I have been doing in the past is create temp pointer and copy my array to the temporary pointer then have my class pointer to temporary pointer like so.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 Course *temp;

 for(i=0; i<count; i++) {
  temp[i]=courses[i];
 }
 course = NULL; 
 delete []courses;

 //do stuff with the temporary pointer

 //copy temp to original and delete temp
 course = temp;
 temp = NULL;
 delete []temp;


So how do I add information in courses and print it out?

One last thing I have my files separated.

main.h/main.cpp/class.cpp/class.h

main.cpp -> with a switch menu to call functions and function prototypes
main.h -> function calls from main.cpp
class.cpp -> functions from class.h
class.h -> function protoype and definition





Last edited on

You need to dynamically allocate memory when the object of Student class is created. Allocate it in the constructor, free the memory in the deconstructor; something like..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

// Student Class
class Student
{
	private:
		int *pGrades;


	public:
		Student() {
			// allocate 10 spaces for grades.
			pGrades = new int[10]; 
		}	
		~Student() { 
			// free the memory
			delete[] pGrades; 
		}

};


what I have been doing in the past is create temp pointer and copy my array to the temporary pointer

You cant just copy data to a pointer location as you are essentially overwriting memory that doesn't belong to you. You need to allocate memory for your use. Apologies if that's not what you mean but thats how I read it.
Softrix,

Then how do I reallocate a using new? I already know how to allocate it.

<variable> = new <data type>

But when I ever try to do <variable> = new <data type>[number of reallocation] the old memory gets erased. Infact it reallocates the entire array and I get zeros as a result regardless of the new data I stored.

The logical way I can see for this since you may need to add to the allocated memory size would be to take a copy of the data in the old allocated memory, free the old memory, reallocated it at the new size and copy the old contents back.

So easy with vectors but you cant use them which is a shame.

Last edited on
Softrix,

I know what you mean. I've actually done this homework already using vectors. But then on Thursday he told us we aren't allowed to use STL. I complained, ALOT, considering he didn't tell us that in our assignment sheet. So I have to rewrite this doing good old fashion pointers. Could you give me an example on what your talking about?

Say you have the following class pointers, which has been allocated. How would you allocate it?

Class *pointer = new Class;

//how would you copy and resize it?
nvm i got it, i used memcpy

What I had in mind was something like this, first it allocates a initial 10 size array, when this size is reached it allocates a further 10 slots.

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

#include <iostream>
using namespace std;

// Student Class
class Student
{
	private:
		// grades
		int *pGrades;
		int allocMem;	// whats currently allocated.
		int numGrades;	// number of grades.

	public:
		Student();
		~Student();

		// grades
		void addGrade(int grade);
		int getGrade(int index);

};

// Constructor
Student::Student()
{
	// we start off with zero grades.
	numGrades = 0;
	allocMem = 10;	// 10 slots initially
	pGrades = new int[allocMem];
}

// Destructor
Student::~Student()
{
	delete[] pGrades;

}

// Add a Grade
void Student::addGrade(int grade)
{
	int *pTempMemory;

	if (numGrades == allocMem) {
		// we need to allocate more, allocate temp
		// mem and take a copy
		pTempMemory = new int[numGrades];
		memcpy(pTempMemory, pGrades, sizeof(int)*numGrades);
		// free old allocation
		delete[] pGrades;
		// allocate at new size (add 10 new slots)
		pGrades = new int[allocMem + 10];
		// copy the memory back
		memcpy(pGrades, pTempMemory, sizeof(int)*numGrades);
		// free up the temp memory
		delete[]pTempMemory;
		*(pGrades + numGrades) = grade;		// store the value
		numGrades += 1;	// we added a grade
		allocMem += 10;						// new size counter
	}
	else
	{
		// havent reached our already allocated memory
		*(pGrades + numGrades) = grade;		// store the value
		numGrades += 1;	// we added a grade
	}
}

// Read the value
int Student::getGrade(int index)
{
	return *(pGrades + index);
}



int main()
{
	
	Student myTestStudent;

	// enter 50 numbers
	for (int i = 0; i < 50; i++)
		myTestStudent.addGrade(i + 1);

	// display them
	for (int i = 0; i < 50; i++)
		cout << myTestStudent.getGrade(i) << endl;

	return 0;
}

If you prefer not to use memcpy, you can use pointer notation in a for loop.
1
2
		for (int i = 0; i < numGrades; i++)
			*(pTempMemory + i) = *(pGrades + i);

Softrix,

Your code looks good and to be honest I know how to do that. How would you approach something like when its a pointer to a pointer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

class Course {
public:
private:
 string name_of_course;
 int course_id;
 int *enroll
};

class Gradebook {
public:
private:
 Course *courses;
};


where course_id = the id of the students; and *enroll are the students are the students currently enrolled in that course_id?

course_id shouldn't be the id of the students should it?

course_id should be a unique id for the course, student_id should be a unique id for the student that never changes.

A course can have many students, and many students could be on many different courses in that term. Think about this logically, when you join a college you would be given a student number that never changes throughout your history at the college and that id could be used to track your record of achievements, courses you have attended and more.

This is pretty much "one to many" relationships if you were developing a database.
Topic archived. No new replies allowed.