Inheritance

Why is this error?

1
2
3
4
5
6
7
class Polygon
{
protected:
    int width,length;
public:
    Polygon(int n,int m) : length(n), width(m) {}
};

1
2
3
4
5
6
class Triangle : public Polygon,public Output
{
public:
    Triangle(int n,int m) : length(n),width(m) {}
    int area(){return width*length/2;}
};


I thought that Triangle class inherit all the member from Polygon class, it means that it also inherit width and length right?
So, why I may not set the length and width value using this method?
Why must I use Triangle(int n,int m) : Polygon(n,m) {} method??

Thanks..

Edit:
I forgot to ask :
class Triangle : public Polygon
What is the function of declaring public/protected/private in here??

Thanks..
Last edited on
So, why I may not set the length and width value using this method?
Because when you defined Polygon, you created just one way to create a Polygon object: the constructor that takes two ints. That is a contract with anything that wants to use Polygon, including derived classes like Triangle.
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
#include <iostream>

// I swapped the order of length and width since they should be in
// the same order as your member initializer list in the constructor, which
// itself should probably be ordered the same as the constructor parameters.
class Polygon
{
protected:
    int length, width;
public:
    Polygon(int len, int wid) : length(len), width(wid) {}
};

// Your error was trying to access length and width in the member initializer list.
// You could've done it in the body of the constructor since by that point
// the parent has been constructed.
// However, the proper way to initialize the Polygon parameters is to delegate
// the job to the Polygon constructor from the member initializer list.
class Triangle : public Polygon
{
public:
    Triangle(int len, int wid) : Polygon(len, wid) {}
    int area() { return width * length / 2; }
};

int main() {
    Triangle t(4, 7);
    std::cout << t.area() << '\n';
}


Inside a class, the access specifiers (public, protected, private) control the accessibility of members to users of the class as well as the accessibility of members to a class that derives from the class.

With inheritance, the access-specifier can change the accessibility within the derived class of the inherited members.

Within the class itself:
  public   : users of the class can   see it; derived classes can   see it
  protected: users of the class can't see it; derived classes can   see it
  private  : users of the class can't see it; derived classes can't see it

Used as an access-specifier during inheritance:
  public   : no change
  protected: what was public              in base is protected in derived
  private  : what was public or protected in base is private   in derived

If left out during inheritance, the access-specifier defaults to
  public:  for "structs"
  private: for "classes"


It's basically for cutting off access to upper levels of the hierarchy past certain points. If you use private inheritance then the previously public members won't be visible to users of your class (but they are still visible within the implementation of your class). In that situation we obviously cannot say that the derived class "is-a" base class. For that you need at least protected inheritance, which (other things being properly designed) makes the use of your class seem like a base class in appropriate contexts. But with protected inheritance, any further derived classes will not be like the base class since the public members of the base are no longer visible to users. Public inheritance allows the IS-A relationship both for users and further derived classes.
Topic archived. No new replies allowed.