Ajuda c++ class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
						Ficha 2:

Exercício 1)

		Alínea a)

#include <iostream>

#include <stdlib.h>

using namespace std;



int main(int argc, char* argv[]) {

	int x;

	x = atoi(argv[1]);

	for (int i=0; i<x; i++){	

	cout  << "Hello, C++ world!" << endl;

	}

	return 0;
}


	Alínea b)

#include <iostream>
using namespace std;

int main() {

	int x;

	cout << "Insira numero vezes que quer ver a frase no ecran" << endl;
	cin >> x;

	for (int i=0; i<x; i++){	

	cout  << "Hello, C++ world!" << endl;

	}

	return 0;

}


	Alínea c)

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

int main(int argc, char* argv[]) {

	int x;
	string frase;

	cout << "Escreva a frase que pretende" << endl;
	getline(cin,frase);

	x = atoi(argv[1]);

	cout << "+------------------------------------+" << endl;

	for (int i=0; i<x; i++){	

	cout  << frase << endl;

	}

	return 0;

}

	Alínea d)

criação nova file de nome makefile

 - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Makefile example with vars

#

ola: ficha2_ex1.cpp

	g++ ficha2_ex1.cpp -Wall -o ola


- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
shell >> make ola








					Ficha 5

animal.cpp:

#include "animal.h"
#include <string>

Veterinario::Veterinario(string nom, string espec) : nome(nom){  //construtor normal
	especialidade = espec;
};


Veterinario::Veterinario () : nome(""){   //construtor vazio
	especialidade= "" ;
};


//Operadores
Veterinario & Veterinario::operator=(Veterinario &vet){ //operador unitario de Veterinario
	return *this;			//necessario para TODAS as funcoes que retornem um Veterinario
};


Animal::Animal(string esp, bool sex, char * nom, int id) : especie(esp), sexo(sex), nome(nom){
	idade = id;  //construtor sem vet
};


Animal::Animal(string esp, bool sex, char * nom, int id, Veterinario vet) : especie(esp), sexo(sex), nome(nom), veterinario(vet){
	idade = id;  //construtor com vet
};


Animal::Animal(Animal &a): especie(a.getEspecie()), sexo(a.getSexo()), nome(a.getNome()), veterinario(a.getVeterinario()){  //construtor de copia
	idade = (a.getIdade());
};


Animal::~Animal(){};   //destructor


const string Animal::getEspecie() const{   //retorna a especie do Animal
	return especie;
};


const bool Animal::getSexo() const{   //retorna o sexo do Animal
	return sexo;
};


const char* Animal::getNome() const{   //retorna o nome do Animal
	return nome;
};


int Animal::getIdade() const {  //retorna a idade do Animal
	return idade;
};


Veterinario Animal::getVeterinario() const{   //retorna o Veterinario do Animal
	return veterinario;
};


const string Veterinario::getNome() const{   //retorna a string do nome do Veterinário
	return nome;
};


string Veterinario::getEspecialidade() const{   //retorna a string da especialidade do Veterinário
	return especialidade;
};


void Animal::setIdade(int id){   //altera o inteiro de idade em Animal para o Veterinario recebido por vet
	idade = id;
};


void Animal::setVeterinario(Veterinario vet) {  //altera o valor de veterinario em Animal para o Veterinario recebido por vet
	veterinario = vet;
};


///Operadores
ostream & operator<<(ostream &o, Animal &a){  //sobrecarga de operadores

	o << "Animal:" << a.getEspecie() << "," << a.getSexo() << "," << a.getNome() << "," << a.getIdade() << "; Responsável: " << a.getVeterinario().getNome() << endl;

	return o;
};


bool Animal::operator<(const Animal &a){  //operador

	if (idade<a.getIdade()) return 1;

	else return 0;     

};


Animal & Animal::operator=(const Animal &a){    //operador unitário
	return *this;
};
_________________________________________________________________________

animal.h (header):

#include <iostream>
#include <string>
using namespace std;


class Veterinario {

		const string nome;
		string especialidade;
	public:
		Veterinario(string nom, string espec);
		Veterinario();
		const string getNome() const;
		string getEspecialidade() const;
		Veterinario & operator=(Veterinario &vet);

};  //nota: as funcoes em public tem SEMPRE Veterinario::


