Здравствуйте, помогите пожалуйста. Стоит задача показать все принципы ООП и основные отношения между класами. Сильно не ругайтесь, только учась, насчет инициализации масивов, знаю что можно считать с файла, но так пока проще.
В чем суть программы: классификация животных. У меня есть Клас Animal, который вроде абстрактный, он имеет два дочерних класса:Wild и Domestic. В свою очередь Wild агрегирует в себе класс Health, а класс Health использует класс Level. В чем суть программы: пользователь вводит к примеру имя животного и ему выдаеться информация о животном тоесть Имя, размер души и , в зависимости от того какое животное ищет пользователь, ему может вывестись цена(у домашних) или урон и здоровье(у диких животных). Так вот проблема в агрегировании, я не могу получить доступ к массиву, в котором храняться значения здоровья. Не работает функция hp.getelement(hp,i)/*в самом конце кода*/Может быть проблема в конструкторах, но я не вижу где конкретно, помогите пожалуйста. Да и еще, как можно сделать размер массива name общим для всех классов, ведь это число я использую в каждом конструкторе, но как его обобщить не знаю.
Заголовочный файл:
C++ |
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
| #include <iostream>
#include <string>
using namespace std;
class Animal
{
protected:
string* name;
string* soul;
string* id;
public:
Animal();
Animal(const Animal& s);
~Animal();
virtual int search(string Name)=0;
virtual void output(int i)=0;
};
class Level
{
private:
int lvl;
public:
Level();
~Level(){};
Level(Level& s);
void input(Level& s);
int get(){return lvl;};
};
class Health
{
private:
string *health;
public:
Health();
~Health();
Health(Health& s);
void value(Level& s);
string getelement(Health&h,int i);
Health& operator =(const Health& s);
//void outpat(int i);
};
class Wild:public Animal
{
private:
int *damage;
Health hp;
public:
void add();
Wild();
~Wild(){};
Wild(Wild& s);
string get(Health hp, int i);
int search(string Name){return 5;};
void output( int i);
};
class Domestic: public Animal
{
private:
int *arrPrise;
public:
Domestic();
Domestic(const Domestic& s);
~Domestic();
int search(string Name);
void output(int i);
}; |
|
Файл функций:
C++ |
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
| #include "Header.h"
Animal::Animal()
{
int n=15;
name=new string[n];
name[0]="Bear";name[1]="Sabre Cat";name[2]="Skeever";name[3]="Slaughterfish";name[4]="Wolf";name[5]="Dog";name[6]="Horker";
name[7]="Mammoth";name[8]="Mudcrab";name[9]="Chicken";name[10]="Cow";name[11]="Fox";name[12]="Deer";name[13]="Goat";name[14]="Rabbit";
soul=new string[n];
soul[0]="Lesser";soul[1]="Lesser";soul[2]="Petty";soul[3]="Petty";soul[4]="Petty";soul[5]="Petty";soul[6]="Petty";
soul[7]="Grand";soul[8]="Petty";soul[9]="Petty";soul[10]="Petty";soul[11]="Petty";soul[12]="Lesser";soul[13]="Petty";soul[14]="Petty";
id=new string[n];
id[0]="00023a8a";id[1]="00023ab5";id[2]="00023ab7";id[3]="00023ab8";id[4]="00023abe";id[5]="00023a92";id[6]="00023ab1";
id[7]="00023ab4";id[8]="000e4010";id[9]="000a91a0";id[10]="00023a90";id[11]="000829b3";id[12]="000abee0";id[13]="0004359c";id[14]="0006dc9d";
}
Animal::Animal(const Animal& s)
{
int m=15;
name=new string[m];
for(int i=0; i<m; i++) name[i]=s.name[i];
soul=new string[m];
for(int i=0; i<m; i++) soul[i]=s.soul[i];
id=new string[m];
for(int i=0; i<m; i++) id[i]=s.id[i];
}
Animal::~Animal()
{
delete [] name;
delete [] soul;
delete [] id;
}
void Animal::output(int i)
{
cout<<"1."<<"Name:"<<name[i]<<endl;
cout<<"2."<<"Soul:"<<soul[i]<<endl;
cout<<"3."<<"id: "<<id[i]<<endl;
}
Domestic::Domestic()
{
int n=15;
arrPrise=new int[n];
arrPrise[0]=0;arrPrise[1]=0;arrPrise[2]=0;arrPrise[3]=0;arrPrise[4]=0;arrPrise[5]=125;arrPrise[6]=364;
arrPrise[7]=0;arrPrise[8]=0;arrPrise[9]=658;arrPrise[10]=2684;arrPrise[11]=0;arrPrise[12]=0;arrPrise[13]=0;arrPrise[14]=166;
}
Domestic::Domestic(const Domestic& s)
{
int m=15;
arrPrise=new int[m];
for(int i=0; i<m; i++) arrPrise[i]=s.arrPrise[i];
}
Domestic::~Domestic()
{
delete [] arrPrise;
}
void Domestic::output(int i)
{
Animal::output(i);
cout<<"4."<<"Prise: "<<arrPrise[i]<<endl;
}
int Domestic::search(string Name)
{
int n=15, i;
for(i=0; i<n; i++)
{
if (Name==name[i])
return i;
}
return i;
}
Level::Level()
{
int lvl=0;
}
Level::Level(Level&s)
{
lvl=s.lvl;
}
void Level::input(Level&s)
{
cout<<"Enter your level"<<endl;
cin>>s.lvl;
}
Health::Health()
{
int n=15;
health=new string[n];
Level A;
value(A);
}
Health::Health(Health& s)
{
int m=15;
health=new string[m];
for(int i=0; i<m; i++) health[i]=s.health[i];
Level B;
value(B);
}
void Health::value(Level& s)
{
if(s.get()>=1 && s.get()<=5)
{
health[0]="15";health[1]="15";health[2]="15";health[3]="15";health[4]="15";health[5]="15";health[6]="15";
health[7]="15";health[8]="15";health[9]="15";health[10]="15";health[11]="15";health[12]="15";health[13]="15";health[14]="15";
}
if (s.get()>5 && s.get()<=10)
{
health[0]="20";health[1]="20";health[2]="20";health[3]="20";health[4]="20";health[5]="20";health[6]="20";
health[7]="20";health[8]="20";health[9]="20";health[10]="20";health[11]="20";health[12]="20";health[13]="20";health[14]="20";
}
if (s.get()>10 && s.get()<=15)
{
health[0]="25";health[1]="15";health[2]="15";health[3]="15";health[4]="15";health[5]="15";health[6]="15";
health[7]="15";health[8]="15";health[9]="15";health[10]="15";health[11]="15";health[12]="15";health[13]="15";health[14]="15";
}
if (s.get()>15 && s.get()<=20)
{
health[0]="30";health[1]="15";health[2]="15";health[3]="15";health[4]="15";health[5]="15";health[6]="15";
health[7]="15";health[8]="15";health[9]="15";health[10]="15";health[11]="15";health[12]="15";health[13]="15";health[14]="15";
}
}
/*void Health::outpat(int i)
{
cout<<"5."<<"Health: "<<health[i]<<endl;
}*/
Health::~Health()
{
delete [] health;
}
string Health::getelement(Health&h,int i)
{
return h.health[i];
}
Health& Health::operator =(const Health& s)
{
delete[] health;
int k=15;
health =new string[k];
for(int i=0; i<k; i++) health[i]=s.health[i];
return *this;
}
Wild::Wild()
{
int n=15;
damage=new int[n];
damage[0]=5;damage[1]=10;damage[2]=15;damage[3]=20;damage[4]=25;damage[5]=30;damage[6]=35;
damage[7]=40;damage[8]=45;damage[9]=50;damage[10]=55;damage[11]=60;damage[12]=65;damage[13]=70;damage[14]=75;
Health hp;
}
Wild::Wild(Wild&s)
{
int m=15;
damage=new int[m];
for(int i=0; i<m; i++) damage[i]=s.damage[i];
Health M;
M=hp;
}
void Wild::output(int i)
{
Animal::output(i);
cout<<"4."<<"Damage "<<damage[i]<<endl;
cout<<"5."<<"Health"<<hp.getelement(hp,i)<<endl;
} |
|