Unit-IV; Professional Sales Representative (PSR).pptx
Presentation.pptx
1. Oops
Class – collection of objects
Object – instance of a class
Encapsulation – process of binding data members and member functions into single un
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class rectangle
{
public double length;
public double width;
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
2. class Program
{
static void Main(string[] args)
{
rectangle r = new rectangle();
r.length = 3.5;
r.width = 4.6;
r.Display();
}
}
}
Abstraction - Abstraction is a process of hiding the implementation details and displaying the essential features.
.Net has five access Specifiers
Public -- Accessible outside the class through object reference.
Private -- Accessible inside the class only through member functions.
Protected -- Just like private but Accessible in derived classes also through member
functions.
Internal -- Visible inside the assembly. Accessible through objects.
Protected Internal -- Visible inside the assembly through objects and in derived classes outside the assembly through member func
4. Inheritance:
Inheritance is a process of deriving the new class from already existing class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Shape
{
public void setWidth(int w)
{
width = w;
}
public void setHeight(int h)
{
height = h;
}
protected int width;
protected int height;
}
// Derived class
class Rectangle : Shape
{
public int getArea()
{
return (width * height);
}
}
5. class Program
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.setWidth(9);
r.setHeight(5);
r.getArea();
Console.WriteLine(r.getArea());
}
}
}
Polymorphism:
When a message can be processed in different ways is called polymorphism. Polymorphism means many forms.
Polymorphism is of two types:
Compile time polymorphism/Overloading – method name must be same parameters and return type may be varied
Function overloading
Operator overloading
Runtime polymorphism/Overriding – changing the functionality if a method without changing its signature
6. Function Overloading
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Printdata
{
void print(int i)
{
Console.WriteLine("Printing int: {0}", i);
}
void print(double f)
{
Console.WriteLine("Printing float: {0}", f);
}
void print(string s)
{
Console.WriteLine("Printing string: {0}", s);
}
class Program
{
static void Main(string[] args)
{
Printdata p = new Printdata();
p.print(2);
p.print(3.6);
p.print("pavan");
Console.ReadKey();
}
}
}
}
7. Operator Overloading
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Box
{
private double length; // Length of a box
private double width; // Width of a box
private double height; // Height of a box
public double getVolume()
{
return length * width * height;
}
public void setLength( double len )
{
length = len;
}
public void setWidth( double wid )
{
width = wid;
}
public void setHeight( double hei )
{
height = hei;
}
8. // Overload + operator to add two Box objects.
public static Box operator+ (Box b, Box c)
{
Box box = new Box();
box.length = b.length + c.length;
box.width = b.width + c.width;
box.height = b.height + c.height;
return box;
}
}
class Program
{
static void Main(string[] args)
{
Box Box1 = new Box(); // Declare Box1 of type Box
Box Box2 = new Box(); // Declare Box2 of type Box
Box Box3 = new Box(); // Declare Box3 of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.setLength(6.0);
Box1.setWidth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setWidth(13.0);
Box2.setHeight(10.0);
9. // volume of box 1
volume = Box1.getVolume();
Console.WriteLine("Volume of Box1 : {0}", volume);
// volume of box 2
volume = Box2.getVolume();
Console.WriteLine("Volume of Box2 : {0}", volume);
// Add two object as follows:
Box3 = Box1 + Box2;
// volume of box 3
volume = Box3.getVolume();
Console.WriteLine("Volume of Box3 : {0}", volume);
Console.ReadKey();
}
}
}
10. Runtime polymorphism/Overriding
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class BaseClass
{
public virtual void Method1()
{
Console.Write("Base Class Method");
}
}
// Derived class
public class DerivedClass : BaseClass
{
public override void Method1()
{
Console.Write("Derived Class Method");
}
}
class Program
{
static void Main(string[] args)
{
// calling the overriden method
DerivedClass objDC = new DerivedClass();
objDC.Method1();
// calling the baesd class method
BaseClass objBC = (BaseClass)objDC;
objDC.Method1();
}
}
}
11. Overriding in C#
1. Virtual – This keyword is used with a base class which signifies that the method
of a base class can be overridden.
2. Override – This keyword is used with a derived class which signifies that derived
class overrides a method of a base class.
3. Base – This keyword is used in a derived class to call the base class method.
12. using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Bird // base class
{
public void fly() // base class method
{
Console.WriteLine("Birds are flying");
}
}
class Peacock : Bird // derived class
{
public new void fly() // derived class method
{
Console.WriteLine("Peacock is flying");
}
}
class Program
{
static void Main(string[] args)
{
Bird b = new Peacock();
b.fly();
Console.ReadLine();
}
}
}
Example 1 – Without Virtual and Override Keywords
13. using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Bird // base class
{
public void fly() // base class method
{
Console.WriteLine("Birds are flying");
}
}
class Peacock : Bird // derived class
{
public new void fly() // derived class method
{
Console.WriteLine("Peacock is flying");
}
}
class Program
{
static void Main(string[] args)
{
Bird b = new Peacock();
b.fly();
Console.ReadLine();
}
}
}
Example 2 (a)- With Virtual and Override Keywords
14. Constructors
A class constructor is a special member function of a class that is executed whenever we create new objects of that class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Line
{
private double length; // Length of a line
public Line()
{
Console.WriteLine("Object is being created");
}
public void setLength(double len)
{
length = len;
}
public double getLength()
{
return length;
}
class Program
{
static void Main(string[] args)
{
Line line = new Line();
// set line length
line.setLength(6.0);
Console.WriteLine("Length of line : {0}", line.getLength());
Console.ReadKey();
}
}
}
}
15. Destructors
A destructor is a special member function of a class that is executed whenever an object of its class goes out of scope. A destructor will have exact
same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Line
{
private double length; // Length of a line
public Line() // constructor
{
Console.WriteLine("Object is being created");
}
~Line() //destructor
{
Console.WriteLine("Object is being deleted");
}
public void setLength(double len)
{
length = len;
}
public double getLength()
{
return length;
}
class Program
{
static void Main(string[] args)
{
Line line = new Line();
// set line length
line.setLength(6.0);
Console.WriteLine("Length of line : {0}", line.getLength());
}
}
}
}
16. Interface : should be used if you want to imply a rule on the components which may
or may not be related to each other
Abstract Class : should be used where you want to have some basic or default behavior
or implementation for components related to each other
Pros:
Interface: Allows multiple inheritance
provides abstraction by not exposing what exact kind of object is being used in the contex
provides consistency by a specific signature of the contract
Abstract Class: faster then interface
has flexibility in the implementation (you can implement it fully or partially) can be
easily changed without breaking the derived classes
Cons:
Interface : Must implement all the contracts defined
cannot have variables or delegates
once defined cannot be changed without breaking all the classes
Abstract Class : cannot be instantiated does not support multiple inheritance
17. Abstract class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
abstract class M1
{
public int add(int a, int b)
{
return (a + b);
}
}
class M2 :M1
{
public int mul(int a, int b)
{
return a * b;
}
}
class Program
{
static void Main(string[] args)
{
M2 ob = new M2();
int result = ob.add(10, 20);
Console.WriteLine("the result is {0}", result);
int muls = ob.mul(10, 20);
Console.WriteLine("the result is {0}", muls);
Console.ReadLine();
}
}
}
18. Delegates
C# delegates are similar to pointers to functions in C or C++. A delegate is a reference type variable that holds the reference to a method.
The reference can be changed at runtime.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
delegate int NumberChanger(int n);
namespace ConsoleApplication1
{
class TestDelegate
{
static int num = 10;
public static int AddNum(int p)
{
num += p;
return num;
}
public static int MultNum(int q)
{
num *= q;
return num;
}
public static int getNum()
{
return num;
}
class Program
{
static void Main(string[] args)
{ //create delegate instances
NumberChanger nc1 = new NumberChanger(AddNum);
NumberChanger nc2 = new NumberChanger(MultNum); //calling the methods using the delegate objects
nc1(25);
Console.WriteLine("Value of Num: {0}", getNum());
nc2(5);
Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();
}
}
}
}
19. Sealed Class
Class is defined as sealed class, this class cannot be inherited.
A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class.
Sealed classes are primarily used to prevent derivation.
Because they can never be used as a base class,
some run-time optimizations can make calling sealed class members slightly faster.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SealedClass sealedCls = new SealedClass();
int total = sealedCls.Add(4, 5);
Console.WriteLine("Total = " + total.ToString());
}
}
// Sealed class
sealed class SealedClass
{
public int Add(int x, int y)
{
return x + y;
}
}
}
}
20. Partial Class
Splitting up of a class in to two or more sub classes is called as partial classes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public partial class MyTest
{
private int a;
private int b;
public void getAnswer(int a, int b)
{
this.a = a;
this.b = b;
}
}
public partial class MyTest
{
public void PrintCoOrds()
{
Console.WriteLine("Integer values: {0},{1}", a, b);
Console.WriteLine("Addition: {0}", a + b);
Console.WriteLine("Mulitiply: {0}", a * b);
}
}
class Program
{
static void Main(string[] args)
{
MyTest ts = new MyTest();
ts.getAnswer(12, 25);
ts.PrintCoOrds();
Console.Read();
}
}
}