0% found this document useful (0 votes)
24 views5 pages

Oop

The document contains C# code demonstrating object-oriented programming concepts such as classes, inheritance, method overloading, and method overriding. It includes examples of a banking system with customer classes, a vehicle class with a car subclass, and a calculator class with multiple addition methods. Additionally, it showcases static versus instance members in a census class.

Uploaded by

posubabu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views5 pages

Oop

The document contains C# code demonstrating object-oriented programming concepts such as classes, inheritance, method overloading, and method overriding. It includes examples of a banking system with customer classes, a vehicle class with a car subclass, and a calculator class with multiple addition methods. Additionally, it showcases static versus instance members in a census class.

Uploaded by

posubabu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

// Class and Object

============================
using System;

namespace PRASimulation
{
class CustomerSBI
{
int id;
private string name;
double balance;

public int GetId()


{
return id;
}

public void SetID(int id)


{
this.id = id;
}

public string GetName()


{
return name;
}

public void SetName(string name)


{
this.name = name;
}

public double GetBalance()


{
return balance;
}

public void SetBalance(double balance)


{
this.balance = balance;
}

public CustomerSBI()
{
this.name = "Anonymous";
this.id = 100;
this.balance = 0.0;
}

public void Deposit(double amount)


{
this.balance += amount;
}

public bool WithDraw(double amount)


{
if((this.balance - amount ) <= 500)
{
return false;
}
this.balance = balance - amount;
return true;
}
}
class Program
{
static void Main(string[] args)
{
CustomerSBI harshad = new CustomerSBI();
harshad.SetID(101);
harshad.SetName("Harshad Bhai");
harshad.SetBalance(2000);

harshad.WithDraw(100);
harshad.Deposit(200);

Console.WriteLine("Balance after transactions: {0}",


harshad.GetBalance());
}
}
}

Inheritance
========================================
namespace PRASimulation
{
class Vehicle
{
protected int regNo;
public Vehicle(int regNo)
{
this.regNo = regNo;
}
}

class Car : Vehicle


{
public string model { get; set; }
public Car(int regNo, string model) : base(regNo)
{
this.model = model;
}
public override string ToString()
{
String message = $"Model: {model}\nRegistration no: {regNo}";
return message;
}
}

class Program
{
static void Main(string[] args)
{
var car = new Car(7860, "Mahindra");
Console.WriteLine(car);
}
}
}
Method overrloading
===============================
using System;

namespace PRASimulation
{
class Calculator
{
public int Addition(int a, int b)
{
Console.WriteLine("Method 1 is executed");
return a + b;
}

public int Addition(int a, int b, int c)


{
Console.WriteLine("Method 2 is executed");
return a + b + c;
}

public float Addition(int a, float b)


{
Console.WriteLine("Method 3 is executed");
return (float)a + b;
}
public float Addition(float a, int b)
{
Console.WriteLine("Method 4 is executed");
return (float) a + b;
}
public float Addition(float a, float b)
{
Console.WriteLine("Method 5 is executed");
return a + b;
}
}

class Program
{
static void Main(string[] args)
{
var adder = new Calculator();

adder.Addition(10, 20);
adder.Addition(10, 20,30);
adder.Addition(10.4f, 20);
adder.Addition(10, 20.4f);
adder.Addition(10f, 20.4f);
}
}
}

Method overriding
========================================
using System;

namespace PRASimulation
{
class YesCustomer
{
public int ID { get; set; }
public string Name { get; set; }
public double Balance { get; set; }

public YesCustomer(int ID, string name, double balance)


{
this.ID = ID;
this.Name = name;
this.Balance = balance;
}

public virtual bool WithDraw(double amount)


{
this.Balance = Balance - amount;
return true;
}

public void Deposit(double amount)


{
this.Balance += amount;
}
}

class SBICustomer: YesCustomer


{
public string type { get; set; }

public SBICustomer(int id, string name, double balance, string type)


: base(id, name, balance)
{
this.type = type;
}

public override bool WithDraw(double amount)


{
if ((this.Balance - amount) <= 500)
return false;
this.Balance = Balance - amount;
return true;
}
}

class Program
{
static void Main(string[] args)
{
var john = new SBICustomer(101, "John wick", 1000, "Current");
var jason = new SBICustomer(101, "Jason momoa", 500, "Savings");

if(john.WithDraw(600))
{
Console.WriteLine("{0} Balance is: {1}", john.Name, john.Balance);
}
else
{
Console.WriteLine("{0} Cannot withdraw due to minimum limit issue",
john.Name);
}

jason.Deposit(3011);
if (jason.WithDraw(600))
{
Console.WriteLine("{0} Balance is: {1}", jason.Name,jason.Balance);
}
else
{
Console.WriteLine("{0} Cannot withdraw due to minimum limit issue",
jason.Name);
}
}
}
}

// Static vs Instance: Modification needed.


using System;

namespace PRASimulation
{
class Census
{
public int ID { get; set; }
public string Name { get; set; }
static int counter { get; set; }

public Census(int ID, string Name)


{
this.ID = ID;
this.Name = Name;
}

public int GetTotalPopulation()


{
return counter;
}
}
class Program
{
static void Main(string[] args)
{
var abbas = new Census(101, "Abbas");
var javed = new Census(102, "Javed");
var arjun = new Census(103, "Arjun");
var vikas = new Census(104, "Vikas");
}
}
}

You might also like