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

P

Piotre Ugrumov

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)

{

}
 
L

Lefteris Laskaridis

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
 
L

Le Géant Vert

osmium said:
<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
 
N

Nick Hounsome

Piotre Ugrumov said:
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&)
 
O

osmium

Piotre said:
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++ :->
 
L

Lefteris Laskaridis

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
 
L

Le Géant Vert

Lefteris Laskaridis said:
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
 
L

Lefteris Laskaridis

"> 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:)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top