I am writing a program which has these require ments
1.Program must be command-line based and interactive, allowing the user to control its operation by entering commands.
2. ability to accept new data
3. ability to search for specific previously entered data
4. ability to save data to a file
5. ability to retrieve data from a file
6. at least 5 classes
7. must show inheritance and polymorphism
8. must read and write a file

I am writing a program to store a basketball league. I have two classes already called personType and PlayerType, one of which is inherited from the other.

I am now brainstorming for the part of my program which will store 3 different divisions. Each division will have 5 teams and each team will have 5 players.

I was going to have the division class be the base class and team class will be derived from division and player's derived from team.

Would it be best to have division object which have a string for the division name, an array for the team names and each team would have an array of players?

#ifndef H_playerType
#define H_playerType

#include <fstream>
#include "personType.h"


class playerType : public personType
{
public:
    void print();
        //Function to print player's first and last name
        //and also their number and position

    void printFile(ostream& outF);
      //Function to print the player information.
      //This function sends the player information to the
      //output device specified by the parameter outF. If the 
      //actual parameter to this function is the object cout, 
      //then the output is shown on the standard output device. 
      //If the actual parameter is an ofstream variable, say 
      //outFile, then the output goes to the file specified by 
      //outFile.
    
    void setPlayerNum(int num);
        //Function to set the Player's number
        //Postcondition:    playerNum = num;

    int getNum() const;
        //Function to get player's number
        //Postcondition:    return playerNum

    void setPlayerPos(string pos);
        //Function to set the Player's position
        //Postcondition:    playerPos = pos;

    string getPos() const;

    playerType(string first = "", string last = "", int num = 0, string pos = "");
        //Constructor 
        //sets the parameters specified by the user
        //defaults first and last name and pos to null strings, num = 0
        //Postcondition:    personType name = first, last; playerNum = num; playerPos = pos;

private:
    personType name;    //variable to hold player's first and last name
    int playerNum;        //variable to store the player's Number
    string playerPos;    //variable to store the player's Position

};

#endif



#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>


using namespace std;

#include "playerType.h"

playerType::playerType(string first, string last, int num, string pos)

    : personType(first, last)
{
    playerNum = num;
    playerPos = pos;
    
} // end function playerType

void playerType::setPlayerNum(int num)
{
    try                    //Exception Handling
    {
        if(num < 0 )
            throw num;

        playerNum = num;
    }
    catch (int num)
    {
        cout << "Exception: Player's number must be positive. ";
    }

} //end function setPlayerNum

int playerType::getNum() const
{
    return playerNum;
} //end function getNum

void playerType::setPlayerPos(string pos)
{
    
    try                    //Exception handling
    {
        if (pos.length() > 2)
            throw string("Player's position must be PG, SG, G, SF, PF, F,, FC or C!");
        playerPos = pos;
    }
    catch (string str)
    {
        cout << "Exception handler: " << str << endl << endl;
    }
} //end function setPlayerPos

string playerType::getPos() const
{
    return playerPos;
} //end getNum function

void playerType::print()
{
    personType::print();
    cout << "Player's Number: " << playerNum << endl
        << "Player's Position: " << playerPos << endl << endl;
} //end function print


void playerType::printFile(ostream& outF)
{
    outF << left;
    outF << "Player Name:" << "  " << "Number:" << "    " << "Position:\n";
    outF << setw(9) << getFirstName();
    outF << setw(10) << getLastName();
    outF << setw(12) << getNum();
    outF << setw(12) << getPos();
}



#ifndef H_personType
#define H_personType

#include <string>
 
using namespace std;

class personType
{

    friend ostream& operator<<(ostream& , const personType& );
    friend istream& operator>>(istream& , personType& );
public:
    void print() const;
       //Function to output the first name and last name
       //in the form firstName lastName.
  
    void setName(string first, string last);
       //Function to set firstName and lastName according 
       //to the parameters.
       //Postcondition: firstName = first; lastName = last

    string getFirstName() const;
       //Function to return the first name.
       //Postcondition: The value of the firstName is returned.

    string getLastName() const;
       //Function to return the last name.
       //Postcondition: The value of the lastName is returned.

    virtual float pay(float hoursWorked) const;
        //Function exhibiting Polymorphism which calculates pay
        //Postcondition: The value of the person's pay is returned.

    personType(string first = "", string last = "", float personPay = 2500.00);
       //constructor
       //Sets firstName and lastName according to the parameters.
       //The default values of the parameters are empty strings.
       //Postcondition: firstName = first; lastName = last  

 private:
    string firstName; //variable to store the first name
    string lastName;  //variable to store the last name
    float payRate;      //variable to store the person's payRate
};

#endif




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

using namespace std;

    //Overloaded extration << operator
ostream& operator<<(ostream& osObject, const personType& name)
{
    osObject << name.firstName << " " << name.lastName << endl;

    return osObject;
}

    //Overloaded insertion >> operator
istream& operator>>(istream& isObject, personType& name)
{
    cout << "Please enter Person's first and last names: " << endl;
    isObject >> name.firstName >> name.lastName;

    return isObject;
}

void personType::print() const
{
    cout << "Name: " << firstName << " " << lastName << endl;
}

void personType::setName(string first, string last)
{
    firstName = first;
    lastName = last;
}

string personType::getFirstName() const
{
    return firstName;
}

string personType::getLastName() const
{
    return lastName;
}

float personType::pay(float hoursWorked) const
{
    return hoursWorked * payRate;
}

    //constructor
personType::personType(string first, string last, float personPay) 
{ 
    firstName = first;
    lastName = last;
    payRate = personPay;
}