class Animal {
		const string especie;	// urso, girafa, etc
		const bool sexo;	// macho: 1; fêmea: 0;
		const char *nome;	// atenção à alocação de espaço!
		int idade;	// em anos
		Veterinario veterinario;	// outra classe!
	public:
		Animal(string esp, bool sex, char *nom, int id);
		Animal(string esp, bool sex, char *nom, int id, Veterinario vet);
		Animal(Animal &a);
		~Animal();
		const string getEspecie() const;
		const bool getSexo() const;
		const char * getNome() const;
		int getIdade() const;
		Veterinario getVeterinario() const;
		void setIdade(int id);
		void setVeterinario(Veterinario vet);
		bool operator<(const Animal &a);
		Animal & operator=(const Animal &a);

}; //nota: as funcoes em public tem SEMPRE Animal::


ostream & operator<<(ostream &o, Animal &a);  //fora das classes nao necessita de operando (ex: Animal, Veterinario) 

What exactly is the error?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

#include "animal.h"

using namespace std;

/**
*****************************************VETERINARIO***************************
*/
///Constructores
Veterinario::Veterinario(string nom, string esp): nome(nom){	//este nome(nom) e equivalente a fazer nome = nom so que agora tratando-se de um const; isto so funciona em constructores (esta explicado diretinho nos acetatos)
	especialidade = esp;
}

Veterinario::Veterinario() : nome(""){
	especialidade = "";
}
///get's e set's
//os get's sao necessarios porque os membros sao private (default)
const string Veterinario::getNome() const{
	return nome;
}

string Veterinario::getEspecialidade() const{
	return especialidade;
}

void Veterinario::setEspecialidade(string esp){
	especialidade = esp;
}

//Operadores
Veterinario & Veterinario::operator=(Veterinario &vet){
	return vet;
}


/**
*****************************************ANIMAL***************************
*/
///Constructores e destructor
Animal::Animal(const string esp, const bool sex, char * nom, int id) : especie(esp), sexo(sex), nome(nom), veterinario(Veterinario()){
	idade = id;
}

Animal::Animal(string esp, const bool sex, char * nom, int id, Veterinario vet) : especie(esp), sexo(sex), nome(nom), veterinario(vet){
	idade = id;
}

Animal::Animal(Animal &a): especie(a.especie), sexo(a.sexo), nome(a.nome), veterinario(a.veterinario) {
	idade = a.idade;
}

Animal::~Animal(){}

///get's e set's
const string Animal::getEspecie() const{
	return especie;
}

const bool Animal::getSexo() const{
	return sexo;
}

const char* Animal::getNome() const{
	return nome;
}

 int Animal::getIdade() const{
	return idade;
}

Veterinario Animal::getVeterinario() const{
	return veterinario;
}

void Animal::setIdade(int id){
	idade = id;
}

void Animal::setVeterinario(Veterinario vet){
	veterinario = vet;
}

///Operadores
ostream & operator<<(ostream &o, Animal &a){
	o << "Animal: " << a.getEspecie() << ", " << a.getSexo() << ", " << a.getNome() << ", " << a.getIdade() << "; Responsável: " << a.getVeterinario().getNome() << endl;

	return o;
}

bool Animal::operator<(const Animal &a){
	return idade < a.idade;
}

Animal & Animal::operator=(const Animal &a){
	return *this;
}

/*
int main(){

	//alinea a)
	Veterinario vet = Veterinario();
	cout << "vet: " << vet.getEspecialidade() << "; " << vet.getNome() << ";" << endl;

	Veterinario vet2 = Veterinario("paulo", "caes");

	cout << "esp = " << vet2.getEspecialidade() << "; " << vet2.getNome() << ";" << endl;

	Veterinario vet3 = Veterinario("Fernando", "gatos");

	cout << "esp = " << vet3.getEspecialidade() << "; " << vet3.getNome() << ";" << endl;

	//alinea b)
	
	string nome = "bobby";
	Animal a1("cao", true, (char*)nome.c_str(), 2, vet2);
	Animal a2("cao", true, (char*)nome.c_str(), 2);
	//cout << a1.getEspecie() << "; " << a1.getSexo() << "; " << a1.getNome() << "; " << a1.getIdade() << "; " << a1.getVeterinario().getNome() << ": " << a1.getVeterinario().getEspecialidade() << ";\n";

	Animal a3(a1);

	a2.setIdade(5);

	cout << a1;
	cout << a2;
	cout << a3;

	if(a1 < a2)
		cout << "funcou\n";
	else
		cout << "nao funcou\n";

	return 0;
}*/

