Thread: doubt in c++ code, urgent please

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    222

    Thumbs up doubt in c++ code, urgent please

    What is the output of following code?
    Code:
    int fn () {return 5;}
    class B {
    private:
             static const int x = fn();
             static const double d = 2.0;
    };
    
    int main ()
    {
    }
    a) x
    b) d
    c) x and d
    d) none

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    stop posting your homework for us to do for you. this isn't that kind of board. Homework Policy

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What do you think, and why?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    222
    Quote Originally Posted by Elysia View Post
    What do you think, and why?
    please assume necessary header files here. I am eager to know the answer here. if I am wrong , correct me with proper logic here.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But you haven't given an answer, so we can't correct you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    222
    i think it's none? what do you say?

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why do you think none is the answer?

    Jim

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    222
    Quote Originally Posted by jimblumberg View Post
    Why do you think none is the answer?

    Jim
    i am not a windows expert, so i leave it to you guys to answer the questions.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you want an answer, then you must give us an answer. We don't require that you be right, only that you state your reasoning. By understanding your reasoning, we can correct it if necessary and help you figure out the answer, thereby giving you more useful information than just an answer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What does being a Windows expert or not have anything to do with this question? This is a question based on standard C++, the answer will be the same if I'm using Windows, OSX, Linux, DOS, or any other operating system. If they have a standard compliant compiler the answer will be the same. This question can easily be answered by compiling the code and observing the output.

    Jim

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    6 new threads in the space of a few hours, all with urgent in the title.
    If these had been your first 6 posts (and not a member with some track record), I would have deleted all of them as some selfish "me me me" prat.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    6 new threads in the space of a few hours, all with urgent in the title.
    Bwahaha!

    I didn't realize they were all by the same game.

    Now my urgent post just looks silly.

    Soma

  13. #13
    Registered User
    Join Date
    Nov 2008
    Posts
    222
    while running with online compiler, i got below error...pls help

    Line 1: error: 'int fn()' cannot appear in a constant-expression
    compilation terminated due to -Wfatal-errors.

  14. #14
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by leo2008 View Post
    while running with online compiler, i got below error...pls help

    Line 1: error: 'int fn()' cannot appear in a constant-expression
    compilation terminated due to -Wfatal-errors.
    Well, now you know what the output of that code is - it's not valid C++ and will not compile.

    And an "online" compiler? I'm not sure how you expect to do programming if you don't even have the most fundamental tool of the trade.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    So you get an error message, does any of your choices include an error message?

    a) x
    b) d
    c) x and d
    d) none
    If you get an error message can a, b, or c ever happen? Remember when you get an error message your program will not run, therefor it produces no output.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Urgent help please with C code
    By Clement Museka in forum C Programming
    Replies: 5
    Last Post: 06-15-2012, 03:50 AM
  2. Explain following code,doubt written in comments
    By ackr1201 in forum C Programming
    Replies: 9
    Last Post: 07-08-2011, 04:17 PM
  3. Doubt regarding C++ code in old textbooks
    By thefeedinghand in forum C++ Programming
    Replies: 4
    Last Post: 08-30-2010, 03:43 PM
  4. Code Doubt?
    By shwetha_siddu in forum C Programming
    Replies: 1
    Last Post: 07-21-2008, 01:02 AM
  5. doubt in C--(reducing the length of code)
    By nardneran in forum C Programming
    Replies: 44
    Last Post: 10-15-2006, 02:38 PM