How would you stop a class from class from being derived or
inherited.
Answers were Sorted based on User's Feedback
Answer / satya
By Declaring the Class as 'Sealed' in C# or
as "NotInheritable" in VB.NET
| Is This Answer Correct ? | 8 Yes | 3 No |
Answer / deepak joshi
Guys what happened to you all???? Will 'Final' not do????
| Is This Answer Correct ? | 8 Yes | 4 No |
Answer / stavanm
By Creating class as a final
final class A{
// code
}
| Is This Answer Correct ? | 5 Yes | 1 No |
#define DO_NOT_DERIVE(T) \
class NoDerive_##T { \
friend class T;
\
NoDerive_##T() {}
\
NoDerive_##T(const NoDerive_##T&) {} \
};
#define stopderiving(T) private virtual NoDerive_##T
when we derive from any class, the constructor needs to be
called, since here constructor of NoDerive calss is
private, it cannot be derived. And friendship cannot be
inherited.
usage
class finalclass : stopderiving (finalclass)
{
....
};
the other possible solution for this is making the default
constructor private.
| Is This Answer Correct ? | 4 Yes | 1 No |
Answer / bala
Final will do in Java. But not in C++.
I hope, bye creating all the constructor,copy constructor
in private will stop inherite the class.
Plz refer 'Singleton Pattern'
| Is This Answer Correct ? | 1 Yes | 1 No |
Answer / selvam
but onething.If you make constructor as private you will
not be able to create an object of that class.
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / montylee
Here's the correct answer:
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.11
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / anant tiwari
There are three way to stop a class been derived (example in C#)
1) declare a class as static (Ex : static class Sample {....}
2) declare the class constructor as private (Ex class Sample{ private Sample(){}....}
3) declare a class as sealed class Ex : sealed class Sample {...}
| Is This Answer Correct ? | 0 Yes | 0 No |
In c++ there is only virtual destructors, no constructors. Why?
what is the definition of incapsulation
what is the use of classes in c++;
What is difference between data abstraction and encapsulation?
Why is abstraction used?
What is static modifier?
What is debug class?what is trace class? What differences are between them? With examples.
When you define a integer it gets stored in which data structure?(Stack or a heap)
What is meant by multiple inheritance?
1.explicit call for destructor 2.calling function inside a constructor. 3.base *b-new derived delete b; 4.delete p what it will delete. 5.size of base class and derived class int i,in base class and int j in derived. 6.int i-20 int main() { int i =5; printf("%d".::i); { int i =10; printf("%d".::i); } } 7.object slicing 8.new 9.function overloading(return type). 10.class base() { virtuval fun() { ----- } } class derivied:public base() { fun() { ----- } } int main() { derived d; } 11.how static function will call in C++? 12.default structures are in C++? 13.constructors should be in public . 14.virtuval constructor not exist. 15.multilevel inhritence. destructor order.
can you give real time example for polymarphism
Program to check whether a word starts with a capital letter or not.