473,326 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How to prevent a class from inheritance in C++?

Hi Everybody,

How can I prevent my class from inheritance in C++?.

There are three ways which I found in some sites->
1. Using Named constructor
2. Using friend function
3. Commant appraoch.

I am not very much clear about this. So anybody can give answer of this with examples.

I would like to thank in advance.

With Kind Regards,
Devendra
Oct 27 '06 #1
2 9302
To block class from inheritance, you can define you class as final:

Expand|Select|Wrap|Line Numbers
  1. public final MyClass 
  2. {
  3. }
  4.  
that way, if someone write:

Expand|Select|Wrap|Line Numbers
  1. public OtherClass : MyClass
  2. {
  3. }
  4.  
Then, the compiler will give an error.
Oct 27 '06 #2
Sorry, my bad....the final keyword is only present in Java.

In C++, you can write your constructor of the base class private.
Here is an example:

http://www.devx.com/tips/Tip/29062
Oct 27 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Ricky Romaya | last post by:
Hi, Are there any ways to get multiple inheritace in PHP4? For example, I have 3 parent class, class A, B, and C. I want class X to inherit all those 3 classes. Consider merging those 3 classes...
8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
4
by: KInd | last post by:
Hello All, When is nested class more preferable that Inheritance ? I think with proper inheritance and friend class concept we can get the same flexibility as nested classes Any comments .. Best...
20
by: modemer | last post by:
Question is as in subject. For example: class BaseClass { public: void func() { do something; } // I don't want this function being overloaded in its inherited class };
6
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a...
3
by: Tony Maresca | last post by:
Hi. I have a class derived from a UserControl, that I want to allow others to derive controls from. I don't want them to design the base class (which is derived from a UserControl). I know that...
7
by: Kyle M. Burns | last post by:
I have an application where I needed to refactor to a base class using the template method pattern in order to avoid duplication of code between two objects that needed to fulfill the same...
4
by: tony | last post by:
Hello! If I want to prevent creation of object for a class. I can accomplish this by using different technique. One way of doing this is to define the class to be abstract even if all the...
3
by: hurcan solter | last post by:
Consider the code fragment; class A { public: A(){} A(int prm):mprm(prm){} int mprm; }; class B:public A {
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.