C# Program to Display Cost of a Rectangle Plot Using Inheritance
Posted by Superadmin on August 13 2022 11:11:31

C# Program to Display Cost of a Rectangle Plot Using Inheritance

 

 

This is a C# Program to display cost of a rectangle plot using inheritance.

Problem Description

This C# Program Displays Cost of a Rectangle Plot Using Inheritance.

Problem Solution

Here cost of the rectangle is found using inheritance

Program/Source Code

Here is source code of the C# Program to Display Cost of a Rectangle Plot Using Inheritance. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 *  C# Program to Display Cost of a Rectangle Plot Using Inheritance
 */
using System;
    class Rectangle
    {
        protected double length;
        protected double width;
        public Rectangle(double l, double w)
        {
            length = l;
            width = w;
        }
        public double GetArea()
        {
            return length * width;
        }
        public void Display()
        {
            Console.WriteLine("Length: {0}", length);
            Console.WriteLine("Width: {0}", width);
            Console.WriteLine("Area: {0}", GetArea());
        }
    } 
    class Tabletop : Rectangle
    {
        private double cost;
        public Tabletop(double l, double w)
            : base(l, w)
        { }
        public double costcal()
        {
            double cost;
            cost = GetArea() * 70;
            return cost;
        }
        public void Display()
        {
            base.Display();
            Console.WriteLine("Cost: {0}", costcal());
        }
    }
    class CalRectangle
    {
        static void Main(string[] args)
        {
            Tabletop t = new Tabletop(7.5, 8.03);
            t.Display();
            Console.ReadLine();
        }
    }
Program Explanation

In this C# program, create the object variable‘t’ for Tabletop class by passing the value of length as 7.5 and the value of width as 8.03 as an argument.

The Tabletop class inherits the property of rectangle class. Compute the area of the rectangle using the formula
Area = length * width

Multiply the value of ‘area’ variable with 70 to compute the cost of the rectangle plot. Print the cost of a rectangle plot.

Runtime Test Cases
 
Length: 7.5
Width: 8.03
Area: 60.225
Cost: 4215.75