Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

How would you stop a class from class from being derived or
inherited?The constructer should not be Private,as object
instantiation should be allowed.

Answer Posted / boriska

OK, here is the piece of code which illustrates the concept
=======================================================
class Base
{
private :
char _id;
~Base() {};
public :
Base(char id='0') : _id(id) {};
static Base* CreateBase(char id) { return new Base(id); }
static void DestroyBase(Base* ptr) { delete ptr; }
};

/**/
class Derived : public Base
{
public :
Derived(char id='1') : Base(id) {};
~Derived() {};
};
/**/

int main(int argc, char** argv)
{

Base* pb = Base::CreateBase('B');
Base::DestroyBase(pb);
return 0;
}
=================================================

I should have added that since Base class declares
destructor private, it cannot be instantiated directly
either, but I though it is kinda self-evident. My apologies
- just typed quick answer, describing a concept.

So here you go : you need static create/destroy functions,
declared within the scope of the Base class (or friend
functions, declared globally).

Now the major point : no matter how you try, declaration of
class Derived will not pass compilation - unless you define
similar create/destroy methods for class Derived within
Base class scope - which you, as a designer, choose not to
do. Here is the compiler error, produced at line with "void
Derived::~Derived()" declaration (compiled in .NET) :
==========================================
1>.\Test.cpp(46) : error C2248: 'Base::~Base' : cannot
access private member declared in class 'Base'
1> .\Test.cpp(35) : see declaration of 'Base::~Base'
1> .\Test.cpp(32) : see declaration of 'Base'
======================================

If you still in doubt - try this piece of code for yourself.
If you comment out declaration of class Derived, code
compiles just fine.

Now a bit of personal advise for Mms Zubeir : in the future,
before calling other's people opinion a "junk", try to give
it a little thought...and by extension, benefit of doubt to
your own self-assured opinion. This attitude may look good
on interview for some junior/mid-level position - for some
dumb hiring manager, grown on Scott Meyers/C++ in 21 day
type of books :-) But, it will not earn you much respect
amongst professionals. Arrogance is usually sure sign of
ignorance and lack of confidence...never convincing, and
rather good cause for laughter instead. Please remember that
next time before opening your mouth in attempt to discredit
other people's views.

Is This Answer Correct ?    3 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is null and void pointer?

1043


Write a program using display() function which takes two arguments.

1047


Why we use #include conio h in c++?

1025


what are the iterator and generic algorithms.

1952


How come you find out if a linked-list is a cycle or not?

1021


Why is c++ not purely object oriented?

993


Who discovered c++?

1032


Can I learn c++ without learning c?

995


What is exception handling? Does c++ support exception handling?

1019


What is the error in the code below and how should it be corrected?

749


Reads in the size of a square from the screen; 2. Prints a hollow square of that size out of “-“, “|” and blanks on screen; 3. Prints the same hollow square onto a text file. Your program should work for squares of all side sizes between 1 and 20. --- │ │ │ │ │ │ ---

2091


What data encapsulation is in c++?

1127


By using c++ with an example describe linked list?

1015


What size is allocated to the union variable?

1044


What is array in c++ example?

1100