hey guys quick question. I have a base class "baseballplayer". From there i have made a derived class "pitcher". The big honcho of the program is "PlayerDatabase" which is used to store all the baseballplayers. I want to access 'P_earnedRuns' from my Pitcher class and add up multiple P_earnedRuns from my PlayerDatabase class.. the question is how would i grab P_earnedRuns from a stored Pitcher object and lets say cout that from commented out area in my PlayerDatabase.cpp code..? THANKS!!
THIS IS PLAYERDATABASE.CPP
#include "PlayerDatabase.h"
#include "Pitcher.h"
#include "Hitter.h"
}
void PlayerDatabase::load_team(ifstream& input)
{
for(int i = 0; i < 4; i++)
{
input >> P_or_H;
if(P_or_H == 'P')
{
team[i] = new Pitcher;
team[i]->load_player(input);
}
else if(P_or_H == 'H')
{
team[i] = new Hitter;
team[i]->load_player(input);
}
else
cout << "This line does not specify as a Pitcher or Hitter.";
/* OR SHOULD I USE DYNAMIC_CAST TO FIND IT?????
pitcherPtr = dynamic_cast<Pitcher*>(team[i]);
if(pitcherPtr)
{
tot_earnedRuns = tot_earnedRuns + team[i]->P_earnedRuns;
}
*/
}
}
void PlayerDatabase::print_team()
{
for(int i = 3; i < 4; i++)
{
team[0]->print_player();
//cout << team[0]-> HOW WOULD I ACCESS P_earnedRuns FROM PITCHER CLASS???
}
}
////////////////////////////////////////////////////////////////////
AND MY PITCHER CLASS..
#include <iostream>
#include <iomanip>
#include <fstream>
#include "Pitcher.h"
using namespace std;
Pitcher::Pitcher(string name, int height, int weight, char throws, int earnedRuns, float inningsPitched, const char THROWS_UNINITIALIZED)
: BaseballPlayer(name, height, weight)
{
P_throws = throws;
P_earnedRuns = earnedRuns;
P_inningsPitched = inningsPitched;
Why isn't "PitchingStats" just a member of "Player"? Either a player has pitched and has pitching stats, or they haven't and don't have any such stats. This maps well to a relational database where there would be a 1..[0..1] relationship between Player and PitchingStats tables.
Then you just do something like this:
1 2 3 4 5 6 7 8
if (player.hasPitchingStats())
{
// compute ERA
}
else
{
// ERA is undefined.
}
This is one case where I think inheritance may not be the most appropriate representation of the data. Whenever I get into a bind like this, it always serves me well to ask "is inheritance really appropriate here?" More often than not the answer is "no."