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!

4
  • 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. Commented Mar 19, 2019 at 18:59
  • learn.microsoft.com/en-us/dotnet/csharp/tutorials/inheritance and tutorialspoint.com/csharp/csharp_inheritance.htm Commented Mar 19, 2019 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) Commented Mar 19, 2019 at 19:01
  • @JonathonChase Thanks so much guys, I can't believe I missed that! Commented Mar 19, 2019 at 19:08

1 Answer 1

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"; } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.