Dev - C++ - Ayuda en POO

 
Vista:
sin imagen de perfil

Ayuda en POO

Publicado por adrian (5 intervenciones) el 08/10/2015 17:40:51
Hola,estoy haciendo un pequeño programa en c++ que consiste en agregar un alimento con su nombre,calorias en un listbox nada mas, pero el programa no funciona,estoy buscando el "error" pero no se donde esta mal.
Tengo 2 clases: Uno de Alimentos y el otro de Arreglo_Alimentos.
(Esta es la clase Alimentos)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<string.h>
#include<stdio.h>
class Alimentos
{
private:
	char *Nombre;
	int calorias;
public:
	Alimentos();
	~Alimentos();
	char *get_Nombre();
	int get_calorias();
	void set_Nombre(char *valor);
	void set_calorias(int valor);
 
};

(Esta es del Arreglo)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "Alimentos.h"
class Arreglo_Alimentos
{
private:
	Alimentos **Arreglo;
	int *n;
public:
	Arreglo_Alimentos();
	~Arreglo_Alimentos();
	int get_n();
	Alimentos**Crear_arreglo();
	Alimentos *get_dato_arreglo(int pos);
	void agregar(Alimentos *dato);
 
};

(Esta es del MyForm.h ya que estoy utilizando visual stdudio)

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
#include"Arreglo_Alimentos.h"
 
namespace comprobando {
 
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
 
	/// <summary>
	/// Summary for MyForm
	/// </summary>
	public ref class MyForm : public System::Windows::Forms::Form
	{
	public:
		MyForm(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			obj_arreglo = new Arreglo_Alimentos;
			//
		}
 
	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~MyForm()
		{
			if (components)
			{
				delete components;
			}
		}
 
	private:
		/// <summary>
		/// Required designer variable.
		Arreglo_Alimentos * obj_arreglo;
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::TextBox^  textBox2;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::TextBox^  textBox3;
	private: System::Windows::Forms::Button^  btnagregar;
	private: System::Windows::Forms::ListBox^  listBox1;
			 /// </summary>
		System::ComponentModel::Container ^components;
 
#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->textBox3 = (gcnew System::Windows::Forms::TextBox());
			this->btnagregar = (gcnew System::Windows::Forms::Button());
			this->listBox1 = (gcnew System::Windows::Forms::ListBox());
			this->SuspendLayout();
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(165, 49);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(148, 20);
			this->textBox1->TabIndex = 0;
			// 
			// textBox2
			// 
			this->textBox2->Location = System::Drawing::Point(165, 99);
			this->textBox2->Name = L"textBox2";
			this->textBox2->Size = System::Drawing::Size(148, 20);
			this->textBox2->TabIndex = 1;
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(100, 56);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(47, 13);
			this->label1->TabIndex = 2;
			this->label1->Text = L"Nombre:";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(100, 106);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(47, 13);
			this->label2->TabIndex = 3;
			this->label2->Text = L"Calorias:";
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(100, 156);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(31, 13);
			this->label3->TabIndex = 4;
			this->label3->Text = L"Tipo:";
			// 
			// textBox3
			// 
			this->textBox3->Location = System::Drawing::Point(165, 149);
			this->textBox3->Name = L"textBox3";
			this->textBox3->Size = System::Drawing::Size(148, 20);
			this->textBox3->TabIndex = 5;
			// 
			// btnagregar
			// 
			this->btnagregar->Location = System::Drawing::Point(165, 195);
			this->btnagregar->Name = L"btnagregar";
			this->btnagregar->Size = System::Drawing::Size(75, 23);
			this->btnagregar->TabIndex = 6;
			this->btnagregar->Text = L"agregar";
			this->btnagregar->UseVisualStyleBackColor = true;
			this->btnagregar->Click += gcnew System::EventHandler(this, &MyForm::btnagregar_Click);
			// 
			// listBox1
			// 
			this->listBox1->FormattingEnabled = true;
			this->listBox1->Location = System::Drawing::Point(335, 49);
			this->listBox1->Name = L"listBox1";
			this->listBox1->Size = System::Drawing::Size(430, 173);
			this->listBox1->TabIndex = 7;
			// 
			// MyForm
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(800, 339);
			this->Controls->Add(this->listBox1);
			this->Controls->Add(this->btnagregar);
			this->Controls->Add(this->textBox3);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->textBox2);
			this->Controls->Add(this->textBox1);
			this->Name = L"MyForm";
			this->Text = L"MyForm";
			this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
			this->ResumeLayout(false);
			this->PerformLayout();
 
		}
#pragma endregion
		void Insertar_Listbox()
		{
			listBox1->Items->Clear();
 
			Alimentos*aux;
			for (int i = 0; i < obj_arreglo->get_n(); i++)
			{
				aux = obj_arreglo->get_dato_arreglo(i);
 
				listBox1->Items->Add("Nombre: " + gcnew String(aux->get_Nombre()) +
					"Calorias: " + aux->get_calorias()
					);
			}
 
		}
	private: System::Void btnagregar_Click(System::Object^  sender, System::EventArgs^  e)
	{
				 if (textBox1->Text->Length>0 && textBox2->Text->Length>0 && textBox3->Text->Length>0)
				 {
					 Alimentos*aux = new Alimentos();
					 char *minombre = new char[20];
					 sprintf(minombre, "%s", textBox1->Text);
					 aux->set_Nombre(minombre);
					 aux->set_calorias(Convert::ToInt32(textBox2->Text));
					 //aux->set_tipo(Convert::ToInt32(textBox3->Text));
 
					 obj_arreglo->agregar(aux);
					 Insertar_Listbox();
				 }
	}




