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); }
};
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.
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.
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
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.