I have some doubts concerning some basic C++ concepts, and would be very thankful, if someone would kindly clear the doubts:

a) Every variable of a data type occupies a specific amount of memory, like int occupies 2 bytes and so on. So, if I declare a reference variable as,

int &y = int x;

How much memory does the reference variable occupy?

b) Suppose I have a structure named "person", like:

struct person
{
   //Data members...
}

and I also have a class named "person", in the same program, like:

class person
{
   //Class members
}

and then if I declare person p , will a "person" type variable be created, or will an object of the "person" class be created?

Again thanks in advance.

>>like int occupies 2 bytes

Wow, how old is your computer? Usually, on a 32bit system, an int occupies 4 bytes (and 8 bytes on a 64bit system). For an int to occupy 2 bytes, your system must be pretty old (or very special).

>>How much memory does the reference variable occupy?

Usually, the same as a pointer. But using sizeof() on it will return the size of the type of the variable it refers to, not the size of the reference variable itself.

>>Suppose I have a structure named "person"
>>and I also have a class named "person"

Your compiler should throw a compilation error for multiple declaration of "person". In C++, there is only very trivial differences between "struct" and "class". The "struct" keyword was kept for compatibility with C, but it really wouldn't be needed since both "class" and "struct" are almost exactly the same in C++. The only trivial difference is the default access rights and default inheritance. Besides that, they are essentially interchangeable keywords. And declaring two types (whether it is struct, class, enum, typedef, whatever..) with the same name is an error, it breaks ODR (One-Definition-Rule).

a) In a 32-bit system, a reference variable needs at least 4 bytes of memory and in a 64-but system, it needs at least 5 bytes. That's because a reference points to the memory of the variable its pointing to. Thus it needs only enough memory to store a memory address.

b) It will be a compile error because you have two of the same identifier/variable name.

>>like int occupies 2 bytes

Wow, how old is your computer? Usually, on a 32bit system, an int occupies 4 bytes (and 8 bytes on a 64bit system). For an int to occupy 2 bytes, your system must be pretty old (or very special).

Actually, my computer isn't old, the book that I was reading was old(kind of) - it even has instructions on compiling .cpp files on DOS and Unix.LOL:D
Nevertheless, thanks to everyone for helping!

Actually, my computer isn't old, the book that I was reading was old(kind of) - it even has instructions on compiling .cpp files on DOS and Unix.LOL:D
Nevertheless, thanks to everyone for helping!

Looks like you need to update the book you read from. From googling the first link seems good for free C++ books online. Check it out here

@firstPerson,
Thanks for your suggestion.
By the way, the book I was using was published in 2008, so how come it has so old instructions???

Hmmm...looks like it may just have been a reprint of an old book.

By the way, the book I was using was published in 2008, so how come it has so old instructions???

What book is it? Most likely it's on the "not recommended" list.

What book is it? Most likely it's on the "not recommended" list.

Who cares? I had just used for a week.
Have dumped it now.

Who cares? I had just used for a week.
Have dumped it now.

Just curiosity.

Who cares? I had just used for a week.

You're not the center of the universe, dude. I care because I can use that information when helping other people.

@Narue,
Well, if you insist, the book was maybe a study book I had to buy when I had taken a beginner's course in c++, from an institution (can't remember the name).

It probably wasn't for sale to the public, and moreover, if you need that information for helping people, then you'll probably find much better and useful alternatives online.

@Narue,
Well, if you insist, the book was maybe a study book I had to buy when I had taken a beginner's course in c++, from an institution (can't remember the name).

It probably wasn't for sale to the public, and moreover, if you need that information for helping people, then you'll probably find much better and useful alternatives online.

You're curiously reluctant and defensive about this. But fine, if it means so much that you don't name the book, I'll stop asking. :icon_rolleyes:

You're curiously reluctant and defensive about this. But fine, if it means so much that you don't name the book, I'll stop asking. :icon_rolleyes:

No! What I said in my last post really was true, I really don't remember the name of the institution from where I had bought it! Jeez!
And by the way, what would you do with a book full of non-standard c++ programs:?:???

[EDIT]
Heyyy, what was that negative rep for ???!!!

Assume simple classes representing a student profile.it consist of emp-id,name,branch-code,category.the name will be of any length.create a file called std.txt and write the objects of the class student into the file.implement functions to do the searching in the based on emp-id and display information.use exception handling mechanism for the following members emp-id:four digit with 1st character and rest 3 numeric

please clarify my doubt as soon as possible!!!

commented: We don't do HOMEWORK!!! -3

Please, don't dig up old threads by posting unrelated questions in them - start a new thread with a title appropriate to you question. Also, you're 'explanation' of the problem is opaque at best.

class X
{
public :
void f()
{
cout<<"hello";
}
};

int main()
{
X* ptr;
ptr->f();
return 0;
}

Question : How can we access class function with just its class pointer??
Surprisingly /it prints hello
Do any oe have details regarding dis concept ??

the method f() is called, but it's "this" pointer is undefined because ptr was not initialized.

The function call to f() is determined at compile time according to the type X.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.