Link to home
Start Free TrialLog in
Avatar of soodsandeep
soodsandeepFlag for India

asked on

C++ Classes.

hello experts,
i have created the following empty class (i mean without any member data and member functions) , Even an object can be created and the class can be inherited also.
class temp
{
//Nothing Here.
} object1;

class child_temp : public temp
{
//Nothing Here either
} object2;


Experts, Please tell me what is the fun of creating such an empty class. what purpose it can serve. Any appllication or use of it?
waiting..
Thanks.
Sandeep.
ASKER CERTIFIED SOLUTION
Avatar of constructor
constructor

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of constructor
constructor

Regarding inheritance, common base classes in class hierarchies are sometimes empty/abstract classes (no members, no implementation of methods) since these enter at a certain level of specialization only.
well sandeep...right question!!

so you created an empty class; try out for some fun too.

check out this:

cout << "size of temp" << sizeof(temp) << endl;
cout << "size of child_temp " << sizeof(child_temp) << endl;

what do ya guess the answer is? 0? wrong!!!
it's 1; just think of why

make the dtor of the class temp as virtual and check the size again. Find out the justification for the answers you're getting.

Sometimes you may need to have an emply class with a virtual dtor...just come back again if you still have any doubts.

sin
Virtual classes are of huge importance.

Imagine you are creating a billing system for a communications company.
They provide Phone, Email, and Cellular communications.

So you create a general class that is virtual called Communications.
Then you create the Phone, Email and Cellular classes and have them extended to the virtual class.

Now you can pass any one of the three types as Communications.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept constructor's comment as answer.

Please leave any comments here within the next four days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

migoEX
EE Cleanup Volunteer