Results 1 to 12 of 12

Thread: C++ problems

  1. #1
    Abh1shek is offline Upgraded User
    Join Date
    Jan 2009
    Posts
    88

    C++ problems

    Have a computer science exam tomorrow, need help with a couple of questions



    Q. If n given by user is 15, what maximum and minimum values the program can display?



    void main()

    {

    int n, guessme;

    randomize();

    cin >> n;

    guesme = random(n) + 10;

    cout << guessme;

    }



    Answer is 24, and 10. I need the explanation.



    Q. How many bytes will be required by an object belonging to class AUTHOR?



    class PUBLISHER

    {

    char Pub[12];

    double Turover;

    protected:

    void Register();

    public:

    PUBLISHER();

    void Enter();

    void Display();

    };



    class BRANCH

    {

    char CITY [20];

    protected:

    float Employees;

    public:

    BRANCH();

    void Haveit();

    void Giveit();

    };



    class AUTHOR: private BRANCH, publuc PUBLISHER

    {

    int Acode;

    char Aname[20];

    float Amount;

    public:

    AUTHOR();

    Void Start();

    Void Show();

    };



    Answer is 70, just need the explanation.



    Thanks a lot.

  2. #2
    Techman is offline Upgraded User
    Join Date
    Mar 2007
    Posts
    333

    Re: C++ problems

    Let me try



    Ans to Q1:

    - Random(n) will range from 0 to 14

    - After adding 10 guesme = random(n) + 10; to it the max value can be 24 (14 + 10) and the min value can be 10 (0 + 10)



    Ans to Q2

    I am not sure how it ended up with 70, it must be 72



    char Pub[12]; takes 12 bytes

    double Turover; takes 8 bytes

    char CITY [20]; takes 20 bytes

    float Employees; takes 4 bytes

    int Acode; takes 4 bytes

    char Aname[20]; takes 20 bytes

    float Amount; takes 4 bytes



    As the author class inherits branch and publisher, variables are also part of it. Hence the size of Author should be



    12 + 8 + 20 + 4 + 4 + 20 + 4 = 72



    Edit: My bad, integer -32768 to 32767 range should consume only 2 byte. Hence 12 + 8 + 20 + 4 + 2 + 20 + 4 = 70

  3. #3
    pr0ing is offline Privileged Users
    Join Date
    Sep 2006
    Posts
    409

    Re: C++ problems

    1.



    random(n) will return a positive random value between 0 to n-1.

    If 15 is entered then the minimum returned value is 0 while the maximum returned value is 14.

    add 10 to those values and you get your answer.





    2.



    an object of the class occupies space for all variables defined in it's definition.

    These are the number of bytes allocated to each variable type.

    char - 1byte.

    int - 2bytes.

    float - 4 bytes.

    double - 8 bytes.

    these are typical values for most compilers but can vary for different architectures.



    object sizes for your classes.



    class PUBLISHER

    {

    char Pub[12]; -12bytes

    double Turover; - 8bytes

    protected:

    void Register();

    public:

    PUBLISHER();

    void Enter();

    void Display();

    };



    class BRANCH

    {

    char CITY [20]; -20 bytes

    protected:

    float Employees; -4 bytes

    public:

    BRANCH();

    void Haveit();

    void Giveit();

    };



    class AUTHOR: private BRANCH -24 bytes, publuc PUBLISHER -20 bytes

    {

    int Acode; -2 bytes

    char Aname[20]; -20 bytes

    float Amount; -4 bytes

    public:

    AUTHOR();

    Void Start();

    Void Show();

    };





    add them all up and you get a value of 70 for an object of the class author.
    -under construction-

  4. #4
    booo is offline Privileged Users
    Join Date
    Mar 2008
    Posts
    488

    Re: C++ problems

    then why the size of object of Class A {}; is not zero??
    Phenom 9500:MSI K9A2 Platinum:Corsair XMS2 4X1GB DDR2 PC6400:ATI Radeon 3870X2:Corsair HX620:Creative Inspire T6060:Creative EP-630: Logitech MX518: Segate 250GB:WD MyBook Essential 500GB:ViewSonic VG1930wm:Nokia 5800XM:Canon 300D

  5. #5
    tech1978 is offline Upgraded User
    Join Date
    Jul 2008
    Posts
    77

    Re: C++ problems

    Quote Originally Posted by booo
    then why the size of object of Class A {}; is not zero??


    that is bcoz to distinguish one object frm another. As class is empty and u can create multiple objects of that class then one object should be different from another which will occupy minimum byte which is 1 bite. in short object will have atleast 1 byte size (sub objects can have zero size).

  6. #6
    booo is offline Privileged Users
    Join Date
    Mar 2008
    Posts
    488

    Re: C++ problems

    Quote Originally Posted by tech1978
    that is bcoz to distinguish one object frm another. As class is empty and u can create multiple objects of that class then one object should be different from another which will occupy minimum byte which is 1 bite. in short object will have atleast 1 byte size (sub objects can have zero size).


    Code:
    #include <stdio.h>
    
    class PUBLISHER
    
    {
    
        char Pub[12];
    
        double Turover;
    
        protected:
    
        void Register();
    
        public:
    
        PUBLISHER();
    
        void Enter();
    
        void Display();
    
    };
    
    
    
    class BRANCH
    
    {
    
        char CITY [20];
    
        protected:
    
        float Employees;
    
        public:
    
        BRANCH();
    
        void Haveit();
    
        void Giveit();
    
    };
    
    
    
    class AUTHOR: private BRANCH, public PUBLISHER
    
    {
    
    int Acode;
    
    char Aname[20];
    
    float Amount;
    
    public:
    
    AUTHOR();
    
    void Start();
    
    void Show();
    
    };
    
    int main()
    
    {
    
        printf("%d", sizeof (AUTHOR));
    
    }
    hmm... I wrote this program in VS2008 and the ouput I got was 80:S
    Phenom 9500:MSI K9A2 Platinum:Corsair XMS2 4X1GB DDR2 PC6400:ATI Radeon 3870X2:Corsair HX620:Creative Inspire T6060:Creative EP-630: Logitech MX518: Segate 250GB:WD MyBook Essential 500GB:ViewSonic VG1930wm:Nokia 5800XM:Canon 300D

  7. #7
    tech1978 is offline Upgraded User
    Join Date
    Jul 2008
    Posts
    77

    Re: C++ problems

    not sure but that is because probably VS calculated float as 8 bytes. just check size of float.

  8. #8
    booo is offline Privileged Users
    Join Date
    Mar 2008
    Posts
    488

    Re: C++ problems

    Quote Originally Posted by tech1978
    not sure but that is because probably VS calculated float as 8 bytes. just check size of float.


    size of float is 4. I know the answer but I am just messing with you guys
    Phenom 9500:MSI K9A2 Platinum:Corsair XMS2 4X1GB DDR2 PC6400:ATI Radeon 3870X2:Corsair HX620:Creative Inspire T6060:Creative EP-630: Logitech MX518: Segate 250GB:WD MyBook Essential 500GB:ViewSonic VG1930wm:Nokia 5800XM:Canon 300D

  9. #9
    haraakiri is offline Privileged Users
    Join Date
    May 2009
    Posts
    786

    Re: C++ problems

    It's important for any c++ programmer to know structure padding. I request all to either use gcc and/or visual c++ rather than age old compilers/ides.
    i5 2500K | Z68XP-UD3R | 2X4GB SNIPER CL9 1600 | GTX 460 768 | 96GB SSD + 2X1TB Seagate | HAF X | 2311H | G9 | Reclusa | Xonar DX | MX5021

    Clip+ 8GB | Fuze 4GB | iPod 5.5G+FiiO E1 | Audio GD Sparrow B | uDac | Beyer 888 600 Triple.Fi 10 | RE1 | M1 | RS130

    LG Optimus 2X | ASUS Transformer | Inspiron 9400 | XBox 360 S

  10. #10
    Abh1shek is offline Upgraded User
    Join Date
    Jan 2009
    Posts
    88

    Re: C++ problems

    Why are we counting the size of data members that are private, so won't be inherited?

  11. #11
    ashvarybabul is offline Privileged Users
    Join Date
    Aug 2009
    Location
    Jabalpur
    Posts
    740

    Re: C++ problems

    Quote Originally Posted by Abh1shek
    Why are we counting the size of data members that are private, so won't be inherited?


    The private members do get inherited [in a way] but goes to invisible section...

    That is, they cannot be used any how by the derived class - neither by function nor by object...



    Thus in inheritance U need to add size data members of all scope defined of base class when calculated for derived class.. whether they are public private or protected..
    Flickr



    Time you enjoyed wasting is not wasted time.

  12. #12
    shantanugoel is offline Upgraded User
    Join Date
    Dec 2008
    Posts
    282

    Re: C++ problems

    For random number question, if you want to learn a bit more than just getting your question answered, take a look at this: Random Number Between Two Integers | Safer Code - Secure Coding In C C++ And More.. (Also read the comments which are quite informative)

 

 

Similar Threads

  1. XP problems
    By PrancerTran in forum Operating Systems
    Replies: 2
    Last Post: 16-06-09, 11:10 AM
  2. is this going to cause any problems?
    By i_max2k2 in forum Graphic Cards
    Replies: 14
    Last Post: 08-08-08, 09:10 AM
  3. Have few problems !
    By deep_nx in forum PC Peripherals
    Replies: 8
    Last Post: 03-12-07, 02:41 PM
  4. OC Problems
    By techcheat in forum Overclocking 'n' Modding
    Replies: 2
    Last Post: 28-11-06, 12:01 AM
  5. Problems, Problems, Damn !
    By Anish in forum Overclocking 'n' Modding
    Replies: 3
    Last Post: 21-06-05, 09:29 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT +5.5. The time now is 09:04 PM. Powered by vBulletin® Version 4.1.12
Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.
Content Relevant URLs by vBSEO 3.6.0 PL2