sumit gugnani

sumit gugnani

  • NA
  • 48
  • 7.6k

how to pass object to a function in inheritance or polymorph

Nov 12 2014 3:28 AM
 hello, am using this code
pls give me some response it is correct or wrong



using System;


class first
{
    protected int num1, num2;
    public first(int a = 0, int b = 0)
    {
        num1 = a;
        num2 = b;
    }
    public virtual int sum()
    {
        Console.WriteLine("sum is :");
        return 0;
    }
}
class second : first
{
    public second(int a = 0, int b = 0)
        : base(a, b)
    {

    }
    public override int sum()
    {
        Console.WriteLine("sum is :");
        return (num1 + num2);
    }
}
class three : first
{
    public three(int a = 0, int b = 0)
        : base(a, b)
    {

    }
    public override int sum()
    {
        Console.WriteLine("value is :");
        return (num1 + num2);
    }
}
class Call
{
    public void callsum(first fr)
    {
        int a;
        a = fr.sum();
        Console.WriteLine("sum: {0}", a);
    }
}
class Test
{

    static void Main(string[] args)
    {
        Call c = new Call();
        second s = new second(10, 7);
        three t = new three(10, 5);
        c.callsum(s);
        c.callsum(t);
        Console.ReadKey();
    }
}


Answers (1)