Solved

C++ Classes.

Posted on 2003-12-04
6
303 Views
Last Modified: 2010-04-01
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.
0
Comment
Question by:soodsandeep
6 Comments
 
LVL 2

Accepted Solution

by:
constructor earned 100 total points
ID: 9880818
There isn't much fun in creating such a class. The things you can accomplish thereby can be covered in various other ways that are at least as intuitive.

But if you're just looking for any application at all, sure there are some. The class still has an attribute: its type; and the object has its identity: the address. These can be utilized in application.

In function/operator overloading, you can use the type to resolve the ambiguity and achieve different functionalities (here illustrated on the constructor of a class):

class Graph
{
public:
  class Directed { };
  class Symmetric{ };
  Graph(Directed) { /* Whatever implementation... */ }
  Graph(Symmetric) { /* Whatever implementation... */ }
  /* Whatever remaining implementation... */
};

In a small game, you can keep track of the active player using the address of objects (null meaning the game is over):

class Player { } one,two;
Player *active=&one;
while(active!=0)
{
  if(active==&one)
  {
    // Handle player 1...
  }
  else
  {
    // Handle player 2...
  }
}
0
 
LVL 2

Expert Comment

by:constructor
ID: 9880836
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.
0
 
LVL 2

Expert Comment

by:sin_
ID: 9881173
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
0
 
LVL 7

Expert Comment

by:jj819430
ID: 9884208
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.
0
 
LVL 5

Expert Comment

by:migoEX
ID: 10443496
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now