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.

advertisement
advertisement

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

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.