Default private access in C++

Posted on 2005-04-02
Medium Priority
Last Modified: 2008-01-09

I know that the default access in C++ is private.  But why is the default constructor (compiler's constructor if we don't provide one) public?


class Dog

void main()
Dog fido;

If the default access is private, then why am I still able to instantiate an object of the Dog class?  Thanks.
Question by:Monica2003
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 13690545
A default constructor is implicitly declared if you don't declare any
constructor of your own.  An implicitly declared default constructor
is public and inline.

That's how C++ comiler generates obj code.

LVL 23

Accepted Solution

Mysidia earned 150 total points
ID: 13690751

> But why is the default constructor (compiler's constructor if we don't provide one) public?

Because it is defined to be that way as part of the C++ language, and default constructors
would be useless if it were not so.

The constructor will be public unless it appears in the class declaration and
in a section not labelled (or labelled with 'protected' or 'private').

When we say 'the default access is private', what is meant that the default access
of _things explicitly declared_ in the structured type.

By the way, default access for declared access is not always private, either.
i.e. If you had written

struct Dog {

int main() {
   Dog fido();

The default access for declared members would be public.

A 'class' is more or less just a struct where the default is private.


Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question