Help me please, how can I create an array of object of a my class?

Discussion in 'C++' started by Piotre Ugrumov, Jan 10, 2004.

  1. I have tried to write the class Student(Studente), Teacher(Docente). This
    classes derive from the class Person.
    In a class university(facoltà). I have tried to create an array of Student
    and an array of Teacher, but the compiler give me an error. How can I do to
    go on?
    Here I have copied the Student, Teacher and University classes.
    Thanks

    Student class:

    #pragma once

    #include "persona.h"

    #include "String.h"

    class Studente :

    public Persona

    {

    public:

    Studente(String, String, String, int, long);

    Studente(Studente &);

    Studente(Persona, long);

    long getMatricola();

    void print();

    ~Studente(void);

    protected:

    const long matricola;

    };



    #include "StdAfx.h"

    #include ".\studente.h"

    #include <iostream>

    using namespace std;

    Studente::Studente(String nome, String cognome, String codfisc, int eta,
    long matr):persona(nome, cognome, codfisc, eta), matricola(matr)

    {

    }

    Studente::Studente(Persona p, long matr):persona(p), matricola(matr){

    }

    long Studente::getMatricola(){

    return matricola;

    }

    Studente::Studente(Studente &s):persona(s.nome, s.cognome, s.codFisc,
    s.eta), matricola(s.matricola){

    }

    void Studente::print(){

    Persona::print();

    cout<<matricola<<endl;

    }

    Studente::~Studente(void)

    {

    }



    Teacher class:

    #include "StdAfx.h"

    #include ".\docente.h"

    #include <iostream>

    using namespace std;

    Docente::Docente(String nome, String cognome, String codfisc, int eta, int
    as, int stipendio, long cod, String laurea, String insegnamento, String
    ruolo):Impiegato(nome, cognome, codfisc, eta, as, stipendio, cod)

    {

    setLaurea(laurea);

    setInsegnamento(insegnamento);

    setRuolo(ruolo);

    }

    Docente::Docente(Impiegato i, String laurea, String insegnamento, String
    ruolo):Impiegato(i){

    setLaurea(laurea);

    setInsegnamento(insegnamento);

    setRuolo(ruolo);

    }

    void Docente::setLaurea(String lau){

    laurea=lau;

    }

    void Docente::setInsegnamento(String ins){

    insegnamento=ins;

    }

    void Docente::setRuolo(String ruo){

    ruolo=ruo;

    }

    String Docente::getLaurea(){

    return laurea;

    }

    String Docente::getInsegnamento(){

    return insegnamento;

    }

    String Docente::getRuolo(){

    return ruolo;

    }

    void Docente::print(){

    Impiegato::print();

    cout<<"Laurea conseguita: ";

    laurea.print();

    cout<<"Insegnamento: ";

    insegnamento.print();

    cout<<"Incarico (titolare o assistente): ";

    ruolo.print();

    }

    Docente::~Docente(void)

    {

    }



    University class:

    #pragma once

    #include "Docente.h"

    #include "Studente.h"

    class Facolta

    {

    public:

    Facolta(String);

    Facolta(int, int, int, String);

    void setStudente(int, Studente);

    void setDocente(int, Docente);

    void setFacolta(String);

    String getFacolta();

    Studente getStudente();

    Docente getDocente();

    ~Facolta(void);

    private:

    String facolta;

    int ns, nd;

    Studente *sPtr;

    Docente *dPtr;

    ;

    };





    #include "StdAfx.h"

    #include ".\facolta.h"

    #include "Studente.h"

    #include "Docente.h"

    #include "Amministrativo.h"

    Facolta::Facolta(String nome)

    {

    setFacolta(nome);

    ns=0;

    nd=0;

    sPtr=NULL;

    dPtr=NULL;

    }

    Facolta::Facolta(int s, int d, String nome){

    setFacolta(nome);

    nd=d;

    ns=s;

    sPtr=new Studente[ns];

    dPtr=new Docente[nd];



    }

    void Facolta::setFacolta(String nome){

    facolta=nome;

    }

    Facolta::~Facolta(void)

    {

    }
    Piotre Ugrumov, Jan 10, 2004
    #1
    1. Advertising


  2. > I have tried to write the class Student(Studente), Teacher(Docente). This
    > classes derive from the class Person.
    > In a class university(facoltà). I have tried to create an array of Student
    > and an array of Teacher, but the compiler give me an error. How can I do

    to
    > go on?


    why not use a vectors?

    vector< Studente > students;
    vector< Docente > teachers;

    -
    Lefteris
    Lefteris Laskaridis, Jan 10, 2004
    #2
    1. Advertising

  3. "osmium" <> a écrit dans le message de news:
    btp48s$a09at$-berlin.de...
    > Piotre Ugrumov writes:
    >
    > > I have tried to write the class Student(Studente), Teacher(Docente).

    This
    > > classes derive from the class Person.
    > > In a class university(facoltà). I have tried to create an array of

    Student
    > > and an array of Teacher, but the compiler give me an error. How can I do

    > to
    > > go on?
    > > Here I have copied the Student, Teacher and University classes.
    > > Thanks
    > >
    > > Student class:
    > >
    > > #pragma once
    > >
    > > #include "persona.h"
    > >
    > > #include "String.h"
    > >
    > > class Studente :
    > >
    > > public Persona
    > >
    > > {
    > >
    > > public:
    > >
    > > Studente(String, String, String, int, long);
    > >
    > > Studente(Studente &);
    > >
    > > Studente(Persona, long);

    >
    > <snip>
    > To create an array of anything there has to be a default constructor.

    When
    > you define the first ctor, the default one is taken away. To get it back
    > you will have to write an explicit deafult ctor. E.g.,
    >
    > Studente() { }
    >
    > Now you just lost the data you wanted. The work around is to introduce an
    > initiate member function. To be robust you should introduce a flag to

    mark
    > objects that are constructed but not initiated.
    >
    > Welcome to the wonderful world of C++ :->
    >
    >


    another solution can be to procede in two steps :
    1) use the ::eek:perator new to allocate some raw memory
    2) loop and call a placement new with the appropriate parameters

    this way, you don't have to provide a default ctor ; anyway, not to be used
    by C++ beginners : the vector solution is still the best in this example
    Le Géant Vert, Jan 10, 2004
    #3
  4. "Piotre Ugrumov" <> wrote in message
    news:d1ULb.87581$...
    > I have tried to write the class Student(Studente), Teacher(Docente). This
    > classes derive from the class Person.
    > In a class university(facoltà). I have tried to create an array of Student
    > and an array of Teacher, but the compiler give me an error. How can I do

    to
    > go on?

    [snip]

    You have no default constructor i.e. one with no args or all defaults
    therefor you cannot create an array because the
    compiler cannot construct the objects.

    You could use vector<Studente> which only requires a copy constructor -
    start empty and push_back students.

    Also your copy constructor shoudl almost certainly take (const Studente&)
    rather than (Studente&)
    Nick Hounsome, Jan 10, 2004
    #4
  5. Piotre Ugrumov

    osmium Guest

    Piotre Ugrumov writes:

    > I have tried to write the class Student(Studente), Teacher(Docente). This
    > classes derive from the class Person.
    > In a class university(facoltà). I have tried to create an array of Student
    > and an array of Teacher, but the compiler give me an error. How can I do

    to
    > go on?
    > Here I have copied the Student, Teacher and University classes.
    > Thanks
    >
    > Student class:
    >
    > #pragma once
    >
    > #include "persona.h"
    >
    > #include "String.h"
    >
    > class Studente :
    >
    > public Persona
    >
    > {
    >
    > public:
    >
    > Studente(String, String, String, int, long);
    >
    > Studente(Studente &);
    >
    > Studente(Persona, long);


    <snip>
    To create an array of anything there has to be a default constructor. When
    you define the first ctor, the default one is taken away. To get it back
    you will have to write an explicit deafult ctor. E.g.,

    Studente() { }

    Now you just lost the data you wanted. The work around is to introduce an
    initiate member function. To be robust you should introduce a flag to mark
    objects that are constructed but not initiated.

    Welcome to the wonderful world of C++ :->
    osmium, Jan 10, 2004
    #5
  6. To dynamically allocate memory:

    Student* students = new Studente[ size ];
    Teachers* teachers = new Docente[ size ];

    To access:
    teachers[ index ] /* pointer/sunsctipt notation */
    or,
    *(teachers + index ) /* pointer/offset notation (no need to do this
    however)*/

    Both do the same thing, namely access the indexed element of the array.

    However, the major drawback is that those structures can't grow (nor shrink)
    according to your program's requirements. This means that once you fill up
    all
    you table positions, you will have to allocate an new larger memory block
    and transfer your data to the newly allocated space. To avoid this, when you
    don't know how many elements you'll have use dynamic data structures (look
    up
    a tutorial on standard template library for this)

    -
    Lefteirs
    Lefteris Laskaridis, Jan 10, 2004
    #6
  7. "Lefteris Laskaridis" <> a écrit dans le message de
    news: btpbd1$ef4$...
    > To dynamically allocate memory:
    >
    > Student* students = new Studente[ size ];
    > Teachers* teachers = new Docente[ size ];
    >
    > To access:
    > teachers[ index ] /* pointer/sunsctipt notation */
    > or,
    > *(teachers + index ) /* pointer/offset notation (no need to do this
    > however)*/
    >
    > Both do the same thing, namely access the indexed element of the array.
    >
    > However, the major drawback is that those structures can't grow (nor

    shrink)
    > according to your program's requirements. This means that once you fill up
    > all
    > you table positions, you will have to allocate an new larger memory block
    > and transfer your data to the newly allocated space. To avoid this, when

    you
    > don't know how many elements you'll have use dynamic data structures (look
    > up
    > a tutorial on standard template library for this)
    >
    > -
    > Lefteirs
    >


    sorry but... what's the point with what I said ? ... Maybe this post wasn't
    intended to be an answer to mine, and then this is ok, but since it appears
    "under" mine, I'd like to understand the link with it, if there's any :-?
    Just 'coz I have the feeling to miss sth
    Le Géant Vert, Jan 10, 2004
    #7
  8. "> sorry but... what's the point with what I said ? ... Maybe this post
    wasn't
    > intended to be an answer to mine, and then this is ok, but since it

    appears
    > "under" mine, I'd like to understand the link with it, if there's any :-?
    > Just 'coz I have the feeling to miss sth


    sorry, it was NOT intended to answer your post! It was my mistake:)
    Lefteris Laskaridis, Jan 11, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,720
    Thomas Weidenfeller
    Oct 12, 2005
  2. KK
    Replies:
    2
    Views:
    528
    Big Brian
    Oct 14, 2003
  3. Replies:
    9
    Views:
    559
    justanotherguy
    Dec 3, 2004
  4. Big Charles
    Replies:
    2
    Views:
    362
    Thomas Hansen
    Feb 2, 2007
  5. jon wayne
    Replies:
    9
    Views:
    697
    Jim Langston
    Sep 22, 2005
Loading...

Share This Page