Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    [C++] puntatori: array di puntatori

    Salve, solo voi potete farmi da tutor.

    per esercizio ho dovuto creare un oggetto student, e uno student_club, come segue:

    codice:
    #include <iostream>
    #include <string>
    using namespace std;
    
    /*------------------------------------------------Class student---------------*/
    class Student
    {
    public:
       Student();
       Student(string student_name, string major_at_acceptance);
       void set_major(string new_major);
       string get_major() const;
       string get_name() const;
       void set_name(string new_name);
    private:
       string name;
       string major;
    };
    
    
    Student::Student()
    {
    name = "";
    major = "";
    }
    Student::Student(string student_name, string major_at_acceptance)
    {
    name=student_name;
    major=major_at_acceptance;
    }
    void Student::set_major(string new_major)
    {
    major = new_major;
    }
    
    void Student::set_name(string new_name)
    {
    name = new_name;
    }
    
    string Student::get_major() const
    {
    return major;
    }
    string Student::get_name() const
    {
    return name;
    }
    /*----------------------------------------------------------------------------*/
    
    /*--------------------------class Student Club--------------------------------*/
    
    
    class student_club
    {public:
          student_club();
          student_club(string club_name);
          string get_name() const;
          void set_president(Student* a);  
          string get_president() const;  
          void set_secretary(Student* b);
          string get_secretary() const;
     private:
             string name;         
             string President;
             string Vicepresident;
             string Secretary;
             string Treasurer;
                  
          
          };
    
    student_club::student_club()
    {}
    
    student_club::student_club(string club_name)
    {
                                      
        name=club_name;                              
                                      
                                      }
    string student_club::get_name() const
    {
           
           return name;
           
           
           }
    
    void student_club::set_president(Student* c)
    {
         President = c->get_name();
         
         
         }
    
    string student_club::get_president() const
    {
           return President;
           
           }
    void student_club::set_secretary(Student* b)
    {
         Secretary=b->get_name();
         
         
         }
    
    
    string student_club::get_secretary() const
    {
           return Secretary;
           
           }
    
    
    
    
    
    
    /*----------------------------------------------------------------------------*/
    
    int main(void)
    {
    
    Student* AAAA= new Student("Pippo Paperino","scienze");
    Student* AAAB= new Student("Braccobaldo Bau","Lettere");
    Student* AAAC= new Student("Piffo Paffo","Ingegnerie");
    student_club* scacchi = new student_club("Schacci");
    student_club* bridge = new student_club("bridge club");
    
    scacchi->set_president(AAAA);
    scacchi->set_secretary(AAAC);
    bridge->set_president(AAAB);
    bridge->set_secretary(AAAA);
    
    
    cout<<"il club è : "<<scacchi->get_name()<<'\n';
    cout<<"il presidente è : "<<scacchi->get_president()<<'\n';
    cout<<"che essendo primcipalmente studente frequenta "<<AAAA->get_major()<<'\n';
    
    cout<<"ci sono due club : "<<'\n';
    cout<<scacchi->get_name()<<" e "<<bridge->get_name()<<'\n';
    cout<<"il presidente di scacchi è : "<<scacchi->get_president()<<'\n';
    cout<<"il segretario di scacchi è : "<<scacchi->get_secretary()<<'\n';
    cout<<"il presidente di bridge è  : "<<bridge->get_president()<<'\n';
    cout<<"il segretario di bridge è  : "<<bridge->get_secretary()<<'\n';
    
    cout<<"codeste persone studiano anche in : "<<AAAA->get_major()<<" "<<AAAB->get_major()
    <<" "<<AAAC->get_major()<<'\n';
    
    system("PAUSE");
    return 0;
    }

    ora devo fare un ulteriore esercizio:

    Usando la classe Student_Club class creata in P3, si aggiunga un campo addizionale,
    members, costituito da un array di puntatori a oggetti di tipo Student.

    MI date una mano??

    Il campo members, devo passarlo nel costruttore? tipo :

    codice:
    student_club(string club_name, Student* members[]);
    e poi nella definizione del costruttore come lo inizializzo??


    Grazie mille a tutti.

  2. #2
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Ogni club ha un numero fisso di membri oppure no?
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  3. #3
    per semplicità assmumiamo di si

  4. #4
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Qualcosa del genere.
    codice:
    class student_club {
       public:
           /* tutto il resto */
           void addMember(Studente* stud) {
               if (numMembers < MAX_MEMBERS) {
                   members[numMembers++] = stud;
               } else {
                   delete stud;
               }
           }
       private:
           /* tutto il resto */
          const int MAX_MEMBERS = 16;
          Studente* members[MAX_MEMBERS];
          int numMembers; // va inizializzato a 0 nel costruttore.    
        
    };
    Questo in linea di principio.
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  5. #5
    grazie, lo avevo immaginato che come campo addizionale intendeva una funzione, e non nel costruttore

    quindi quando ho un array di puntatori devo sempre fare:

    int A[10];

    int* p=A;

    e poi p++


    mai

    int* A[10]

  6. #6
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Tra i due è int* A[10] l'array di puntatori ( a int in questo caso ).
    Comunque il fatto che sia o no nel costruttore dipendeva dall'altra domanda che avevo fatto. Volendo usare un array a dimensione fissa, è più comodo dichiararlo come ho fatto.
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.