开发者

What is the default protection level when inheriting a subclass? [duplicate]

This开发者_Python百科 question already has answers here: Closed 11 years ago.

Possible Duplicate:

Default class inheritance access

I know I can set the protection level when I declare a subclass from a superclass as in:

class Dog : public Pet {
   *blah, blah, blah*
}

But what does the protection level default to in this case?

Class Dog: Pet {
   *blah, blah, blah*
}


For a class it is private

class Dog: Pet  // Pet is inherited privately.
{}

For a struct it is public.

struct Dog: Pet  // Pet is inherited publicly.
{}

Simple test:

class Pet {};
class  DogClass:  Pet {};
struct DogStruct: Pet {};
int main()
{
    DogClass   dogClass;
    // Pet&       pet1 = dogClass;  This line will not compile.

    DogStruct  dogStruct;
    Pet&       pet2 = dogStruct;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