Answer to Question #326400 in C# for Chinara

Question #326400

12. Create a class named 'Rectangle' with two data members 'length' and 'breadth' and two functions to print the area and perimeter of the rectangle respectively. Its constructor having parameters for length and breadth is used to initialize the length and breadth of the rectangle. Let class 'Square' inherit the 'Rectangle' class with its constructor having a parameter for its side (suppose s) calling the constructor of its parent class. Print the area and perimeter of a rectangle and a square. C#

1
Expert's answer
2022-04-12T16:18:53-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ComputeAverageApp
{
    class ComputeAverageProgram
    {
        static void Main(string[] args)
        {
 

            Console.Write("Enter length: ");
            int length = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter breadth: ");
            int breadth = Convert.ToInt32(Console.ReadLine());

            Console.Write("Rectangle: ");
            Rectangle rectangle = new Rectangle(length, breadth);
            rectangle.PrintArea();
            rectangle.PrintPerimeter();

            Console.Write("Square: ");
            Square square = new Square(length);
            square.PrintArea();
            square.PrintPerimeter();

            Console.ReadLine();
        }

 
    }

    public class Rectangle
    {
        int length;
        int breadth;

        public Rectangle(int l, int b)
        {
            length = l;
            breadth = b;
        }

        public void PrintArea()
        {
             Console.WriteLine("Area = {0}", length * breadth);
        }

        public void PrintPerimeter()
        {
            Console.WriteLine("Perimeter = {0}", (length + breadth) * 2);
        }
    }

    public class Square: Rectangle
    {
        public Square(int s) : base (s,s)
        {
 
        }
    }

}

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