468,539 Members | 1,906 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,539 developers. It's quick & easy.

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

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)

{

}
Jul 22 '05 #1
7 1251
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


Jul 22 '05 #2
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++ :->

Jul 22 '05 #3

"osmium" <r1********@comcast.net> a écrit dans le message de news:
bt************@ID-179017.news.uni-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 ::operator 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
Jul 22 '05 #4

"Piotre Ugrumov" <au************@tin.it> wrote in message
news:d1********************@news4.tin.it...
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&)
Jul 22 '05 #5
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
Jul 22 '05 #6

"Lefteris Laskaridis" <ce*******@germanosnet.gr> a écrit dans le message de
news: bt**********@newsmaster.public.dc.hol.net...
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
Jul 22 '05 #7

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

Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by stephane | last post: by
7 posts views Thread by Alan Bashy | last post: by
4 posts views Thread by Desperate | last post: by
4 posts views Thread by Tarun Mistry | last post: by
3 posts views Thread by ricolee99 | last post: by
7 posts views Thread by gubbachchi | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.