fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. class Flight;
  6.  
  7. class Time {
  8. private :
  9. int hour;
  10. int minute;
  11. public :
  12. Time(int hour,int minute){
  13.  
  14. this->hour = hour;
  15. this->minute = minute;
  16.  
  17. };
  18. int getHour(){
  19. return hour;
  20. }
  21. int getMinute(){
  22. return minute;
  23. }
  24. };
  25.  
  26. class Passenger{
  27. private:
  28. string name;
  29. int age;
  30. public :
  31. Passenger(string name , int age){
  32.  
  33. this->name = name;
  34. this->age = age;
  35.  
  36. }
  37. void printDetails(){
  38.  
  39. cout << "Name: " << name << "\t";
  40. cout << "Age: " << age <<"\t";
  41. }
  42. friend void addPassenger(std::vector<Passenger> const& plist, Flight& f);
  43. friend Flight;
  44. };
  45.  
  46. class Flight {
  47.  
  48. private :
  49. string id;
  50. string destination;
  51. Time *depart;
  52. Time *arrival;
  53. vector<Passenger> passengerList;
  54.  
  55. public :
  56. Flight(string id, string destination, Time *t, Time *a){
  57.  
  58. this->id = id;
  59. this->destination = destination;
  60. depart = t;
  61. arrival = a;
  62. id = 3;
  63. };
  64.  
  65. void printInfo(){
  66. cout<< "Flight Number : " << id << endl;
  67. cout<< "Destination : " << destination << endl;
  68. cout<< "Desparture : " << depart->getHour() << ":" << depart->getMinute()<< endl;
  69. cout<< "Arrival : " << arrival->getHour() << ":" << arrival->getMinute() << endl;
  70. }
  71.  
  72. void printList(){
  73. for(auto& p : passengerList ){
  74. cout << p.name << endl;
  75. }
  76. }
  77.  
  78. friend class Passenger;
  79. friend void addPassenger(std::vector<Passenger> const& plist, Flight& f);
  80. };
  81.  
  82. void addPassenger(std::vector<Passenger> const& plist, Flight& f)
  83. {
  84. for(auto& p : plist ){
  85. f.passengerList.push_back(p);
  86. }
  87. }
  88.  
  89. int main(){
  90.  
  91. int num_passenger;
  92. int temp_age;
  93. string temp_name;
  94.  
  95. vector<int> passenger_age;
  96. vector<string> passenger_name;
  97.  
  98. Time t(2,4);
  99. Time t2(2,3);
  100. Flight ff("3","4",&t,&t2);
  101.  
  102. cout<< "Enter the number of passenger" << endl;
  103. cin>> num_passenger;
  104. cout<< endl;
  105.  
  106. std::vector<Passenger> pas;
  107.  
  108. for(int i=0;i < num_passenger; i++){
  109.  
  110. cout<< "Enter the name of adult "<< i+1 << endl;
  111. cin>> temp_name;
  112. passenger_name.push_back(temp_name);
  113. cout<< "Enter the age of adult "<< i+1 << endl;
  114. cin>> temp_age;
  115. passenger_age.push_back(temp_age);
  116.  
  117.  
  118. }
  119.  
  120. for(int p=0; p < num_passenger; p++){
  121. pas.push_back(Passenger(passenger_name[p],passenger_age[p]));
  122. }
  123.  
  124. addPassenger(pas, ff);
  125. ff.printList();
  126.  
  127. return 0;
  128. }
  129.  
Success #stdin #stdout 0s 3468KB
stdin
2
John 22
Jane 25
stdout
Enter the number of passenger

Enter the name of adult 1
Enter the age of adult 1
Enter the name of adult 2
Enter the age of adult 2
John
Jane