________________________________________________________________________________________________

#include <iostream>
#include <string.h>

using namespace std;

class Veterinario {
		const string nome;
		string especialidade;
	public:
		Veterinario(string nom, string esp);
		Veterinario();
		const string getNome() const;
		string getEspecialidade() const;
		void setEspecialidade(string esp);
		Veterinario & operator=(Veterinario &vet);
};

class Animal {
		const string especie;	// urso, girafa, etc
		const bool sexo;	// macho: 1; fêmea: 0;nome
		const char *nome;	// atenção à alocação de espaço!
		int idade;	// em anos
		Veterinario veterinario;	// outra classe!
	public:
		const string getEspecie() const;
		const bool getSexo() const;
		const char* getNome() const;
		int getIdade() const;
		Veterinario getVeterinario() const;

		Animal(string esp, const bool sex, char * nom, int id);
		Animal(string esp, bool sex, char * nom, int id, Veterinario vet);
		Animal();
		~Animal();
		void setIdade(int id);
		void setVeterinario(Veterinario vet);
		bool operator<(const Animal &a);
		Animal & operator=(const Animal &a);
		Animal(Animal &a);
};
ostream & operator<<(ostream &o, Animal &a);


Sorry, I forgot the next part.

What do you think is the best code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <sstream>	//para poder utilizar o istringstream

using namespace std;

class Aluno{
	private:
	string nome;
	string curso;
	int number;
	int media;

	public:
	//alinea a)
	Aluno(string nom){		//pode ser feito desta maneira
		nome = nom;
		curso = "";
		number = 0;
		media = 0;
	}

	Aluno(string nom, string cur, int num); //ou desta com a definicao da funcao mais tarde

	//alinea b)
	int getMedia();
	int getNumber();
	string getNome();
	string getCurso();
	void setCurso(string cur);
	void setNumber(int num);
	void setMedia(int med);	

	//alinea c)
	void imprimirAluno(ostream & os);

	//alinea d)
	string sigla();

};		//atencao ao ponto e virgula
//alinea a)
Aluno::Aluno(string nom, string cur, int num){	//Definicao da funcao declarada acima
	nome = nom;
	curso = cur;
	number = num;
	media = 0;
}
//alinea b)
int Aluno::getMedia(){
	return media;
}

int Aluno::getNumber(){
	return number;
}

string Aluno::getNome(){
	return nome;
}

string Aluno::getCurso(){
	return curso;
}

void Aluno::setCurso(string cur){
	curso = cur;
}

void Aluno::setNumber(int num){
	number = num;
}

void Aluno::setMedia(int med){
	media = med;
}
//alinea c)
void Aluno::imprimirAluno(ostream & os){
	cout << nome << " | " << curso << " | " << number << " | " <<  media << endl;
}

//alinea d)
string Aluno::sigla(){
	string nm = nome, nm2;
	string sigla;
	
//ha outras maneiras de fazer isto. esta e a que me parece mais imediata para mim mas aconselho-vos a procurarem alternativas :)
	istringstream iss(nm, istringstream::in);
	while(!iss.eof()) {
		iss >> nm2;
		sigla.push_back(nm2[0]);

		nm2.clear();
	}
	return sigla;
}

