Inheritance question

Hello ,
Can someone tell me this please ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class CPolygon {
  protected:
    int width, height;
  public:
    void set_values (int a, int b)
      { width=a; height=b;}
  };

class CRectangle: public CPolygon  //Can anyone please tell me what does public mean here
//,and what would happen when we use protected and private here.
{
  public:
    int area ()
      { return (width * height); }
  };

class CTriangle: public CPolygon {
  public:
    int area ()
      { return (width * height / 2); }
  };
  
Last edited on
I can't believe you are already back.

http://www.cplusplus.com/doc/tutorial/classes/
I know what private public and protected mean .
I am confused because we are using two access specifiers one is in the base class and one while defining the derived class.what does the class CRectangle: public CPolygon public is doing here.
can someone give small examples.
From http://www.cplusplus.com/doc/tutorial/inheritance/ :
The public access specifier may be replaced by any one of the other access specifiers protected and private. This access specifier limits the most accessible level for the members inherited from the base class: The members with a more accessible level are inherited with this level instead, while the members with an equal or more restrictive access level keep their restrictive level in the derived class.
just above the code example you posted
closed account (z05DSL3A)
The access level of the base class affects which member of the base class are accessible to users of the derived class. In effect it caps the access level of the inherited members, so in your example, class CRectangle: public CPolygon, the member of inherited from CPolygon have the same access level in CRectangle. If it were private inheritance all the member inherited from CPolygon would be private in CRectangle.

Public inheritance:
public member become public member
protected member become protected member
private member become private members

Protected inheritance:
public member become protected member
protected member become protected member
private member become private members

Private inheritance:
public member become private member
protected member become private member
private member become private members




Last edited on
Can someone tell me what is wrong with the following code ?

[
1
2
3
4
5
6
7
8
9
10
11
12
13
14
code]class CPolygon {
public:
    int width, height;
public:
  
    void set_values (int a, int b)
      { width=a; height=b;}
  };

class CRectangle:protected CPolygon {
	public:
    int area ()
      { return (width * height); }
  };


as we know that in protected inheritance public become protected which means it should work.
Last edited on
closed account (z05DSL3A)
I assume you are doing this:

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
#include <iostream>
//-----------------------------------------------------------------------------
class CPolygon 
{
public:
    int width, height;

public:  
    void set_values (int a, int b)
      { width=a; height=b;}
};
//-----------------------------------------------------------------------------
class CRectangle : protected CPolygon 
{
public:
    int area ()
      { return (width * height); }
};
//-----------------------------------------------------------------------------
int main() 
{
    CRectangle rect;

    rect.set_values(2,4);  // error: set_values() is protected in CRectangle

    std::cout << "Area: " << rect.area() << std::endl;

    return 0;
}

/* End of file ***************************************************************/


thank you grey wolf
Topic archived. No new replies allowed.