Pages

Saturday, 13 October 2012

Write a program in C# to create a base class shape and derived classes i.e., Rectangle, Circle, and Triangle. Invoke the method from base class shape using polymorphism

Write a program in C# to create a base class shape and derived classes i.e., Rectangle, Circle, and Triangle. Invoke the method from base class shape using polymorphism

Description:

In this post we are going to learn Polymorphism and call base class from derived class.
Open Microsoft visual studio
File-> New -> Project -> visual c#-> console Application

Program:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Polymorphism
{
    class shape
    {
  public void Callme()
        {
           Console.WriteLine("Shape is called");
        }
    }
    class rect:shape
    {
        public void Callme()
{
        Console.WriteLine("Rectangle is called");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            shape s = new shape();
            rect a = new rect();
            a.Callme();
            Console.Read();
        }
    }
}

Demo:

Write a program in C# to create a base class shape and derived classes i.e., Rectangle, Circle, and Triangle. Invoke the method from base class shape using polymorphism
Write a program in C# to create a base class shape and derived classes i.e., Rectangle, Circle, and Triangle. Invoke the method from base class shape using polymorphism





No comments:

Post a Comment