int main(){
	//testa a)
	Aluno a1 = Aluno("pedro antunes rocha");
	Aluno a2 = Aluno("maria jose rodrigues santos", "mieic", 123);

	//testa b)
	cout << a1.getNome() << "\t" << a1.getCurso() << "\t" << a1.getNumber() << "\t" << a1.getMedia() << endl;
	cout << a2.getNome() << "\t" << a2.getCurso() << "\t" << a2.getNumber() << "\t" << a2.getMedia() << endl;

	a1.setNumber(145);
	a1.setCurso("MIEEC");
	a1.setMedia(12);

	cout << "apos as alteracoes:\n";

	//cout << a1.getNome() << "\t" << a1.getCurso() << "\t" << a1.getNumber() << "\t" << a1.getMedia() << endl;

	//testa c)
	a1.imprimirAluno(cout);
	
	//testa a d)
	cout << a1.sigla() << endl << a2.sigla() << endl;

	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

5a -f2:

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char* argv[]){
	
	if(argc < 2){		//tem de ter pelo menos o nome do programa e um valor
		cout << "atencao ao numero de argumentos\n";
		return -1;
	}

	int soma = 0;

	for(int i = 1; i < argc; i++)
		soma += atoi(argv[i]);

	cout << "a media e " << (float)soma/(float)(argc-1) << endl;

	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

f2-5b:

#include <iostream>
#include <stdlib.h>
#include <string.h>

using namespace std;

float med(int valores[], int n){
	int soma = 0;

	for(int i = 0; i < n; i++)
		soma += valores[i];

	return (float)soma/(float)(n);
}

int max(int valores[], int n){
	int max = valores[0];

	for(int i = 1; i < n; i++)
		if(valores[i] > max)
			max = valores[i];

	return max;
}

int min(int valores[], int n){
	int min = valores[0];

	for(int i = 1; i < n; i++)
		if(valores[i] < min)
			min = valores[i];

	return min;
}

int gam(int valores[], int n){
	return max(valores, n) - min(valores, n);
}

int main(int argc, char* argv[]){

	if(argc < 3){		//tem de ter pelo menos o nome do programa, o nome da operacao e um valor
		cout << "atencao ao numero de argumentos\n";
		return -1;
	}
	
	string comando;
	int n = argc-2, valores[n], tam = strlen(argv[1]);

	for(int i = 0; i < tam; i++)
		comando.push_back(tolower(argv[1][i]));

	for(int i = 0; i < argc-2; i++)
		valores[i] = atoi(argv[i+2]);

	float resultado;

	if(comando.compare("max") == 0)
		resultado = max(valores, n);

	else if(comando.compare("min") == 0)
		resultado = min(valores, n);

	else if(comando.compare("gam") == 0)
		resultado = gam(valores, n);

	else if(comando.compare("med") == 0)
		resultado = med(valores, n);

	else{
		cout << "nome de operacao nao aceite\n";
		return -1;
	}
		
	cout << "o " << comando  << " e " << resultado << endl;

	return 0;
}
Please give me your comments about the code. I'm trying to make it as fast as I can. :x
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Considere a classe Bilhete usada num programa de reserva de lugares
// para um determinado espectáculo. Esta classe inclui informação
// sobre o titulo do espectáculo a que o bilhete se destina, a fila e
// o lugar da sala que este bilhete reserva.

// Uma implementação incompleta da classe Bilhete é apresentada a seguir:
#include <iostream>
#include <string>
using namespace std;

class  Bilhete{
 	string espectaculo;
 	int fila;
 	int lugar;
 public:
 	Bilhete(string espect, int fil, int lug);
         // INSIRA o seu codigo desde AQUI
	string getEspectaculo() const;	// a definir
	bool filaPar(int fila) const;
        // ATE AQUI
};

// a) Implemente o construtor da classe Bilhete, completando a seguinte função:

Bilhete::Bilhete(string espect, int fil, int lug) {
        // INSIRA o seu codigo desde AQUI
 	espectaculo = espect;
 	fila = fil;
 	lugar = lug;
       // ATE AQUI
}

// b) Implemente um membro-função que retorna o titulo do espectaculo a que o 
//     bilhete se refere, completando a seguinte função:
string Bilhete::getEspectaculo() const {
       // INSIRA o seu codigo desde AQUI
	return (espectaculo);
      // ATE AQUI
}


// c) Implemente um membro-função que determina se a fila da sala
//    correspondente ao lugar a que o bilhete se refere é par ou não,
//     completando a seguinte função:
bool Bilhete::filaPar(int fila) const {
     // INSIRA o seu codigo desde AQUI
     return ((fila % 2) == 0);
    // ATE AQUI
}
//   Esta função retorna true se a fila é um número par, e false no caso 
//   contrário.


// Nota: não esqueça que deve incluir a declaração de todos os
// membros-função na declaração da classe Bilhete.
Topic archived. No new replies allowed.