|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace abstraction
{
public partial
class
frmEncapsulation : Form
{
public frmEncapsulation()
{
InitializeComponent();
}
public class
CustomerDetails
{
private int
CustomerID;
//private type variables accessable only same
class
private string
CustomerName;
//private type variable accessable only same
class
protected double
Balance;
//protected type variable accessable only same class
and 1 level derived class
public string
CustomerType; //public type variable accessable
anywhere
private void
Input(int customerId,
string customerName) //private method not
directly called
{
CustomerID=customerId;
//use the private member in same class
CustomerName=customerName;
//ues the private member in same class
}
public void
Input(int customerId,
string customerName, double balance)//public function
{
Input(customerId, customerName);
//public function call the private method in
the the same class
Balance=balance;//use of protected function in
the same class
}
public void
Display()
//public function
{
MessageBox.Show("Customer Details\nCustomer ID..." +
CustomerID+"\nCustomer Name..."+CustomerName+"\nCustomer Balance..."+Balance);
}
}
public class
BalanceUpdate :
CustomerDetails
//creatin child class BalanceUpdate from the base
class CustomerDetails
{
//so BalanceUpdate automatically derived the
protected, public property/method of CustomerDetails
public void
AddBalance(double balance)
//create the public function
{
if (balance > 0)
Balance = Balance + balance;
//access the prtected member of base class
else
MessageBox.Show("give valid balance");
}
public void
SubstractBalance(double balance)
//create the public function
{
if (balance > 0)
Balance = Balance - balance;
//access the prtected member of base class
else
MessageBox.Show("give valid balance");
}
}
public class
CustomerType :
BalanceUpdate
//creatin child class CustomerType from the base class
BalanceUpdate
{
//BalanceUpdate also a child class of a
CustomerDetails so CustomerType automatically derived the only public property
and method of CustomerDetails
public void
GetType(string customerType)
{
CustomerType = customerType;
//access the public member of CustomerDetails
class
}
public void
Show()
{
Display(); //access the public function of
CustomerDetails class
MessageBox.Show("Customer Type
Details\nCustomer Type..." + CustomerType);
}
}
private void
buttonShow_Click(object sender, EventArgs e)
{
CustomerType CT =
new CustomerType();
//creating object of CustomerType class
double Bal =
Int32.Parse(txtBalance.Text);
CT.Input(Int32.Parse(txtCustomerID.Text),
txtCustomerName.Text, Bal);//access the public
function of CustomerDetails class
CT.GetType(txtCustometType.Text);//access the
public member of CustomerType class
CT.Show();
//access the public member of CustomerType
class
CT.AddBalance(3000);//access the public member
of BalanceUpdate class
CT.Show();//access the public member of CustomerType class
CT.SubstractBalance(2000);//access the public
member of BalanceUpdate class
CT.Show();//access the public member of CustomerType class
}
}
}
|