I have a class named Student. I create many student objects in my main (each object representing one student.)What i'm really trying to do is pass each one of these students to function enter of School class, representing that a student enters the school and then print his/her name etc.Here's the code:
my study.h file consists of:
#include <iostream>
#include <cstring>
using namespace std;
class Student
{
private:
string name;
int no_floor;
int no_classroom;
public:
Student(const string& nam,int no_fl,int no_cla)//constructor
: name(nam), no_floor(no_fl), no_classroom(no_cla)
{
cout << "A new student has been created! with name " << name << " heading to floor: "<< no_floor << " class: " << no_classroom << endl;
};
~Student() //destructor
{
cout << "A Student to be destroyed! with name " << name << " is at floor: " << no_floor << " class: " << no_classroom;
};
Then the School class:
class School
{
private:
Student* pointer_array[2];
public:
School()//constructor
{
cout << "A New School has been created!" << endl;
};
~School(){//destructor
cout << "A School to be destroyed!" << endl;
};
void enter(Student student, int stc=0/*student counter*/);
};
on my main.cpp file: (memory allocation for each student)
#include <iostream>
#include <cstring>
#include <study.h>
using namespace std;
int main(void)
{
//Student creation
int i,floor,classroom;
string stname;
Student* students[2];
for(i=0; i<2; i++)
{
cin >> stname;
cin >> floor;
cin >> classroom;
students[i] = new Student(stname, floor, classroom);
}
School sch;
for(i=0; i<2; i++)
{
sch.enter(*(students[i]),i);
}
for(i=0; i<2; i++)
{
delete students[i];
}
}
Lastly on my study.cpp file i've got the School class function where i'm trying to pass each object by reference and not by coping them to a new object:
#include <iostream>
#include <cstring>
#include <study.h>
using namespace std;
void School::enter(Student student, int stc/*student counter*/)
{
pointer_array[stc] = &student;
cout << "pointer array[" << stc << "]:" << pointer_array[stc] << endl;
//^ this cout prints the same adress for both students array[0]:0x1ffefffd20
// array[1]:0x1ffefffd20
}
Any ideas on how to pass pointers to all students and not just one. Again i'm trying to pass the array by reference.Thoughts?
here's the solution to this problem School class:
class School
{
private:
Student* pointer_array[5];
public:
School()//constructor
{
cout << "A New School has been created!" << endl;
};
~School(){//destructor
cout << "A School to be destroyed!" << endl;
};
void enter(Student* student, int stc=0/*student counter*/);
};
Student class doesn't change:
class Student
{
private:
string name;
int no_floor;
int no_classroom;
public:
Student(const string& nam,int no_fl,int no_cla)//constructor
: name(nam), no_floor(no_fl), no_classroom(no_cla)
{
cout << "A new student has been created! with name " << name << " heading to floor: "<< no_floor << " class: " << no_classroom << endl;
};
~Student() //destructor
{
cout << "A Student to be destroyed! with name " << name << " is at floor: " << no_floor << " class: " << no_classroom;
};
Then main.cpp:
#include <iostream>
#include <cstring>
#include <study.h>
using namespace std;
int main(void)
{
//Student creation
int i,floor,classroom;
string stname;
Student* students[2];
for(i=0; i<2; i++)
{
cin >> stname;
cin >> floor;
cin >> classroom;
students[i] = new Student(stname, floor, classroom);
}
School sch;
for(i=0; i<2; i++)
{
sch.enter(students[i],i);
}
for(i=0; i<2; i++)
{
delete students[i];
}
}
Lastly the study.cpp function:
void School::enter(Student* student, int stc/*student counter*/)
{
pointer_array[stc] = student;
(pointer_array[stc])->print();
cout << " enters school!" << endl;
cout << "pointer array[" << stc << "]:" << pointer_array[stc] << endl;
}