0% found this document useful (0 votes)
20 views4 pages

Comipledemo: //method Overloading - Same Class Same Funtion With Different Argument List...

The document contains code examples demonstrating method overloading, constructor overloading, operator overloading, and method overriding in C#. Method overloading allows defining methods with the same name but different parameters in the same class. Constructor overloading allows defining multiple constructors with different parameters. Operator overloading allows defining operators like + for custom classes. Method overriding allows redefining methods in derived classes with the same signature as the base class.

Uploaded by

Naveen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

Comipledemo: //method Overloading - Same Class Same Funtion With Different Argument List...

The document contains code examples demonstrating method overloading, constructor overloading, operator overloading, and method overriding in C#. Method overloading allows defining methods with the same name but different parameters in the same class. Constructor overloading allows defining multiple constructors with different parameters. Operator overloading allows defining operators like + for custom classes. Method overriding allows redefining methods in derived classes with the same signature as the base class.

Uploaded by

Naveen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

using System;

//Method Overloading----Same Class Same funtion With Different Argument list....


namespace Polymorphism_Demo
{
class ComipleDemo
{
public int Add(int a,int b)
{
return (a + b);
}
public double Add(double a, double b)
{
return (a + b);
}
public float Add(float a, float b)
{
return (a + b);
}

}
class Program
{
static void Main(string[] args)
{
ComipleDemo c1 = new ComipleDemo();
double a=c1.Add(12.44,23.55);
Console.WriteLine(a);
Console.Read();
}

Construcotr Overloading

using System;
//Constructor Overloading----Same Class Same funtion With Different Argument list....
namespace Polymorphism_Demo
{
class Employee
{
int salary;
public Employee()
{
salary = 10000;
}
public Employee(int s)
{
salary = s;
}
public void Show()
{
Console.WriteLine("Salary=" + salary);
}
}
class Program
{
static void Main(string[] args)
{
Employee e = new Employee();
e.Show();
Employee e1 = new Employee(30000);
e1.Show();

}
}

Console.Read();

Operator Overloading

using System;
//Method Overloading----Same Class Same funtion With Different Argument list....
namespace Polymorphism_Demo
{
class Employee
{
int salary;
public Employee()

salary = 10000;
}
public Employee(int s)
{
salary = s;
}
public void Show()
{
Console.WriteLine("Salary=" + salary);
}
public static Employee operator +(Employee e1, Employee e2)
{
Employee e3 = new Employee();
e3.salary = e1.salary + e2.salary;
return e3;
}
public static Employee operator +(Employee e, int b)
{
Employee e1=new Employee();
e1.salary = e.salary + b;
return e1;
}

class Program
{
static void Main(string[] args)
{

}
}

Employee e = new Employee();


// e.Show();
Employee e1 = new Employee(30000);
// e1.Show();
Employee e3 = new Employee(30000);
Employee e5 = new Employee(30000);
//e3.Show();
Employee e4 = e + e1+e3+e5;
Employee e6 = e+e3 + 1000;
e6.Show();
e4.Show();
Console.Read();

Method Overridng
using System;

//Method Overloading----Same Class Same funtion With Different Argument list....


namespace Polymorphism_Demo
{
class Parent
{
public virtual void Show(int a)
{
Console.WriteLine("Parent" + a);
}
}
class Child:Parent
{
public override void Show(int a)
{
Console.WriteLine("Child" + a);
}
public void Show2()
{
Console.WriteLine("Child Show2");
}
}
class Program
{
static void Main(string[] args)
{
// Parent p = new Parent(); //Static Binding
// p.Show(45);
//Child cl = new Child();
// cl.Show(12);
Parent p = new Parent(); //Dynamic Binding
p.Show(10);
p = new Child();
p.Show(23);

}
}

Console.Read();

You might also like