473,799 Members | 3,866 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
2 1944
"Fergal Moran" <fe**********@g mail.com> a écrit dans le message de news:
11************* *********@i40g2 00...legr oups.com...

| 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.

You are not quite getting the syntax right. you don't need to redeclare the
abstract method in B.

public abstract class A
{
public abstract void foo();
}

public abstract class B : A
{
}

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

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Feb 24 '06 #2
You are completely correct Joanna thanks a lot - I also could have used

public override abstract string foo();

in class B - which to appears a bit closer to what I am used to...

Thanks for the reply

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

Feb 24 '06 #3

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

Similar topics

3
3152
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 from it. During run time, using a static method, the class creates an instance of the derived class using the Activator class and returns it. This design pattern is very similar to the design pattern applied by the Assembly class. The twist is...
12
3099
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 ordered. I'm just too curious about this one to wait for the book. I would like to know is if it's good php programming practice to use abstract classes instead of singleton classes. For exemple a login class. I've made one as an abstract class...
2
9609
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 define the behavior for most, but not all of its members. The derived classes will define the behavior for the remaining members (the undefined members). I'd like to force the derived classes to implement the undefined members in the base class. I...
9
5202
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
3027
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 function. When I try and compile the DLL I get "The type or namespace name "MyForm" could not be found. I think I have to reference the class but since the DLL needs to be built before the EXE it looks like I have a chicken and egg type problem....
7
1807
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 such that i have to define data members in class LogBuffer.i.e ************************************************************************************ class LogBuffer
7
4476
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 to be checking that the correct data type is used DataAcess sets an abstract class and methods
0
2839
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 magic, I wrote the following module. It is mainly useful as a light weight tool to help programmers catch mistakes at definition time (e.g., forgetting to implement a method required by the given interface). This is handy when unit tests or...
0
2798
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
3019
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 C# even has Iinterfaces. I took some Java programming classes and Interfaces are a main staple of Java. And in VB.Net I use interfaces for setting up definitions of classes. I am guessing that Abstract classes in C# do the same thing as...
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10488
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10237
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10029
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7567
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.