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

  • SEARCH
  • Home
  • 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 7512635
In Process

The Archive Base Latest Questions

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

Consider this code: class First { public: int y; First() { y = 90;

  • 0

Consider this code:

class First
{
public:
    int y;
    First()
    {
        y = 90;
    }
};

class Second{
    public:
       int x;
       First ob;  //if i comment it, value of x is initialised to zero
};

int main()
{
    Second obj = Second();
    cout << obj.x << endl;
}

This program gives different output when I change it:

  1. If I comment out the line First ob; then value of x is initialised to zero
  2. If the class consists object of class First, then member has garbage value.
  3. If I create object like Second obj; then it has garbage value.

What is the reason it behaves differently when Second class consists only built-in members and when object of another class?

And what is the difference between these statements:

Second obj = Second();
Second obj;
  • 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:55:32+00:00Added an answer on May 29, 2026 at 11:55 pm

    Case 1. When you comment out the line First ob, then Second class becomes a POD type, which can be value-initialized using the syntax Second obj= Second(), and value-initialization, in case of built-in types, means zero-initialized, so x is zero-initialized.

    Case 2: When you keep the line First ob, then Second class becomes non-POD type, because First class is non-POD type (as it has user-defined constructor1]). In this case, the syntax Second obj=Second() doesn’t initialize built-in data type, if you don’t initialize it in the constructor.

    Case 3: When write Second obj, then there are again two cases (read them carefully):

    • obj will be default constructed if there is First ob line. And in this case, x will not be initialized at all, as it is not manually initialized in the constructor of Second.

    • obj will not be initialized at all if you First ob line is commented out. In this case, Second becomes a POD, and POD types are not initialized if you only write Second obj.


    1 See this related topic:

    • Can't C++ POD type have any constructor?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's consider the following code in Java. package obj; final class First { public
Consider this code: class test { public static void main(String[] args) { test inst_test
Consider this snippet of code: public static class MatchCollectionExtensions { public static IEnumerable<T> AsEnumerable<T>(this
Consider this code (Java, specifically): public int doSomething() { doA(); try { doB(); }
Consider this code: int main() { int e; prn(e); return 0; } void prn(double
Consider this code: void res(int a,int n) { printf(%d %d, ,a,n); } void main(void)
Consider the following code: class Rectangle { public: // Constructors Rectangle(){ init(0,0); } Rectangle(int
Consider this syntactically correct(?) pseudocode: class Event { public: virtual int getID() const =
Consider the following code (C# 4.0): public class Foo : LambdaExpression { } This
Consider this small piece of code from apscheduler.scheduler import Scheduler import time class First():

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.