473,654 Members | 3,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

I have tried to write the class Student(Student e), Teacher(Docente ). This
classes derive from the class Person.
In a class university(faco ltà). 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(Studen te &);

Studente(Person a, long);

long getMatricola();

void print();

~Studente(void) ;

protected:

const long matricola;

};

#include "StdAfx.h"

#include ".\studente .h"

#include <iostream>

using namespace std;

Studente::Stude nte(String nome, String cognome, String codfisc, int eta,
long matr):Persona(n ome, cognome, codfisc, eta), matricola(matr)

{

}

Studente::Stude nte(Persona p, long matr):Persona(p ), matricola(matr) {

}

long Studente::getMa tricola(){

return matricola;

}

Studente::Stude nte(Studente &s):Persona(s.n ome, s.cognome, s.codFisc,
s.eta), matricola(s.mat ricola){

}

void Studente::print (){

Persona::print( );

cout<<matricola <<endl;

}

Studente::~Stud ente(void)

{

}

Teacher class:

#include "StdAfx.h"

#include ".\docente. h"

#include <iostream>

using namespace std;

Docente::Docent e(String nome, String cognome, String codfisc, int eta, int
as, int stipendio, long cod, String laurea, String insegnamento, String
ruolo):Impiegat o(nome, cognome, codfisc, eta, as, stipendio, cod)

{

setLaurea(laure a);

setInsegnamento (insegnamento);

setRuolo(ruolo) ;

}

Docente::Docent e(Impiegato i, String laurea, String insegnamento, String
ruolo):Impiegat o(i){

setLaurea(laure a);

setInsegnamento (insegnamento);

setRuolo(ruolo) ;

}

void Docente::setLau rea(String lau){

laurea=lau;

}

void Docente::setIns egnamento(Strin g ins){

insegnamento=in s;

}

void Docente::setRuo lo(String ruo){

ruolo=ruo;

}

String Docente::getLau rea(){

return laurea;

}

String Docente::getIns egnamento(){

return insegnamento;

}

String Docente::getRuo lo(){

return ruolo;

}

void Docente::print( ){

Impiegato::prin t();

cout<<"Laurea conseguita: ";

laurea.print();

cout<<"Insegnam ento: ";

insegnamento.pr int();

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

ruolo.print();

}

Docente::~Docen te(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(Stri ng);

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::Facolt a(String nome)

{

setFacolta(nome );

ns=0;

nd=0;

sPtr=NULL;

dPtr=NULL;

}

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

setFacolta(nome );

nd=d;

ns=s;

sPtr=new Studente[ns];

dPtr=new Docente[nd];

}

void Facolta::setFac olta(String nome){

facolta=nome;

}

Facolta::~Facol ta(void)

{

}
Jul 22 '05 #1
7 1468
I have tried to write the class Student(Student e), Teacher(Docente ). This
classes derive from the class Person.
In a class university(faco ltà). 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(Student e), Teacher(Docente ). This
classes derive from the class Person.
In a class university(faco ltà). 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(Studen te &);

Studente(Person a, 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********@com cast.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(Student e), Teacher(Docente ). This classes derive from the class Person.
In a class university(faco ltà). 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(Studen te &);

Studente(Person a, 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******** ************@ne ws4.tin.it...
I have tried to write the class Student(Student e), Teacher(Docente ). This
classes derive from the class Person.
In a class university(faco ltà). 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*******@germ anosnet.gr> a écrit dans le message de
news: bt**********@ne wsmaster.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
1184
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? There is no code to write just general questions about the two class'. the code: /--
7
2382
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 me. This was inspired by Exercise 7 and Programming Problem 8 in Chapter 3 of our text. I have done Exercise 7 for you: Below you will find the ADT specification for a string of characters. It represents slightly more that a minimal string...
7
3594
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> </head> <style type="text/css">
4
366
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 public int color = 0 public int background = 0
4
2426
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 application I will be making, also my first .NET application, so im looking for any help and guidance. Ok, my problems are todo with object and database abstraction, what should i do.
13
2137
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 will fall somewhere between a Start & End time Further more, there will be Break & Lunch times between Start & End. Example... Start 08:00 Break start 10:30
3
2086
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, "Mapper.cs" that's supposed to map objects from any type of dataset to any type of object. Inside Mapper.cs, there's a method, object Map(Type typeOfObjectToMap, DataSet theDataSet)
0
5557
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
2
1649
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 oMyCar(0).Wheels.NumberOfWheels oMyCar(0).Wheels.ColorOfWheels
7
2361
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 date from calender and clicks search button it will fetch data from database. But what I need is as soon the user clicks the date, it should fetch data for that date from database. How to do it. Here is the code I am using <html> <script...
0
8596
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7309
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2719
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1597
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.