What does mean the public keyword?

Hi, I read the tutorials about classes but this convention is not clear to me:

That does mean the public Link after class ColorLink :
?

1
2
3
4
5
class ColorLink : public Link
{
public:
	long ref;
};


Note Link is class defined in code.
Last edited on
Look up section of Inheritance from the book/tutorials you're following. It tells the compiler that the class ColorLink 'inherits' publicly from the existing class Link.
Last edited on
http://www.cplusplus.com/doc/tutorial/inheritance/

Link is a public base of ColorLink. Alternatives are protected and private base.
public says how ColorLink can access member data of Link.

No. ... how the user of ColorLink can access members of ColorLink that actually members of the base
Oh, thanks. I'm surpised so many replies here. I found one more interesting declaration

class unit_in_rect : public std::unary_function<const Unit, bool>

I've found:
http://en.cppreference.com/w/cpp/utility/functional/unary_function
hm, but that looks hard to understand even I learned about the templates.

Last edited on
It is in no way different. ColorLink and unit_in_rect are deriving classes.
Link and std::unary_function<const Unit, bool> are base classes.
std is a namespace.
unary_function is a template for a class.
Is this right?

A) PUBLIC
class derived_class_name: public base_class_name
derived_class_name will inherit protected, private, public members

B) PRIVATE
class derived_class_name: private base_class_name
derived_class_name will inherit members protected, private ... Member public is not accessible in the class / instance.

C) PROTECTED
class derived_class_name: public base_class_name
derived_class_name will inherit member protected only. Members private and public are not accessible in the class / instance.
Is this right?


No. Hopefully the pseudocode thing below will help:

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
class Base
{
private: A

protected: B

public: C
};

class Derived: private Base
{
private: B, C
};

class Derived: protected Base
{
protected: B, C
};

class Derived: public Base
{
protected: B

public: C
};


As you can see, users of Derived can never access private members of Base, no matter what kind of inheritance is used.

As for B and C they get "inserted" in the Derived class as shown above.
Catfish666:
Thanks, I see now:

1) private members are not inherited at all
2) protected members and public members are degraded to private level if :private is used (and these are inherited).
3) protected members stays protected, but public members are degraded to protected level when :protected is used (and these are inherited)
4) when :public is used, then protected stays protected and public stays public (and these are inherited).
Topic archived. No new replies allowed.