Search code examples
c++arraysclassfriend

How to pass an object(type a) to a private object(type a) of another object(type b) through function of friend


#include <iostream>
#include <vector>
using namespace std;

class Flight;

class Time {
   private :
      int hour;
      int minute;
   public :
      Time(int hour,int minute){

         this->hour = hour;
         this->minute = minute;

      };
      int getHour(){
         return hour;
      }
      int getMinute(){
         return minute;
      }
};

class Passenger{
   private:
      string name;
      int age;
   public :
      Passenger(string name , int age){

         this->name = name;
         this->age = age;

      }
      void printDetails(){

         cout << "Name: " << name << "\t";   
         cout << "Age: " << age <<"\t";              
      }
      friend void addPassenger(Passenger *p,int num,Flight f);
      friend Flight;
};

class Flight {

   private :
      string id;
      string destination;
      Time *depart;
      Time *arrival;
      vector<Passenger> passengerList;
      Passenger *pass;

   public :
      Flight(string id, string destination, Time *t, Time *a){

         this->id = id;
         this->destination = destination;
         depart = t;
         arrival = a;
         id = 3;
      };

      void printInfo(){
         cout<< "Flight Number : " << id << endl;
         cout<< "Destination : " << destination << endl;
         cout<< "Desparture : " << depart->getHour() << ":" << depart->getMinute()<< endl;
         cout<< "Arrival : " << arrival->getHour() << ":" << arrival->getMinute() << endl;
      }
      void printList(){

         cout << pass->name[0];

      }
      friend class Passenger;
      friend void addPassenger(Passenger *p,int num,Flight f);
};

void addPassenger(Passenger *p,int num,Flight f){

   //  for(int i=0;i<num;i++){

   //  f.passengerList.push_back(p[i]);
   f.pass->name = p->name;
   //  }

}

int main(){

   int num_passenger;
   int temp_age;
   string temp_name;

   vector<int> passenger_age;
   vector<string> passenger_name;

   Time t(2,4);
   Time t2(2,3);
   Flight ff("3","4",&t,&t2);

   cout<< "Enter the number of passenger" << endl;
   cin>> num_passenger;
   cout<< endl;

   Passenger *pas[num_passenger];

   for(int i=0;i < num_passenger; i++){

      cout<< "Enter the name of adult "<< i+1 << endl;
      cin>> temp_name;
      passenger_name.push_back(temp_name);
      cout<< "Enter the age of adult "<< i+1 << endl;
      cin>> temp_age;
      passenger_age.push_back(temp_age);


   }

   for(int p=0; p < num_passenger; p++){

      pas[p] =  new Passenger(passenger_name[p],passenger_age[p]);

   }
   addPassenger(*pas,2,ff);
   ff.printList();

   return 0;
}

I need to pass array object of Passenger Class into private array object of Passenger Class that inside object of Flight Class through function addPassenger. Everything compile fine, but I can't get printList(object Flight) work, it simply jumps out and terminates the console.

Sorry it quite complicated due to requirement of project. Hope to have some helps, Thanks.


Solution

  • Problem 1

    The main problem is at line

    f.pass->name = p->name;
    

    The source of the problem is that the member variable pass of Flight is not initialized properly and then you go on to use f.pass as though it is a valid pointer.

    Problem 2

    The function addPassenger takes a Flight object as input. When you call the function, the function gets a copy of the original Flight object. Any changes made to the Flight object in addPassenger are to the local copy, not the object from main.


    With the following changes, I was able to run your program without any problem.

    1. Changed the member of variable of Flight to:

      string id;
      string destination;
      Time *depart;
      Time *arrival;
      vector<Passenger> passengerList;
      

      Removed the member varible pass.

    2. Changed the signature of addPassenger to.

      void addPassenger(std::vector<Passenger> const& plist, Flight& f);
      

      and changed its implementation to

      void addPassenger(std::vector<Passenger> const& plist, Flight& f)
      {
         for(auto& p : plist ){
            f.passengerList.push_back(p);
         }
      }
      
    3. Changed the implementation of Flight::printList to:

      void printList(){
         for(auto& p : passengerList ){
            cout << p.name << endl;
         }
      }
      
    4. Changed the type of pas in main to:

      std::vector<Passenger> pas;
      

    Other things needed to be changed to account for these changes. Here's the complete program:

    #include <iostream>
    #include <vector>
    using namespace std;
    
    class Flight;
    
    class Time {
       private :
          int hour;
          int minute;
       public :
          Time(int hour,int minute){
    
             this->hour = hour;
             this->minute = minute;
    
          };
          int getHour(){
             return hour;
          }
          int getMinute(){
             return minute;
          }
    };
    
    class Passenger{
       private:
          string name;
          int age;
       public :
          Passenger(string name , int age){
    
             this->name = name;
             this->age = age;
    
          }
          void printDetails(){
    
             cout << "Name: " << name << "\t";   
             cout << "Age: " << age <<"\t";              
          }
          friend void addPassenger(std::vector<Passenger> const& plist, Flight& f);
          friend Flight;
    };
    
    class Flight {
    
       private :
          string id;
          string destination;
          Time *depart;
          Time *arrival;
          vector<Passenger> passengerList;
    
       public :
          Flight(string id, string destination, Time *t, Time *a){
    
             this->id = id;
             this->destination = destination;
             depart = t;
             arrival = a;
             id = 3;
          };
    
          void printInfo(){
             cout<< "Flight Number : " << id << endl;
             cout<< "Destination : " << destination << endl;
             cout<< "Desparture : " << depart->getHour() << ":" << depart->getMinute()<< endl;
             cout<< "Arrival : " << arrival->getHour() << ":" << arrival->getMinute() << endl;
          }
    
          void printList(){
             for(auto& p : passengerList ){
                cout << p.name << endl;
             }
          }
    
          friend class Passenger;
          friend void addPassenger(std::vector<Passenger> const& plist, Flight& f);
    };
    
    void addPassenger(std::vector<Passenger> const& plist, Flight& f)
    {
       for(auto& p : plist ){
          f.passengerList.push_back(p);
       }
    }
    
    int main(){
    
       int num_passenger;
       int temp_age;
       string temp_name;
    
       vector<int> passenger_age;
       vector<string> passenger_name;
    
       Time t(2,4);
       Time t2(2,3);
       Flight ff("3","4",&t,&t2);
    
       cout<< "Enter the number of passenger" << endl;
       cin>> num_passenger;
       cout<< endl;
    
       std::vector<Passenger> pas;
    
       for(int i=0;i < num_passenger; i++){
    
          cout<< "Enter the name of adult "<< i+1 << endl;
          cin>> temp_name;
          passenger_name.push_back(temp_name);
          cout<< "Enter the age of adult "<< i+1 << endl;
          cin>> temp_age;
          passenger_age.push_back(temp_age);
    
    
       }
    
       for(int p=0; p < num_passenger; p++){
          pas.push_back(Passenger(passenger_name[p],passenger_age[p]));
       }
    
       addPassenger(pas, ff);
       ff.printList();
    
       return 0;
    }
    

    See it working at http://ideone.com/Chttoe.