Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7511533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:34:35+00:00 2026-05-29T23:34:35+00:00

Bjarne wrote:- For a type T, T() is the notation for the default value

  • 0

Bjarne wrote:-
For a type T, T() is the notation for the default value , as defined by the default constructor .
What happen when we don’t declare default constructor ? For example

using namespace std;

class date{
    int n,m;
    public:
   int day(){return n;}
   int month(){return m;}
          };//no default constructor

int main()
{
     date any =date();
     cout<<any.month()<<endl;   
     cout<<any.day()<<endl;
return 0;

}

Output of this program is 0 and 0 every time i run my program. I haven’t declare any default constructor then why there exits a default value i.e. 0?

EDIT-

    class date{
        int n,m;
        public:
        date (){
        m=1;}
       int day(){return n;}
       int month(){return m;}
     };

 int main()
  {
     date any =date();
     cout<<any.month()<<endl;   
     cout<<any.day()<<endl;
return 0;

}

After reading answers i provide a default constructor but now n is getting garbage value but according to answers it should be 0 as m is out of reach of any other constructor and it is value initialisation as mentioned in answer

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:34:38+00:00Added an answer on May 29, 2026 at 11:34 pm

    The behavior you see is Well-Defined for your class.


    How & Why is the behavior Well-Defined?

    The rule is:
    If you do not provide a no argument constructor the compiler generates one for your program in case your program needs one.
    Caveat:
    The compiler does not generate the no argument constructor if your program defines any constructor for the class.

    As per the C++ Standard an object can be initialized in 3 ways:

    • Zero Initialization
    • Default Initialization &
    • Value Initialization

    When, a type name or constructor initializer is followed by () the initialization is through value initialization.

    Thus,

    date any =date();
                  ^^^
    

    Value Initializes an nameless object and then copies it in to the local object any,
    while:

    date any;
    

    would be a Default Initialization.

    Value Initialization gives an initial value of zero to members that are out of reach of any constructor.
    In your program, n and m are beyond the reach of any constructor and hence get initialized to 0.


    Answer to Edited Question:
    In your edited case, your class provides a no argument constructor, date(), which is capable(& should) initialize members n and m, but this constructor doesn’t initialize both the members, So In this case no zero initialization takes place, and the uninitialized members in the object have an Indeterminate(any random) value, further this temporary object is copied to any object which displays the shows indeterminate member values.


    For Standerdese Fans:
    The rules for object Initialization are aptly defined in:

    C++03 Standard 8.5/5:

    To zero-initialize an object of type T means:
    — if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
    — if T is a non-union class type, each nonstatic data member and each base-class subobject is zero-initialized;
    — if T is a union type, the object’s first named data member is zero-initialized;
    — if T is an array type, each element is zero-initialized;
    — if T is a reference type, no initialization is performed.

    To default-initialize an object of type T means:
    — if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
    — if T is an array type, each element is default-initialized;
    — otherwise, the object is zero-initialized.

    To value-initialize an object of type T means:
    — if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
    — if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;
    — if T is an array type, then each element is value-initialized;
    — otherwise, the object is zero-initialized

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some people claim that using namespace std; is bad practice. Others say it's OK
In N3257 I found an example using initializing members without a constructor , which
Is a type specifier required here? const c = 7; Bjarne Stroustrup's 'The C++
I am reading Bjarne Stroustrup's Programming : Principles and Practice Using C++ In the
I am trying an example from Bjarne Stroustrup's C++ book, third edition. While implementing
Bjarne Stroustrup writes in his C++ Style and Technique FAQ , emphasis mine: Because
I'm studying The C++ Programming Language from Bjarne Stroustrup and he talks about logical
Guys in one of excersises (ch.5,e.8) from TC++PL Bjarne asks to do following: 'Run
In The C++ Programming Language, Bjarne writes that the null pointer is not the
class table{ name *p; int i; public: table(int j=15){p=new name[i=j]} //constructor ~table(){delete[]p;} } void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.