Answer to Question #329541 in C# for Babar302

Question #329541

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.


1
Expert's answer
2022-04-16T12:57:00-0400
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();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS