0

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?

11
  • 4
    May I ask why you're using pointers, and arrays? Do you happen to know what std::vector is? Commented Nov 11, 2020 at 19:51
  • 1
    pointer_array[stc] = &student; stores a pointer to a local copy of Student - it's not too surprising it always has the same address, considering you are calling the function in a loop Commented Nov 11, 2020 at 19:52
  • 1
    The problem in the shown code is a well known problem called "useless use of pointers". Nothing in the shown code requires the use of new, or delete. Using new to create a pointer to a new object, but then immediately making a useless copy of the same object in order to pass it by value to another function -- this doesn't really accomplish anything useful. Commented Nov 11, 2020 at 19:57
  • 2
    The quick fix would be to change the function parameter to take a pointer directly (Student* student) since you already have those Student objects dynamically allocated anyway Commented Nov 11, 2020 at 19:58
  • 1
    @ScatterBrainer the code won't compile by changing just the parameter because you will now need to also pass a pointer to the function, which you dereference when you call *(students[i]). You will need to change that to students[i] Commented Nov 11, 2020 at 20:07

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.