using System;
class Calculator
{
// Method to add two integers
public int Add(int a, int b)
{
return a + b;
}
// Overloaded method to add three integers
public int Add(int a, int b, int c)
{
return a + b + c;
}
// Overloaded method to add two doubles
public double Add(double a, double b)
{
return a + b;
}
}
class Program
{
static void Main()
{
Calculator calc = new Calculator();
Console.WriteLine(calc.Add(2, 3)); // Calls Add(int, int)
Console.WriteLine(calc.Add(1, 2, 3)); // Calls Add(int, int, int)
Console.WriteLine(calc.Add(2.5, 3.5)); // Calls Add(double, double)
}
}
using System;
// Base class
class Animal
{
// Virtual method to be overridden in derived classes
public virtual void Speak()
{
Console.WriteLine("The animal makes a sound.");
}
// Method to display the animal's type
public void DisplayType()
{
Console.WriteLine("This is an animal.");
}
}
// Derived class
class Dog : Animal
{
// Overriding the Speak method
public override void Speak()
{
Console.WriteLine("The dog barks.");
}
// Method specific to Dog
public void Fetch()
{
Console.WriteLine("The dog fetches the ball.");
}
}
// Derived class
class Cat : Animal
{
// Overriding the Speak method
public override void Speak()
{
Console.WriteLine("The cat meows.");
}
// Method specific to Cat
public void Scratch()
{
Console.WriteLine("The cat scratches the furniture.");
}
}
class Program
{
static void Main()
{
// Create instances of Dog and Cat
Animal myDog = new Dog();
Animal myCat = new Cat();
// Demonstrate run-time polymorphism
myDog.Speak(); // Outputs: The dog barks.
myCat.Speak(); // Outputs: The cat meows.
// Demonstrate base class method
myDog.DisplayType(); // Outputs: This is an animal.
myCat.DisplayType(); // Outputs: This is an animal.
// Directly accessing methods specific to Dog and Cat
Dog specificDog = new Dog();
Cat specificCat = new Cat();
specificDog.Fetch(); // Outputs: The dog fetches the ball.
specificCat.Scratch(); // Outputs: The cat scratches the furniture.
}
}