Al ejecutar no me muestra errores, pero no funciona,espero una ayuda con el código.
Gracias de antemano.Hola,estoy haciendo un pequeño programa en c++ que consiste en agregar un alimento con su nombre,calorias en un listbox nada mas, pero el programa no funciona,estoy buscando el "error" pero no se donde esta mal.
Tengo 2 clases: Uno de Alimentos y el otro de Arreglo_Alimentos.
(Esta es la clase Alimentos)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<string.h>
#include<stdio.h>
class Alimentos
{
private:
	char *Nombre;
	int calorias;
public:
	Alimentos();
	~Alimentos();
	char *get_Nombre();
	int get_calorias();
	void set_Nombre(char *valor);
	void set_calorias(int valor);
 
};

(Esta es del Arreglo)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "Alimentos.h"
class Arreglo_Alimentos
{
private:
	Alimentos **Arreglo;
	int *n;
public:
	Arreglo_Alimentos();
	~Arreglo_Alimentos();
	int get_n();
	Alimentos**Crear_arreglo();
	Alimentos *get_dato_arreglo(int pos);
	void agregar(Alimentos *dato);
 
};

(Esta es del MyForm.h ya que estoy utilizando visual stdudio)

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
#include"Arreglo_Alimentos.h"
 
namespace comprobando {
 
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
 
	/// <summary>
	/// Summary for MyForm
	/// </summary>
	public ref class MyForm : public System::Windows::Forms::Form
	{
	public:
		MyForm(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			obj_arreglo = new Arreglo_Alimentos;
			//
		}
 
	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~MyForm()
		{
			if (components)
			{
				delete components;
			}
		}
 
	private:
		/// <summary>
		/// Required designer variable.
		Arreglo_Alimentos * obj_arreglo;
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::TextBox^  textBox2;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::TextBox^  textBox3;
	private: System::Windows::Forms::Button^  btnagregar;
	private: System::Windows::Forms::ListBox^  listBox1;
			 /// </summary>
		System::ComponentModel::Container ^components;
 
#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->textBox3 = (gcnew System::Windows::Forms::TextBox());
			this->btnagregar = (gcnew System::Windows::Forms::Button());
			this->listBox1 = (gcnew System::Windows::Forms::ListBox());
			this->SuspendLayout();
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(165, 49);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(148, 20);
			this->textBox1->TabIndex = 0;
			// 
			// textBox2
			// 
			this->textBox2->Location = System::Drawing::Point(165, 99);
			this->textBox2->Name = L"textBox2";
			this->textBox2->Size = System::Drawing::Size(148, 20);
			this->textBox2->TabIndex = 1;
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(100, 56);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(47, 13);
			this->label1->TabIndex = 2;
			this->label1->Text = L"Nombre:";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(100, 106);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(47, 13);
			this->label2->TabIndex = 3;
			this->label2->Text = L"Calorias:";
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(100, 156);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(31, 13);
			this->label3->TabIndex = 4;
			this->label3->Text = L"Tipo:";
			// 
			// textBox3
			// 
			this->textBox3->Location = System::Drawing::Point(165, 149);
			this->textBox3->Name = L"textBox3";
			this->textBox3->Size = System::Drawing::Size(148, 20);
			this->textBox3->TabIndex = 5;
			// 
			// btnagregar
			// 
			this->btnagregar->Location = System::Drawing::Point(165, 195);
			this->btnagregar->Name = L"btnagregar";
			this->btnagregar->Size = System::Drawing::Size(75, 23);
			this->btnagregar->TabIndex = 6;
			this->btnagregar->Text = L"agregar";
			this->btnagregar->UseVisualStyleBackColor = true;
			this->btnagregar->Click += gcnew System::EventHandler(this, &MyForm::btnagregar_Click);
			// 
			// listBox1
			// 
			this->listBox1->FormattingEnabled = true;
			this->listBox1->Location = System::Drawing::Point(335, 49);
			this->listBox1->Name = L"listBox1";
			this->listBox1->Size = System::Drawing::Size(430, 173);
			this->listBox1->TabIndex = 7;
			// 
			// MyForm
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(800, 339);
			this->Controls->Add(this->listBox1);
			this->Controls->Add(this->btnagregar);
			this->Controls->Add(this->textBox3);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->textBox2);
			this->Controls->Add(this->textBox1);
			this->Name = L"MyForm";
			this->Text = L"MyForm";
			this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
			this->ResumeLayout(false);
			this->PerformLayout();
 
		}
#pragma endregion
		void Insertar_Listbox()
		{
			listBox1->Items->Clear();
 
			Alimentos*aux;
			for (int i = 0; i < obj_arreglo->get_n(); i++)
			{
				aux = obj_arreglo->get_dato_arreglo(i);
 
				listBox1->Items->Add("Nombre: " + gcnew String(aux->get_Nombre()) +
					"Calorias: " + aux->get_calorias()
					);
			}
 
		}
	private: System::Void btnagregar_Click(System::Object^  sender, System::EventArgs^  e)
	{
				 if (textBox1->Text->Length>0 && textBox2->Text->Length>0 && textBox3->Text->Length>0)
				 {
					 Alimentos*aux = new Alimentos();
					 char *minombre = new char[20];
					 sprintf(minombre, "%s", textBox1->Text);
					 aux->set_Nombre(minombre);
					 aux->set_calorias(Convert::ToInt32(textBox2->Text));
					 //aux->set_tipo(Convert::ToInt32(textBox3->Text));
 
					 obj_arreglo->agregar(aux);
					 Insertar_Listbox();
				 }
	}



Aqui una imagen de como es

Sin-titulo-2
Al ejecutar no me muestra errores, pero no funciona,espero una ayuda con el código.
Tambien adjunto el codigo.
Gracias de antemano.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder