473,394 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 1441
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: stephane | last post by:
I am preparing an exam and I have a copy of last year exam. I answered the first questions and I wander if I am right or wrong. Can someone have a look at the questions and at my answers please?...
7
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
4
by: Desperate | last post by:
I want to create class Matrix which contains array of class Rows and some additions Class Rows contains array of Cells Here is the declaration public class Cell public int value = 0...
4
by: Tarun Mistry | last post by:
Hi all, I have posted this in both the c# and asp.net groups as it applies to both (apologies if it breaks some group rules). I am making a web app in asp.net using c#. This is the first fully OO...
13
by: sd00 | last post by:
Hi all, can someone give me some coding help with a problem that *should* be really simple, yet I'm struggling with. I need the difference between 2 times (Target / Actual) However, these times...
3
by: ricolee99 | last post by:
Hi everyone, I have a problem that i have been trying to solve for awhile. I'm given a code where the purpose is to create a general dataset mapper. Given any dataset, i have a class,...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
2
by: Big Charles | last post by:
Hello, I would like to create an array-class to be able to call like: Dim oMyCar as New MyCar ' After initializing oMyCar, the object has to be like: oMyCar(0).Brand...
7
by: gubbachchi | last post by:
Hi all, In my application I need to display the data fetched from mysql database after the user selects date from javascript calender. I have written the code in which after the user selects the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.