473,378 Members | 1,489 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,378 software developers and data experts.

pure virtual (as in C++)

From what I read, the only similiar thing in C# is declaring a method as
abstract. Problem is I wanted to only declare some methods as "pure".
So I obviously cannot use an abstract modifier in this case. Is there
any ways to accomplish this?

If there is no straightforward way, I was thinking of declaring an
abstract class, creating a subclass that implements all the "non-pure"
methods and then having other classes inherit from this new class
instead of the original abstract. Any better ways?
Nov 16 '05 #1
7 11004
An abstract class can have both abstract and non-abstract methods. So
you can add all your non-pure methods in the same abstract base class
itself, instead of adding one more level to the inheritance hierarchy.

Regards
Senthil
<b>i</b>

Nov 16 '05 #2
Vadim Berezniker wrote:
If there is no straightforward way, I was thinking of declaring an
abstract class, creating a subclass that implements all the "non-pure"
methods and then having other classes inherit from this new class
instead of the original abstract. Any better ways?


Oh doh. What was I thinking. This solution is obviously not gonna work.
Nov 16 '05 #3
sadhu wrote:
An abstract class can have both abstract and non-abstract methods. So
you can add all your non-pure methods in the same abstract base class
itself, instead of adding one more level to the inheritance hierarchy.


I would like to be able to access the method via the base class name.
If I declare it as non-abstract and make a call via the base class name,
the base class's method will be invoked and not the child class'.
Nov 16 '05 #4
Vadim Berezniker wrote:
sadhu wrote:

I would like to be able to access the method via the base class name.
If I declare it as non-abstract and make a call via the base class name,
the base class's method will be invoked and not the child class'.


Oh man. What's wrong with me... I think I need to go to sleep.
I can just mark the method as virtual. Sorry for the dumb questions.
I am not thinking straight.
Nov 16 '05 #5
"Vadim Berezniker" <va***@berezniker.com> wrote in message
news:uo**************@tk2msftngp13.phx.gbl...
Vadim Berezniker wrote:
sadhu wrote:

I would like to be able to access the method via the base class name.
If I declare it as non-abstract and make a call via the base class name,
the base class's method will be invoked and not the child class'.


Oh man. What's wrong with me... I think I need to go to sleep.
I can just mark the method as virtual. Sorry for the dumb questions.
I am not thinking straight.


;D

If you mark the method as abstract, the virtual is implied.

public MyClass {
public abstract void PureMethod(); // Pure
public virtual void VirtualMethod() { // Virtual
}
}
Nov 16 '05 #6
Note that you have to mark the class as abstract if it has any abstract methods or properties

abstract class Foo
{
public abstract void Bar();
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

"Vadim Berezniker" <va***@berezniker.com> wrote in message
news:uo**************@tk2msftngp13.phx.gbl...
Vadim Berezniker wrote:
sadhu wrote:

I would like to be able to access the method via the base class name.
If I declare it as non-abstract and make a call via the base class name,
the base class's method will be invoked and not the child class'.


Oh man. What's wrong with me... I think I need to go to sleep.
I can just mark the method as virtual. Sorry for the dumb questions.
I am not thinking straight.


;D

If you mark the method as abstract, the virtual is implied.

public MyClass {
public abstract void PureMethod(); // Pure
public virtual void VirtualMethod() { // Virtual
}
}

Nov 16 '05 #7
Whoops! Yeah, thanks.

"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
message news:uX**************@TK2MSFTNGP12.phx.gbl...
Note that you have to mark the class as abstract if it has any abstract
methods or properties

abstract class Foo
{
public abstract void Bar();
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

"Vadim Berezniker" <va***@berezniker.com> wrote in message
news:uo**************@tk2msftngp13.phx.gbl...
Vadim Berezniker wrote:
sadhu wrote:

I would like to be able to access the method via the base class name.
If I declare it as non-abstract and make a call via the base class
name,
the base class's method will be invoked and not the child class'.


Oh man. What's wrong with me... I think I need to go to sleep.
I can just mark the method as virtual. Sorry for the dumb questions.
I am not thinking straight.


;D

If you mark the method as abstract, the virtual is implied.

public MyClass {
public abstract void PureMethod(); // Pure
public virtual void VirtualMethod() { // Virtual
}
}

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
6
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
3
by: sudhir | last post by:
I defined a pure virtual function like virtual void sum()=0; <--- pure virtual function but If I defined a virtual function in a base class in case of multilevel inheritance for the base...
10
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a...
7
by: sam_cit | last post by:
Hi Everyone, I wanted to know as to what is the exact difference between a virtual function and a pure virtual function? Thanks in advance!!!
3
by: keith | last post by:
Dear mentors and gurus, I noticed at the end of section 22.4 of the 'FAQ-Lite' it says "Note that it is possible to provide a definition for a pure virtual function, but this usually confuses...
1
by: jinendrashankar | last post by:
Hi All, 1.Please check this code and its output which is working fine while pure virtual functions( FUN1) prototype of Class CA is different then Class CB Function (FUN1). 2. In Class...
14
by: Jack | last post by:
Hi, I meet a question with it , I did not get clear the different betteen them, for example: #include <iostream>
7
by: Tonni Tielens | last post by:
I'm trying to create a pure virtual class describing an interface. Normally, when I do this I make the destructor pure virtual so that, even if there are no members in the class, it cannot be...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.