How to inherit from one class to another

Hi Guys, I'm having a problem with inheritance.

I have created a class called cteams and when the program runs it asks the user to enter team names which are then stored in cteams.

What I want to do is create a manager class called cleague which will have a way of storing a collection of teams, these teams I want to come from the first class cteams.

Is this possible?here is some snippets of code which I have been working on trying to do this. thanks guys

#include <iostream>
#include <string>
using namespace std;

class cteams
{
private:
string f_team1, f_team2, f_team3,f_team4;
int f_points_for_team1, f_points_for_team2, f_points_for_team3, f_points_for_team4;
public:
cteams (int);
void set_team1_score (int),set_team2_score (int),set_team3_score (int),set_team4_score (int);
int get_team1_score (),get_team2_score (),get_team3_score (),get_team4_score ();
void set_team1_name (const string &),set_team2_name (const string &),set_team3_name (const string &),set_team4_name (const string &);
void set_team_name (const string &);
};

void cteams::set_team1_name (const string &team1)
{
f_team1 = team1;
}

class cleague:public cteams
{
private:
string teamNames;
public:
cleague (const string &team1);
void setTeams (const string &team1);
};


int main()
{
cteams teamscore (0);
cteams teams (0);

string team1;
string team2;
cout << "Enter Team 1 here: " << endl;
getline (cin, team1);
teams.set_team1_name (team1);
cout << "Enter Team 2 here " << endl;
getline (cin, team2);
teams.set_team2_name (team2);



cout << team1 << " Begins with " << teamscore.get_team1_score () << endl;
cout << team2 << " Begins with " << teamscore.get_team2_score () << endl;

system ("pause");
}

Thanks in advance guys!!


I want to inherit from the cteams class and store what I have taken from their in the cleague class and store it in an array.
Why are you using inheritance?

Teams has a collection of Team.
League has a Teams (not League is a Teams).
I'm alittle unsure what you mean??
I want to set up an array in cleague which will hold the information from cteams if your with me? and I assumed that I would have to inherit the information from cteams in order to place it in to an array?
Inheritance implies an "is a" relationship. Having a member variable implies a "has a" relationship.

If your league has-an array of teams, then logically you use a member variable:

1
2
3
4
5
6
7
8
class cleague   // no parent class
{
public:
 // ...

protected:
  cteams teamlist[ Number_of_teams ];
};


Deriving cleague from cteams is like saying a league "is a" team, which it of course isn't, and so that doesn't make much sense.
Ahh ok I'm with you, but still not sure on how I would go about obtaining the information which is stored in cteams?

Could you elaborate alittle more on the code? thanks
To access member variables/functions, you use the . operator (if you have an object) or the -> operator (if you have a pointer).

Here we have the normal object, so we'd use the . operator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class cleague
{
public:
  void SomeFunction()
  {
    // teamlist[0].GetScore() calls the GetScore() function in the cteams class
    //  using 'teamlist[0]' as the object

    if( teamlist[0].GetScore() > 100 )
    {
      // do something
    }
  }

protected:
  cteams teamlist[ Number_of_teams ];
};
class cleague
{
public:
void set_team1_name (const string &);
protected:
cteams teamlist [4];
};
void cleague::set_team1_name (const string &team1)
{
teamlist[1].set_team1_name (team1);
}


How about that is that a has a relationship? just written it now, will try the other example you have posted if this is incorrect?.

Many thanks for your time and patients
class cleague
{
public:

void set_team1_name (const string &team1)
{
teamlist[0].set_team1_name (team1);

if (teamlist[0].set_team1_name (team1))
{
cout<< "hello" << endl;
}
}

protected:
cteams teamlist [4];
};


The code above is returning error C2451:Conditional Expression of type void is illegal?

Could anyone shed any light on this?? thanks
if (teamlist[0].set_team1_name (team1))

if statements need to have a boolean or integral value in the parenthesis. If you do:

if( call_a_function() )

Then 'call_a_function()' must either return a bool or some other integral type.

set_team1_name returns a void (nothing), and therefore you cannot put it in an if() statement like that.
The problem im having here, is I want it to return a string and I'm not sure how to go about it? All I want is for it to put the team names which the user enters in to cteams and then for cleague to put them in to an array. I have managed to get them to store the names in cteams but still having megga problems with getting cleague to take them and put them in an array. However I do know that the "has a" relationship is deffo on the right tracks!!
Topic archived. No new replies allowed.