Inheritance and Access Specifiers

I have trouble understanding a certain thing about inheritance, even though I have already read the tutorial. Let's assume a class derives from a base class with public access specifier as in the simple example below. Will the class Daughter be able to change the content of the private members (a,b) of the class Mother with its own constructor?
If it is not possible, in order to change the contents of a,b do I have to use the constructor of the class Mother?

1
2
3
4
5
6
7
8
9
10
11
12
13
class Mother {
private:
int a,b;
public:
Mother() { a=1; b=1; }
};

class Daughter : public Mother {
private:
int c,d;
public:
Daughter() { c=1;d=1;}
};
Will the class Daughter be able to change the content of the private members (a,b) of the class Mother with its own constructor?

If you mean like this...

1
2
3
4
5
6
7
8
9
class Daughter : public Mother {
private:
    int c,d;
public:
    Daughter() {
        c=1;d=1;
        a=3;b=2; // not allowed if private
    }
};


... NO!

BUT if you make int a,b; protected in Mother, then you will be able to change them.

BUT...

1. it is better form to use the constructor init list when intializing member variables, e.g.

1
2
3
4
5
6
class Mother {
private:
    int a,b;
public:
    Mother() : a(1), b(1) { /*nothing to do here*/ }
};


2. and rather than make a,b protected, you should consider providing a constructor which takes params

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Mother {
private:
    int a,b;
public:
    Mother() : a(1), b(1) {}
    Mother(int a_, int b_) : a(a_), b(b_) {}
    //or just provide constructor with defaults
    //Mother(int a_ = 1, int b_ = 1) a(a_), b(b_) {}
};

class Daughter : public Mother {
private:
    int c,d;
public:
    Daughter() : Mother(2, 2), c(1), d(1) {}
    // etc
};


do I have to use the constructor of the class Mother?

No, but it's better encapsulation than using protected.

Andy
Last edited on
class athlete {
private:
string name;
public:
void print_athlete();
athlete (string athlete_name) { name=athlete_name; }
};

class event
{
private:
string etitle;
string organizer;
string place;
date_time when;
int official;
sport *game;
event *next;
public:
event (string event_title, string org, int event_year, int event_day, int event_hour, int event_minute, string plc, int offic, sport *s)
{
etitle=event_title;
organizer=org;
when.y=event_year; when.m=event_month; when.d=event_day; when.hh=event_hour; when.mm=event_minute;
place=plc;
official=offic;
game=s;
next=nullptr;
}
void connect (event *e) { next=e; }

};

class individual_sport : public event {
private:
athlete *the_participant;
public:
virtual void print_event();
virtual bool is_athlete_in_event(string name);
individual_sport(string event_title, string org, int event_year, int event_day, int event_hour, int event_minute, string plc, int offic, sport *s, athlete *t): event(event_title, org, event_year, event_day, event_hour, event_minute, plc, offic, s), the_participant(t) {}
};

class team_sport : public event
{
private:
athletes *the_team;
public:
virtual void print_event();
virtual bool is_athlete_in_event(string name);
team_sport (string event_title, string org, int event_year, int event_day, int event_hour, int event_minute, string plc, int offic, sport *s, athlete *t) : event(event_title, org, event_year, event_day, event_hour, event_minute, plc, offic, s), the_team(t) {}
};

This is what my code actually looks like. I created it according to your advice and got a little help from the tutorials about inheritance as well. Is there any mistake?

Thank you in anvance.
Please tag and format your code, as you more or less did the code in your opening post (indenting correctly will help!)

Thanks Andy
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
class athlete {
  private:
     string name;
  public:
     athlete (string athlete_name) { name=athlete_name; }
};

class event 
{
  private:
    string etitle;
    string organizer;
    string place;
    date_time when;
    int official;
    sport *game;
    event *next;
  public:
     event (string event_title, string org, int event_year, int event_day, int event_hour, int event_minute, string plc, int offic, sport *s)
    {
    etitle=event_title;
    organizer=org;
    when.y=event_year; when.m=event_month; when.d=event_day; when.hh=event_hour; when.mm=event_minute;
    place=plc;
    official=offic;
    game=s;
    next=nullptr;
   }
    void connect (event *e) { next=e; }

};

class individual_sport : public event {
  private:
    athlete *the_participant;
  public:
    individual_sport(string event_title, string org, int event_year, int event_day, int event_hour, int event_minute, string plc, int offic, sport *s, athlete *t): event(event_title, org, event_year, event_day, event_hour, event_minute, plc, offic, s), the_participant(t) {}
};

class team_sport : public event 
{
  private:
    athlete *the_team;
  public:
     team_sport (string event_title, string org, int event_year, int event_day, int event_hour, int event_minute, string plc, int offic, sport *s, athlete *t) : event(event_title, org, event_year, event_day, event_hour, event_minute, plc, offic, s), the_team(t) {}
}


Oops! I didn't notice there was no format! Sorry!
Last edited on
line 43.. do you actually have a class called "athletes"?
Last edited on
No, it was supposed to be ''athlete''. Is that the only mistake?
well, no. But I was hoping you'd actually post code that compiles. In that way we can help you a lot faster.
Well, I just started learning C++ a week ago and I already have a huge project for school. My code is incomplete and it is already 250 lines long. There are certainly many mistakes because I don't know how to use all the features I am asked to use. I was just hoping you could tell me what mistakes I have made in the syntax of the constructors of the classes above.
oh right. I think you should really be compiling as you're going along.
andywestken's first reply is a very good generic description of your issue. I would probably get it compiling first. you could even comment out your athlete class so you can concentrate on your event inheritance tree.
Well the problem is that our professors want us to ''learn'' from this exercise, so half the data of the event are private members of other classes, which confuses me a lot, since I am not used to C++ yet!
Oops! I didn't notice there was no format! Sorry!

I did expect you to edit your existing post rather than re-post!?

Anyway...

Well, for one thing, you should get into the habit of wrapping lines of code so they don't disappear of the side of the monitor. It's annoying to have to scroll right and left so much!

33
34
35
36
37
38
class individual_sport : public event {
  private:
    athlete *the_participant;
  public:
    individual_sport(string event_title, string org, int event_year, int event_day, int event_hour, int event_minute, string plc, int offic, sport *s, athlete *t): event(event_title, org, event_year, event_day, event_hour, event_minute, plc, offic, s), the_participant(t) {}
};


line 37 is 274 chars long -- way, way too long. The 80 column limit from the early days of computing (with 80 column text editors) is a bit draconian, but you shouldn't usually go much beyond that as it's nice to have screen space for other things, like debuggers, etc.

33
34
35
36
37
38
39
40
41
42
class individual_sport : public event {
  private:
    athlete *the_participant;
  public:
    individual_sport(string event_title, string org, int event_year, int event_day,
                     int event_hour, int event_minute, string plc, int offic,
                     sport *s, athlete *t)
    : event(event_title, org, event_year, event_day, event_hour, event_minute, plc, offic, s)
    , the_participant(t) {}
};


I've already mentioned constructor initializer lists...

Maybe the parameters int event_year, int event_day, int event_hour, int event_minute should be replaced with a single date_time parameter?

From an OO point of view, I'd expect class team_sport to have two team members, if you're talking about a football/baseball/hockey match (home team and away team?) (And it should really be a team_sport_event rather than a team_sport.)

Similarly, class individual_sport will need a set of competitors, as there are usually more than one athelete in a sprint race or golf tornament. (And it should be a individual_sport_event.)

And I don't see why an event should know about the next event. That should be handled by an "event_calendar" class.

Andy
Last edited on
Topic archived. No new replies allowed.