class ClsComplex
{
int R, I;
public ClsComplex(int R, int I)
{
this.R = R;
this.I = I;
Console.WriteLine("{0} + {1}i", R, I);
}
public static ClsComplex operator +(ClsComplex Num1, ClsComplex Num2)
{
ClsComplex Num3 = new ClsComplex(Num1.R + Num2.R, Num1.I + Num2.I);
return Num3;
}
}
class ClsOpOverLoad
{
static void Main(string[] args)
{
Console.Write("C1= ");
ClsComplex C1 = new ClsComplex(4, 5);
Console.Write("C2= ");
ClsComplex C2 = new ClsComplex(8, 2);
Console.Write("C3= ");
ClsComplex C3 = C1 + C2;
Console.Read();
}
}
Blog that contains articles about new technologies related or not with programming. I will describe and solves some problems that I encounter in my career. ASP .NET, AJAX, Javascript, C++, C# and SQL are some of the subjects that will appear.
Showing posts with label polymorphism. Show all posts
Showing posts with label polymorphism. Show all posts
19 September 2011
Method OverLoading in console apps
class mOverLoading
{
int x=10;
public void show()
{
Console.WriteLine("default constructor");
Console.WriteLine(x + this.x);
}
public void show(int x)
{
Console.WriteLine("Parameterized constructors");
Console.WriteLine(x+" "+this.x);
}
public void show(string s, int x)
{
Console.WriteLine("Parameterized constructor");
Console.WriteLine(s + " " + x);
}
public static void Main()
{
mOverLoading mol = new mOverLoading();
mol.show();
mol.show(123);
mol.show("satya", 456);
Console.ReadLine();
}
}
{
int x=10;
public void show()
{
Console.WriteLine("default constructor");
Console.WriteLine(x + this.x);
}
public void show(int x)
{
Console.WriteLine("Parameterized constructors");
Console.WriteLine(x+" "+this.x);
}
public void show(string s, int x)
{
Console.WriteLine("Parameterized constructor");
Console.WriteLine(s + " " + x);
}
public static void Main()
{
mOverLoading mol = new mOverLoading();
mol.show();
mol.show(123);
mol.show("satya", 456);
Console.ReadLine();
}
}
Constructor OverLoading in Console apps
class CACOverLoading
{
int x;
public CACOverLoading()
{
Console.WriteLine("Default constructor");
x = 123;
}
public CACOverLoading(int x)
{
Console.WriteLine("Parameterized constructor");
Console.WriteLine(x);
this.x = 789;
}
public void display()
{
Console.WriteLine(x);
}
public static void Main()
{
CACOverLoading col = new CACOverLoading();
col.display();
CACOverLoading col1 = new CACOverLoading(456);
col1.display();
Console.ReadLine();
}
}
{
int x;
public CACOverLoading()
{
Console.WriteLine("Default constructor");
x = 123;
}
public CACOverLoading(int x)
{
Console.WriteLine("Parameterized constructor");
Console.WriteLine(x);
this.x = 789;
}
public void display()
{
Console.WriteLine(x);
}
public static void Main()
{
CACOverLoading col = new CACOverLoading();
col.display();
CACOverLoading col1 = new CACOverLoading(456);
col1.display();
Console.ReadLine();
}
}
Virtual class overriding
class Virtual
{
public virtual void add(int a, int b)
{
Console.WriteLine(a + b);
}
}
class overridevirtual:Virtual
{
public override void add(int a, int b)
{
Console.WriteLine(a);
}
public static void Main()
{
Virtual vir = new Virtual();
vir.add(1,2);
Console.ReadLine();
}
}
{
public virtual void add(int a, int b)
{
Console.WriteLine(a + b);
}
}
class overridevirtual:Virtual
{
public override void add(int a, int b)
{
Console.WriteLine(a);
}
public static void Main()
{
Virtual vir = new Virtual();
vir.add(1,2);
Console.ReadLine();
}
}
Abstract overriding in console apps
abstract class Abstract {
public abstract void mul(int x);
public void add(int x, int y)
{
Console.WriteLine("add is: "+(x + y));
}
}
class overrideabstract : Abstract
{
public override void mul(int a)
{
Console.WriteLine("mul is: "+a*a);
}
public static void Main()
{
overrideabstract ovra = new overrideabstract();
ovra.add(4, 5);
ovra.mul(3);
Console.ReadLine();
}
}
Note: An abstract class can implement from another abstract class. and a class can implement only one abstract class at a time.
public abstract void mul(int x);
public void add(int x, int y)
{
Console.WriteLine("add is: "+(x + y));
}
}
class overrideabstract : Abstract
{
public override void mul(int a)
{
Console.WriteLine("mul is: "+a*a);
}
public static void Main()
{
overrideabstract ovra = new overrideabstract();
ovra.add(4, 5);
ovra.mul(3);
Console.ReadLine();
}
}
Note: An abstract class can implement from another abstract class. and a class can implement only one abstract class at a time.
Subscribe to:
Posts (Atom)