C# example (Polymorphesim)

5
Example In C# Shows How Object Oriented Is Classes/Polymorphesim Inherentence/Overloading & Overriding Done By:- MARWA SAMIH AL-AMRI

description

Example In C# Classes/Polymorphisem Inherentence/Overloading & Overriding

Transcript of C# example (Polymorphesim)

Page 1: C# example (Polymorphesim)

Example In C#Shows How Object Oriented

Is

Classes/PolymorphesimInherentence/Overloading

& Overriding

Done By:-MARWA SAMIH AL-AMRI

Page 2: C# example (Polymorphesim)

using System;using System.Collections.Generic;using System.Text;

namespace uml{ class Shape { public virtual void draw() { Console.WriteLine("Empty"); } }{

using System;using System.Collections.Generic;using System.Text;

namespace uml{ class Circle:Shape { public override void draw() { Console.WriteLine("This is Circle"); }

public double m (int x,int y) { return x * y; }

public double m(int x, double y, int z) { Console.WriteLine("\n\nthe integer version = "+ m(1,2)); Console.WriteLine("the double version = " + m(2.3 , 2)); Console.WriteLine("with different order = " + m(2, 2.3)); return x * y / z; }

public double m(double x, int y) { return x * y; }

public double m(int x, double y) { return x / y; }

}}

Page 3: C# example (Polymorphesim)

using System;using System.Collections.Generic;using System.Text;

namespace uml{ class Rectangle:Shape { public override void draw() { Console.WriteLine("This is Rectangle"); } }}

using System;using System.Collections.Generic;using System.Text;

namespace uml{ class Square:Rectangle { public override void draw() { base.draw(); Console.WriteLine("this is Square"); }

}}

using System;using System.Collections.Generic;using System.Text;

namespace uml{ class Program { static void Main(string[] args) { Shape S; Rectangle R = new Rectangle(); S = R; S.draw();

S = new Circle(); S.draw();

Page 4: C# example (Polymorphesim)

//S.printCenter(); error

Circle C = new Circle(); C.printCenter();

S = new Square(); S.draw();

R = new Square(); S = R; S.draw();

//R = S; error Console.WriteLine("\n");

Shape [] sh = new Shape[4]; sh[3] = new Shape(); sh[0] = new Rectangle(); sh[1] = new Circle(); sh[2] = new Square();

for (int i = 0; i < 4; i++) sh[i].draw();

} }}