Create a class named Shape with a function that prints "This is a shape". Create another class named Polygon inheriting the Shape class with the same function that prints "Polygon is a shape". Create two other classes named Rectangle and Triangle having the same function which prints "Rectangle is a polygon" and "Triangle is a polygon" respectively. Again, make another class named Square having the same function which prints "Square is a rectangle".
Now, try calling the function by the object of each of these classes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace C_SHARP
{
class Program
{
class Shape
{
public virtual void print()
{
Console.WriteLine("This is a shape.");
}
}
class Polygon : Shape
{
public void print()
{
Console.WriteLine("Polygon is a shape.");
}
}
class Rectangle : Polygon
{
public void print()
{
Console.WriteLine("Rectangle is a Polygon.");
}
}
class Triangle : Polygon
{
public void print()
{
Console.WriteLine("Triangle is a Polygon.");
}
}
class Square : Rectangle
{
public void print()
{
Console.WriteLine("Square is a Rectangle.");
}
}
static void Main(string[] args)
{
Shape S =new Shape();
Polygon P = new Polygon();
Rectangle R = new Rectangle();
Triangle T = new Triangle();
Square Sq = new Square();
S.print();
P.print();
R.print();
T.print();
Sq.print();
Console.ReadKey();
}
}
}
Comments
Leave a comment