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

Problem with abstract classes

Hello Guys

I'm a C++ programmer moving to C# and I'm trying to do something which
I regularly do in C++ and am wondering why it doesn't work in C#.

Basically in C++ I can do something similar to the following

class A{
public:
virtual void foo()=0;
};

class B : public A{
public:
virtual void foo()=0;
};

class C : public B{
public:
virtual void foo(){
cout << "Test";
}
};

Where class B and A are abstract classes with a pure virtual function
"foo"

If I try the equivalent in C#

public abstract class A {
public abstract void foo();
}
public abstract class B : A {
public abstract void foo();
}
public class C : B {
public override void foo() {
return "Hello Sailor";
}
}

I get....

error CS0533: 'B.foo()' hides inherited abstract member 'A.foo()'
error CS0534: 'C' does not implement inherited abstract member
'A.foo()'

Is this inheritance structure not valid in C# or am I missing
something.

Regards,

Fergal

Feb 24 '06 #1
3 10006
Try using the override keyword on the function definition in B.

Fergal Moran wrote:
Hello Guys

I'm a C++ programmer moving to C# and I'm trying to do something which
I regularly do in C++ and am wondering why it doesn't work in C#.

Basically in C++ I can do something similar to the following

class A{
public:
virtual void foo()=0;
};

class B : public A{
public:
virtual void foo()=0;
};

class C : public B{
public:
virtual void foo(){
cout << "Test";
}
};

Where class B and A are abstract classes with a pure virtual function
"foo"

If I try the equivalent in C#

public abstract class A {
public abstract void foo();
}
public abstract class B : A {
public abstract void foo();
}
public class C : B {
public override void foo() {
return "Hello Sailor";
}
}

I get....

error CS0533: 'B.foo()' hides inherited abstract member 'A.foo()'
error CS0534: 'C' does not implement inherited abstract member
'A.foo()'

Is this inheritance structure not valid in C# or am I missing
something.

Regards,

Fergal

Feb 24 '06 #2
public abstract class B : A
{
public *override* abstract void foo();
}
Fergal Moran wrote:
Hello Guys

I'm a C++ programmer moving to C# and I'm trying to do something which
I regularly do in C++ and am wondering why it doesn't work in C#.

Basically in C++ I can do something similar to the following

class A{
public:
virtual void foo()=0;
};

class B : public A{
public:
virtual void foo()=0;
};

class C : public B{
public:
virtual void foo(){
cout << "Test";
}
};

Where class B and A are abstract classes with a pure virtual function
"foo"

If I try the equivalent in C#

public abstract class A {
public abstract void foo();
}
public abstract class B : A {
public abstract void foo();
}
public class C : B {
public override void foo() {
return "Hello Sailor";
}
}

I get....

error CS0533: 'B.foo()' hides inherited abstract member 'A.foo()'
error CS0534: 'C' does not implement inherited abstract member
'A.foo()'

Is this inheritance structure not valid in C# or am I missing
something.

Regards,

Fergal

Feb 24 '06 #3
Thanks a log guys - perfect.
As also pointed out on another newsgroup - I could also have left out
the definition in class B completely.

Fergal.
Glad no-one noticed a void function returning a string :)

Feb 24 '06 #4

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

Similar topics

3
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit...
12
by: Daedalus.OS | last post by:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of you. If the only answer you can provide is "get a book about OOP" then don't loose your time and mine cause it's already...
2
by: Dave Veeneman | last post by:
Is is legal to declare abstract members in non-abstract classes? How about non-abstract members in abstract classes? I am writing a base class with three derived classes. The base class will...
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
12
by: scottt | last post by:
hi, I am having a little problem passing in reference of my calling class (in my ..exe)into a DLL. Both programs are C# and what I am trying to do is pass a reference to my one class into a DLL...
7
by: ankitjain.bvcoe | last post by:
Hi i have the following problem in my design :::: i want to define an abstract class LogBuffer and derive two singleton classes from it i.e AlarmBuffer and FireWallBuffer.For this my design is...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
0
by: Kristopher Wragg | last post by:
Hi, I'm having a problem with XMLSerializer, I have classes like the following: public abstract class EulerObject { private Rectangle rect; private string guid = "";
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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: 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...
0
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.