5

Hi i'm new in C# console application and i'm using abstract and override but i get stack in the first method in public abstract double Compute() i got an error and it says cannot declare a body because it is marked abstract please help me. thank you!

`

abstract class Cake
    {
        public string _flavor, _size;
        public int _quantity;

        public Cake(string flavor, string size, int quantity)
        {
            _flavor = flavor;
            _size = size;
            _quantity = quantity;
        }

        public abstract double Compute()
        {
            double price;
            if(_flavor == "Chocolate" && _size == "Regular")
            {
               price = 250.50;
            }
            else if (_flavor == "Chocolate" && _size == "Large")
            {
                price = 450.50;
            }
            else if (_flavor == "Strawberry" && _size == "Regular")
            {
                price = 300.50;
            }
            else
            {
                price = 500.75;
            }
            return price;
        }
    }

    class BirthdayCake:Cake
    {
        public int _numOfCandles;

        public BirthdayCake(string flavor, string size, int quantity, int numOfCandles):base(flavor,size,quantity)
        {
            _numOfCandles = numOfCandles;
        }

        public override double Compute()
        {
            return _numOfCandles * 10.00;
        }
    }`
Yas Smitch
  • 269
  • 2
  • 13
  • 3
    Use *virtual* instead of *abstract* when you have a default implementation but would like to allow sub-classes to override. – Pieter Geerkens Nov 17 '17 at 14:03
  • https://stackoverflow.com/questions/14728761/difference-between-virtual-and-abstract-methods – juharr Nov 17 '17 at 14:04
  • If you declare an abstract function in an abstract class,.You should not declare body in the same class. It must implement its derived class. – SUNIL DHAPPADHULE May 02 '19 at 03:45

4 Answers4

9

Use virtual instead of abstract when you have a default implementation but would like to allow sub-classes to override

Pieter Geerkens
  • 11,775
  • 2
  • 32
  • 52
3

As said, you can't declare a body for an abstract function of an abstract class.

You need to create another class herited from your abstract class that declare the body you want.

abstract class Cake
{
    public string _flavor, _size;
    public int _quantity;
    abstract public double Compute();
}

class BirthdayCake : Cake
{
    public int _numOfCandles;


    public BirthdayCake(string flavor, string size, int quantity, int numOfCandles):base(flavor,size,quantity)
    {
        _numOfCandles = numOfCandles;
    }

    public override double Compute()
    {
        //does your stuff
    }
}
2

abstract class is a class in which you can have functions which are abstract.

abstract function is a function inside an abstract class that has no body and also it forces you to override it in the derived class.

Your issue here is that you wrote an abstract function that has a body.

Fix: Use virtual keyword instead of abstract. virtual don't force you to override the function and you can also have a body in your base class. (NB: If you are using virtual function instead of abstract function remove the abstract keyword from your class too.)

1

Abstract methods have no implementation in the same class, so the method definition is followed by a semicolon instead of a normal method block. Derived classes of the abstract class must implement all abstract methods. When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method. e.g:

Abstract methods have no implementation in the same class-Sample code

public abstract class A
{
public abstract void DoWork(int i);
public String DoWork(String str)
{
  return str;
}

}
public class B:A
{
    public virtual void DoWork(int i)
    {
        // here implementation.
    }
}
SUNIL DHAPPADHULE
  • 2,755
  • 17
  • 32