#include <iostream>
#include <string>
#include <fstream>

#include "playerType.h"
#include "personType.h"

using namespace std;

int main()
{
    ifstream inData;
    ofstream outData;

    inData.open("Player.txt");
    outData.open("PlayerOutput.txt");

    personType person("Jim", "Webb");
    playerType player("Ray", "Allen", 34, "SG");

    cout << "*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_\n"
        "Please choose a menu option: \n\n"
    cout << "1. to add a player to a team." << endl 
         << "2. to search for a player on a team." << endl
         << "3. to save list of players." << endl
         << "X. to EXIT the program." << endl;

    player.printFile(outData);

    inData.close();
    outData.close();
}

I think that is a cool project, btw. Anyway for the division, teams and league I'll give some fastly written code that might spark some ideas in that creative head of yours. Here is some example class that incorporate a simple implementation of polymorphism. Hope you can get something out of this. Your code looks good, kudo's on the formatting:

#ifndef SOMETHING_H 
#define SOMETHING_H  

class team {     
friend ostream &operator<<(ostream &out, const team tm); //Print team_N common information     
public:    
team();    
 ~team();     // Some simple polymorphism to print out a team_N specific class information or etc.     
virtual void teamClass() {cout << "No Class" << endl;} // Not pure virtual so not ADT (i.e. const)      
protected:     
private: 
}; 

// Team one
 class team_1 :  public team 
{     
public:     
team_1();    
~team_1();     
void teamClass() { cout << "Team 1 Specific Class information" << end;}      
protected:     
private:
 };

class team_2 : public team {    
public:    
 team_2();     
~team_2();     
void teamClass() { cout << "Team 2 Specific Class information" << end;}     
protected:     
private: 
}; 

class team_3 : public team {    
public:     team_3();     
~team_3();     
void teamClass() { cout << "Team 3 Specific Class information" << end;}    
protected:     
private:
}; 

class team_4 : public team {     
public:     
team_4();    
 ~team_4();     
void teamClass() { cout << "Team 4 Specific Class information" << end;}    
 protected:    
 private: 
}; 

class team_5 : public team {     
public:     
team_5();     
~team_5();    
 void teamClass() { cout << "Team 5 Specific Class information" << end;}    
 protected:     
private: 
};     

// Division inherits from public and league regulations 
class division : public league, public league_regulations {    
friend ostream &operator<<(ostream &out, const division div);      
public:     
division(string division_name = "Default Division", string league_name = "Basketball");         
~division();     
 protected:     
team_1 t1;    
team_2 t2;    
team_3 t3;     
team_4 t4;    
team_5 t5;     
private:     
string div_name; 
}; 
#endif 

 #include "something.h"  

division::division(string division_name, string league_name) : league(league_name), league_regulations(division_name) {     
div_name = division_name; 
} ....

Anyway, you can see it is not finished but I hope it can perhaps give you an idea(s) that can help meet the specifications of you project.

Good luck, LamaBot

Wouldn't it be better to simply write one team class and just instantiate 5 teams from that one class? What would be the benifit to the 5 different team classes?

Wouldn't it be better to simply write one team class and just instantiate 5 teams from that one class? What would be the benifit to the 5 different team classes?

Well that depends I suppose. What common mechanisms do teams have, yet are implemented differently? Such as each team usually practices at certain locations, which is might be useful information. You could use the base class to write a function that tells a given team to practice, which where procedures might be different among team, but they all practice. I know that this is probably a useless idea, but I'm just trying to perhaps give you a visualization of how to implement the hierarchy.

Good luck, LamaBot

So if I were to only write one team class and used a menu that the user chose to (C)reate a team. would I use a local static variable to create a team object within the switch where a new object could be create incrementally. i.e.

switch (x)
       case 'c' case 'C'
       static int t = 1;
       team team[t]  //where this is a created object
       ++t;
       team1.createTeam();
      break;

You are better off creating a single team class and instantiating the different teams from that class since there are no specific attributes which would make you want a create a seperate class for each of them.

You can create a vector variable in the Team class and the method add player would insert the instance of player in that vector. Thus you would have a team object holding a collection of many players.

Consider adding "addPlayer ( )", "removePlayer ( )" , "queryPlayer ( )" etc functions in your Team class.

So im new to vectors and I'm trying to create an addTeam function, The ultimate outcome should be an object with a division name, 5 teams in a division and 5 players on each team. Here is what I have so far. Please advise on whether I am on the right track or not.


Getting these errors

team.h(22) : error C2059: syntax error : 'constant'
team.cpp(9) : error C2373: 'team::addTeam' : redefinition; different type modifiers
team.h(17) : see declaration of 'team::addTeam'
team.cpp(11) : error C2228: left of '.push_back' must have class/struct/union

#ifndef H_team
#define H_team

#include <string>
#include <vector>
#include "division.h"
#include "personType.h"


using namespace std;

class team : public division , public personType
{
public:
    void printTeam(); const

    void addTeam(string divName, vector<string> name);    //Line 17
        //Function to store a team with a division name and player names
        //Postcondition:    divisionName = divName; firstName = fName; lastName = lName

private:
    vector<string> playerName(5);       //Line 22
    string divisionName;

};

#endif
#include "team.h"
#include <string>
#include <iostream>
#include <vector>


void team::addTeam(string divName, vector<string> name)
{
    divisionName = divName;
    playerName.push_back(name);
}

You can't initalize variables in the class definition. Try doing it in the constructor with an initialization list:

team() : playerName(5) {};
This article has been dead for over six months. Start a new discussion instead.