
class Account
{
private string mCode;
private string mName;
private string mDescription;
private double mBalance;
public Account(string code, string name, string description, double balance)
{
mCode = code;
mName = name;
mDescription = description;
mBalance = balance;
}
class BankAccount : Account
{
public void deposit(double amount)
{
double newBalance;
newBalance = Balance + amount;
Balance = newBalance;
}
public void withdraw(double amount)
{
double newBalance;
newBalance = Balance - amount;
Balance = newBalance;
}
}
To see how inheritance works in C#, create a windows form and name it as BankForm. Design the form as shown in the Fig below. The code for the BankForm is given below.
public partial class BankForm : Form
{
BankAccount myBankAcc = new BankAccount();
public BankForm()
{
InitializeComponent();
}
private void btnOK_Click(object sender, EventArgs e)
{
myBankAcc.Code = txtCode.Text;
myBankAcc.Name = txtName.Text;
myBankAcc.Description = txtDescription.Text;
myBankAcc.Balance = Convert.ToDouble(txtBalance.Text);
}
private void btnDeposit_Click(object sender, EventArgs e)
{
myBankAcc.deposit(100);
MessageBox.Show(myBankAcc.Getbalance().ToString());
}
private void btnWithdraw_Click(object sender, EventArgs e)
{
myBankAcc.deposit(50);
MessageBox.Show(myBankAcc.Getbalance().ToString());
}
}
}
Copyright © 2012 - All Rights Reserved - VKInfotek.com