Welcome to TechEnclave



+ Reply to Thread
Results 1 to 12 of 12

Thread: C++ problems

  1. C++ problems

    #1
    is Offline Hyperactive Member Abh1shek is Mysterious
    Join Date: Jan 2009
    Posts: 84
    Error

    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. Re: C++ problems

    #2
    is Offline Kickass Member Techman is very Respectable Techman is very Respectable Techman is very Respectable
    Join Date: Mar 2007
    Location: Not anymore in Hyderabad
    Posts: 349
    Default

    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
    Last edited by Techman; 7 Jan 10 at 02:37 PM.
  3. Re: C++ problems

    #3
    is Offline Fanatic Member pr0ing is very Notable pr0ing is very Notable
    Join Date: Sep 2006
    Location: Mumbai
    Age: 29
    Posts: 150
    Default

    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.
  4. Re: C++ problems

    #4
    booo's Avatar
    is Offline Steroids Member booo is very Notable booo is very Notable booo is very Notable
    Join Date: Mar 2008
    Location: Hyderabad
    Posts: 384
    Default

    then why the size of object of Class A {}; is not zero??
  5. Re: C++ problems

    #5
    is Offline Junior Member tech1978 is Mysterious
    Join Date: Jul 2008
    Posts: 45
    Default

    Quote Originally Posted by booo View Post
    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. Re: C++ problems

    #6
    booo's Avatar
    is Offline Steroids Member booo is very Notable booo is very Notable booo is very Notable
    Join Date: Mar 2008
    Location: Hyderabad
    Posts: 384
    Default

    Quote Originally Posted by tech1978 View Post
    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
  7. Re: C++ problems

    #7
    is Offline Junior Member tech1978 is Mysterious
    Join Date: Jul 2008
    Posts: 45
    Default

    not sure but that is because probably VS calculated float as 8 bytes. just check size of float.
  8. Re: C++ problems

    #8
    booo's Avatar
    is Offline Steroids Member booo is very Notable booo is very Notable booo is very Notable
    Join Date: Mar 2008
    Location: Hyderabad
    Posts: 384
    Default

    Quote Originally Posted by tech1978 View Post
    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
  9. Re: C++ problems

    #9
    haraakiri's Avatar
    is Offline Overloaded Member haraakiri is very Respectable haraakiri is very Respectable haraakiri is very Respectable
    Join Date: May 2009
    Location: Hyderabad
    Age: 30
    Posts: 577
    Default

    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.
  10. Re: C++ problems

    #10
    is Offline Hyperactive Member Abh1shek is Mysterious
    Join Date: Jan 2009
    Posts: 84
    Default

    Why are we counting the size of data members that are private, so won't be inherited?
  11. Re: C++ problems

    #11
    ashvarybabul's Avatar
    is Offline Deeply Inhaled ashvarybabul is Notable
    Join Date: Aug 2009
    Location: Jabalpur
    Posts: 364
    Default

    Quote Originally Posted by Abh1shek View Post
    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..
  12. Re: C++ problems

    #12
    is Offline Fanatic Member shantanugoel is Notable
    Join Date: Dec 2008
    Posts: 169
    Default

    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)


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts