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#
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)
{
}
}
}
Comments
Leave a comment