Thread: C++ problems
- 7 Jan 10 01:36 PM #1is Offline Hyperactive Member
Join Date: Jan 2009Posts: 84
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. - 7 Jan 10 01:52 PM #2is Offline Kickass Member
Join Date: Mar 2007Location: Not anymore in HyderabadPosts: 349
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 = 70Last edited by Techman; 7 Jan 10 at 02:37 PM.
- 7 Jan 10 02:05 PM #3is Offline Fanatic Member
Join Date: Sep 2006Location: MumbaiAge: 29Posts: 150
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. - 7 Jan 10 05:01 PM #4
then why the size of object of Class A {}; is not zero??
- 7 Jan 10 06:29 PM #5is Offline Junior Member
Join Date: Jul 2008Posts: 45
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).
- 7 Jan 10 06:41 PM #6
hmm... I wrote this program in VS2008 and the ouput I got was 80Code:#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)); }
- 7 Jan 10 06:55 PM #7is Offline Junior Member
Join Date: Jul 2008Posts: 45
not sure but that is because probably VS calculated float as 8 bytes. just check size of float.
- 8 Jan 10 10:31 AM #8
- 8 Jan 10 12:22 PM #9
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.
- 8 Jan 10 10:28 PM #10is Offline Hyperactive Member
Join Date: Jan 2009Posts: 84
Why are we counting the size of data members that are private, so won't be inherited?
- 10 Jan 10 12:41 AM #11
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.. - 7 Feb 10 01:24 AM #12is Offline Fanatic Member
Join Date: Dec 2008Posts: 169
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)
LinkBacks (?)
- 18 Mar 10, 03:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

