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.
11 replies to this topic
#1
Posted 07 January 2010 - 08:06 AM
#2
Posted 07 January 2010 - 08:22 AM
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
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
Posted 07 January 2010 - 08:35 AM
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.
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
Posted 07 January 2010 - 11:31 AM
then why the size of object of Class A {}; is not zero??
#5
Posted 07 January 2010 - 12:59 PM
booo said:
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
Posted 07 January 2010 - 01:11 PM
tech1978 said:
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).
#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
#7
Posted 07 January 2010 - 01:25 PM
not sure but that is because probably VS calculated float as 8 bytes. just check size of float.
#8
Posted 08 January 2010 - 05:01 AM
tech1978 said:
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 :tongue:
#9
Posted 08 January 2010 - 06:52 AM
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
Posted 08 January 2010 - 04:58 PM
Why are we counting the size of data members that are private, so won't be inherited?
#11
Posted 09 January 2010 - 07:11 PM
Abh1shek said:
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
Posted 06 February 2010 - 07:54 PM
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)
Sign In
Create Account
Back to top









