Академический Документы
Профессиональный Документы
Культура Документы
2
Implementing OOP
concepts in C#
Review
Review Contd
C# Simplified / Session 2 / 3 of 28
Objectives
Use Constructors In C#
Use Destructors In C#
Use Inheritance in C#
Discuss Overriding
C# Simplified / Session 2 / 4 of 28
Constructors In C#
C# Simplified / Session 2 / 5 of 28
Constructors - Example
using System;
public class DaysInYear
{
private int days;
public DaysInYear()
{
days = 365;
}
static void Main(String[] args)
{
DaysInYear newDaysInYear =new DaysInYear();
Console.WriteLine (newDaysInYear.days);
}
}
With Constructor
Without Constructor
C# Simplified / Session 2 / 6 of 28
Parameterized
Constructors
C# Simplified / Session 2 / 7 of 28
Destructors In C#
C# Simplified / Session 2 / 8 of 28
Garbage Collector
Garbage Collector
Contd
C# Simplified / Session 2 / 10 of 28
Method Overloading
Two ways of overloading methods
C# Simplified / Session 2 / 11 of 28
C# Simplified / Session 2 / 12 of 28
C# Simplified / Session 2 / 13 of 28
Operator Overloading
Contd
C# Simplified / Session 2 / 14 of 28
Operator Overloading
using System;
public class Distance
{
int longitude, latitude;
public Distance()
{
longitude = 0;
latitude = 0;
}
public Distance(int longitude, int latitude)
{
this.longitude = longitude;
this.latitude = latitude;
}
public static Distance operator - (Distance
first, Distance second)
{
return new
Distance(first.longitude second.longitude, first.latitude second.latitude);
}
//main
}
C# Simplified / Session 2 / 15 of 28
Inheritance In C#
Single Inheritance
Multiple Inheritance
C# Simplified / Session 2 / 16 of 28
Single Inheritance
using System;
namespace Ch2Ex5
{
class Inherit
{
static void Main(string[] args)
{
Square squareObj =new Square();
Rectangle rectObj =new
Rectangle();
squareObj.calculateArea(10,20);
rectObj.calculateArea(20,20);
}
}
class Shape
{
public int length;
public int breadth;
public void calculateArea(int len,
int
breadth);
{
}
}
class Rectangle:Shape
{
public Rectangle()
{
length=0;
breadth=0;
}
class Rectangle:Shape
{
public Rectangle()
{
length=0;
breadth=0;
}
public void calculateArea(int len, int
breadth)
{
Console.WriteLine ("Area of a Rectangle
is
{0}",len*breadth);
}
C# Simplified / Session 2 / 17 of 28
Single Inheritance
Contd
class Square:Shape
{
public Square()
{
}
public void calculateArea(int side1, int
side2)
{
int area;
area = side1*side2;
Console.WriteLine ("Area of a Square is
{0}",area);
}
}
}
C# Simplified / Session 2 / 18 of 28
Sealing a Class
A class is sealed when no class
should be allowed to inherit from
that class.
Keyword sealed is used to seal a
class classOne
class. sealed
{
//Class Implementation
}
C# Simplified / Session 2 / 19 of 28
base Keyword
C# Simplified / Session 2 / 20 of 28
override keyword
C# Simplified / Session 2 / 21 of 28
virtual keyword
C# Simplified / Session 2 / 22 of 28
C# Simplified / Session 2 / 23 of 28
new keyword
C# Simplified / Session 2 / 24 of 28
C# Simplified / Session 2 / 25 of 28
Method Overriding
C# Simplified / Session 2 / 26 of 28
class MethodOverride
{
public static void Main()
{
StringAddition objStringAddition = new
StringAddition();
objStringAddition.add();
}
}
using System;
class IntAddition
{
public void add()
{
int firstNum =1;
int secondNum =2;
Console.WriteLine ("The Sum of the two numbers is :
{0}", firstNum+secondNum);
}
}
class StringAddition : IntAddition
{
new public void add()
{
string firstStr="a";
string secondStr="b";
Console.WriteLine ("The Sum of the two strings is :
{0}", firstStr+secondStr);
}
}
C# Simplified / Session 2 / 27 of 28
Summar
y
C# Simplified / Session 2 / 28 of 28