C# example (Polymorphesim)
-
Upload
marwa-al-amri -
Category
Documents
-
view
288 -
download
4
description
Transcript of C# example (Polymorphesim)
![Page 1: C# example (Polymorphesim)](https://reader036.fdocuments.in/reader036/viewer/2022082702/5564c67cd8b42a7e178b55c5/html5/thumbnails/1.jpg)
Example In C#Shows How Object Oriented
Is
Classes/PolymorphesimInherentence/Overloading
& Overriding
Done By:-MARWA SAMIH AL-AMRI
![Page 2: C# example (Polymorphesim)](https://reader036.fdocuments.in/reader036/viewer/2022082702/5564c67cd8b42a7e178b55c5/html5/thumbnails/2.jpg)
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)](https://reader036.fdocuments.in/reader036/viewer/2022082702/5564c67cd8b42a7e178b55c5/html5/thumbnails/3.jpg)
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)](https://reader036.fdocuments.in/reader036/viewer/2022082702/5564c67cd8b42a7e178b55c5/html5/thumbnails/4.jpg)
//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();
} }}