2

I am trying to create a basic banking system to practice using classes, after creating the parent class "Account", I tried to create a "savings" class which will be the child class and inherit the attributes and methods, however, no matter what I look up nothing will tell me how to do this. I get errors such as "must declare a body because it is not marked abstract, extern or partial", and so on. I really don't know what to do to get it working, so I'm hoping someone here can help, here's my code:

public class Account
{
    protected string name;
    protected string birthdate;
    protected string address;
    protected double balance;
    protected string status;
    protected string type;

    public Account(string customerName, string customerBirthdate, string customerAddress, int customerBalance)
    {
        name = customerName;
        birthdate = customerBirthdate;
        address = customerAddress;
        balance = customerBalance;
        status = "Ok";
        type = "Basic";         
    }

    public void customerDetails()
    {
        Console.WriteLine("Name: {0}, Birthdate: {1}, Address: {2}", name, birthdate, address);
    }

    public void accountDetails()
    {
        Console.WriteLine("Balance: £{0}, Account Status: {1}, Account Type: {2}", Math.Round(balance, 2), status, type);
    }

    private void updateStatus()
    {
        if (balance < 0)
        {
            status = "Overdrawn";
        }
        else if (balance > 0 )
        {
            status = "Ok";
        }
    }

    public void deposit(int amount)
    {
        balance += amount;
        updateStatus();
    }

    public void withdraw(int amount)
    {
        balance -= amount;
        updateStatus();
    }
}

public class Savings : Account
{
    public Savings(string customerName, string customerBirthdate, string customerAddress, int customerBalance) : Account(customerName, customerBirthdate, customerAddress, customerBalance)
    {
        name = customerName;
        birthdate = customerBirthdate;
        address = customerAddress;
        balance = customerBalance;
        status = "Ok";
        type = "Basic";
    }
}

Thanks in advance if anyone can help me out!

Akqa
  • 21
  • 2
  • 3
    When you want to call the constructor of a base class, you should use the `base` keyword, not the name of the base class itself. – Jonathon Chase Mar 19 '19 at 18:59
  • https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/inheritance and https://www.tutorialspoint.com/csharp/csharp_inheritance.htm – Ňɏssa Pøngjǣrdenlarp Mar 19 '19 at 19:01
  • @JonathonChase is right just change the Account class name to Base in Savings class. `public Savings(string customerName, string customerBirthdate, string customerAddress, int customerBalance) : base(customerName, customerBirthdate, customerAddress, customerBalance)` – Hasan Mahmood Mar 19 '19 at 19:01
  • @JonathonChase Thanks so much guys, I can't believe I missed that! – Akqa Mar 19 '19 at 19:08

1 Answers1

0

Code should be like this (Changed Account by base)

public class Account
{
    protected string name;
    protected string birthdate;
    protected string address;
    protected double balance;
    protected string status;
    protected string type;

    public Account(string customerName, string customerBirthdate, string customerAddress, int customerBalance)
    {
        this.name = customerName;
        this.birthdate = customerBirthdate;
        this.address = customerAddress;
        this.balance = customerBalance;
        this.status = "Ok";
        this.type = "Basic";
    }

    public void customerDetails()
    {
        Console.WriteLine("Name: {0}, Birthdate: {1}, Address: {2}", name, birthdate, address);
    }

    public void accountDetails()
    {
        Console.WriteLine("Balance: £{0}, Account Status: {1}, Account Type: {2}", Math.Round(balance, 2), status, type);
    }

    private void updateStatus()
    {
        if (balance < 0)
        {
            status = "Overdrawn";
        }
        else if (balance > 0)
        {
            status = "Ok";
        }
    }

    public void deposit(int amount)
    {
        balance += amount;
        updateStatus();
    }

    public void withdraw(int amount)
    {
        balance -= amount;
        updateStatus();
    }
}

public class Savings : Account
{
    public Savings(string customerName, string customerBirthdate, string customerAddress, int customerBalance) : base (customerName, customerBirthdate, customerAddress, customerBalance)
    {
        base.name = customerName;
        base.birthdate = customerBirthdate;
        base.address = customerAddress;
        base.balance = customerBalance;
        base.status = "Ok";
        base.type = "Basic";
    }
}
Fermín
  • 737
  • 1
  • 7
  • 25