У меня возникли проблемы, и мне нужна ваша помощь. У меня есть один основной класс и один унаследованный от моего основного класса
вот мой основной класс
class NBAPlayer {
protected:
char *ime;
char tim[40];
double prosekBrPoeni;
double prosekBrAsistencii;
double prosekBrSkokovi;
public:
NBAPlayer(char *ime="",char *tim="",double prosekBrPoeni=0,double prosekBrAsistencii=0,double prosekBrSkokovi=0) {
this->ime = new char [strlen(ime)+1];
strcpy(this->ime,ime);
strcpy(this->tim,tim);
this->prosekBrPoeni=prosekBrPoeni;
this->prosekBrAsistencii=prosekBrAsistencii;
this->prosekBrSkokovi=prosekBrSkokovi;
}
NBAPlayer (const NBAPlayer &nbp) {
this->ime = new char [strlen(ime)+1];
strcpy(this->ime,nbp.ime);
strcpy(this->tim,nbp.tim);
this->prosekBrPoeni=nbp.prosekBrPoeni;
this->prosekBrAsistencii=nbp.prosekBrAsistencii;
this->prosekBrSkokovi=nbp.prosekBrSkokovi;
}
NBAPlayer &operator =(const NBAPlayer &nbp) {
if (this!=&nbp) {
delete [] ime;
this->ime = new char [strlen(ime)+1];
strcpy(this->ime,nbp.ime);
strcpy(this->tim,nbp.tim);
this->prosekBrPoeni=nbp.prosekBrPoeni;
this->prosekBrAsistencii=nbp.prosekBrAsistencii;
this->prosekBrSkokovi=nbp.prosekBrSkokovi;
}
return *this;
}
~NBAPlayer() {
delete [] ime;
}
double rating() {
/*double poeni = prosekBrPoeni* (45/100);
double asistencii = prosekBrAsistencii *(30/100);
double skokovi = prosekBrSkokovi * (25/100);
double vkupno = poeni+asistencii+skokovi;
return vkupno; */
return prosekBrPoeni* (45/100) + prosekBrSkokovi * (25/100) +prosekBrAsistencii *(30/100);
}
void print() {
cout<<ime<<" - "<<tim<<endl;
cout<<"Points: "<<prosekBrPoeni<<endl;
cout<<"Assists: "<<prosekBrAsistencii<<endl;
cout<<"Rebounds: "<<prosekBrSkokovi<<endl;
cout<<"Rating: "<<rating()<<endl;
}
};
Это мой унаследованный класс
class AllStarPlayer: public NBAPlayer {
private:
double allstarpoeni;
double allstarasists;
double allstarskok;
public:
AllStarPlayer() {
this->allstarpoeni=allstarpoeni=0;
this->allstarasists=allstarasists=0;
this->allstarskok=allstarskok=0;
}
AllStarPlayer(char *ime,char *tim,double prosekBrPoeni,double prosekBrAsistencii,double prosekBrSkokovi,double allstarpoeni,double allstarasists,double allstarskok)
:NBAPlayer(ime,tim,prosekBrPoeni,prosekBrAsistencii,prosekBrSkokovi) {
//this->n = n;
this->allstarpoeni=allstarpoeni;
this->allstarasists=allstarasists;
this->allstarskok=allstarskok;
}
AllStarPlayer(NBAPlayer &n,double allstarpoeni,double allstarasists,double allstarskok)
:NBAPlayer(ime,tim,prosekBrPoeni,prosekBrAsistencii,prosekBrSkokovi) {
this->allstarpoeni=allstarpoeni;
this->allstarasists=allstarasists;
this->allstarskok=allstarskok;
}
AllStarPlayer (AllStarPlayer &a) {
this->allstarpoeni=a.allstarpoeni;
this->allstarasists=a.allstarasists;
this->allstarskok=a.allstarskok;
}
AllStarPlayer &operator = (const AllStarPlayer &a) {
if (this!=&a) {
this->allstarpoeni=a.allstarpoeni;
this->allstarasists=a.allstarasists;
this->allstarskok=a.allstarskok;
}
}
~AllStarPlayer() {}
double allStarRating() {
double poeni = allstarpoeni* (30/100);
double asistencii = allstarasists *(40/100);
double skokovi = allstarskok * (30/100);
double vkupno = poeni+asistencii+skokovi;
return vkupno;
}
double rating() {
return (NBAPlayer::rating()+allStarRating())/2;
}
void print() {
NBAPlayer::print();
cout<<"All Star Rating: "<<allStarRating()<<endl;
cout<<"New Rating: "<<rating()<<endl;
}
};
И основная функция
int main() {
char name[50];
char team[40];
double points;
double assists;
double rebounds;
double allStarPoints;
double allStarAssists;
double allStarRebounds;
NBAPlayer * players = new NBAPlayer[5];
AllStarPlayer * asPlayers = new AllStarPlayer[5];
int n;
cin >> n;
if (n == 1) {
cout << "NBA PLAYERS:" << endl;
cout << "=====================================" << endl;
for (int i = 0; i < 5; ++i) {
cin >> name >> team >> points >> assists >> rebounds;
players[i] = NBAPlayer(name,team,points,assists,rebounds);
players[i].print();
}
} else if (n == 2) {
for (int i=0; i < 5; ++i) {
cin >> name >> team >> points >> assists >> rebounds;
cin >> allStarPoints >> allStarAssists >> allStarRebounds;
players[i] = NBAPlayer(name,team,points,assists,rebounds);
asPlayers[i] = AllStarPlayer(players[i],allStarPoints,allStarAssists,allStarRebounds);
}
cout << "NBA PLAYERS:" << endl;
cout << "=====================================" << endl;
for (int i=0; i < 5; ++i)
players[i].print();
cout << "ALL STAR PLAYERS:" << endl;
cout << "=====================================" << endl;
for (int i=0; i < 5; ++i)
asPlayers[i].print();
} else if (n == 3) {
for (int i=0; i < 5; ++i) {
cin >> name >> team >> points >> assists >> rebounds;
cin >> allStarPoints >> allStarAssists >> allStarRebounds;
asPlayers[i] = AllStarPlayer(name, team, points, assists, rebounds,
allStarPoints,allStarAssists,allStarRebounds);
}
cout << "ALL STAR PLAYERS:" << endl;
cout << "=====================================" << endl;
for (int i=0; i < 5; ++i)
asPlayers[i].print();
}
delete [] players;
delete [] asPlayers;
}
Теперь, когда я использую случай 1 или этот пример
1
Nikola_Jokic Denver_Nuggets 18.3 6.1 10.6
Lonzo_Ball Los_Angeles_Lakers 10.2 7.2 6.9
Donovan_Mitchell Utah_Jazz 20.4 3.6 3.7
Ben_Simmons Philadelphia_76ers 16 8.2 8.2
Kristaps_Porzingis New_York_Knicks 22.7 1.2 6.6
Все хорошо, но мой рейтинг всегда равен 0, я не знаю, почему, где моя ошибка, также, когда я использую другой случай, такой как случай 2 или 3, когда я выступаю перед своим унаследованным классом, я получаю нули за все, а не только за мой рейтинг.
Что мне нужно изменить в моем первом классе, чтобы получить правильный результат для рейтинга? - первый случай
Мой унаследованный конструктор неверен? - второй случай