Knowee
Questions
Features
Study Tools

Write a C++ program to create a class called Garden with :A default constructor to read the length and breadtha method called Area() that will calculate the area of a rectangular garden. Create a subclass called  Pond that overrides the Area() method to calculate the area of the circular pond .Also define a default constructor that will read the radius of the pond.Sample Input233Sample OutputLength of garden:2Breadth of garden:3Radius of pond:3Area of garden:6Area of pond:28.26

Question

Write a C++ program to create a class called Garden with :A default constructor to read the length and breadtha method called Area() that will calculate the area of a rectangular garden. Create a subclass called  Pond that overrides the Area() method to calculate the area of the circular pond .Also define a default constructor that will read the radius of the pond.Sample Input233Sample OutputLength of garden:2Breadth of garden:3Radius of pond:3Area of garden:6Area of pond:28.26

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is the C++ program according to your requirements:

#include<iostream>
#include<cmath>
using namespace std;

class Garden {
    protected:
        double length, breadth;
    public:
        Garden() {
            cout << "Enter length of garden: ";
            cin >> length;
            cout << "Enter breadth of garden: ";
            cin >> breadth;
        }
        virtual double Area() {
            return length * breadth;
        }
};

class Pond : public Garden {
    private:
        double radius;
    public:
        Pond() : Garden() {
            cout << "Enter radius of pond: ";
            cin >> radius;
        }
        double Area() override {
            return M_PI * radius * radius;
        }
};

int main() {
    Pond p;
    cout << "Area of garden: " << p.Garden::Area() << endl;
    cout << "Area of pond: " << p.Area() << endl;
    return 0;
}

This program first defines a class Garden with a default constructor that reads the length and breadth of the garden. It also has a method Area() that calculates the area of the garden.

Then it defines a subclass Pond that inherits from Garden. It has its own default constructor that reads the radius of the pond, and it overrides the Area() method to calculate the area of the pond instead.

In the main() function, it creates an object of the Pond class, and then it prints out the area of the garden and the area of the pond.

This problem has been solved

Similar Questions

You are given a square piece of land with an area of 1000 square meters. A circular pond is situated within the square land. The radius of the circular pond is 10 meters, and its center is located exactly at the center of the square land. Your task is to remove a rectangular garden inside the square land, parallel to two sides of the square, such that it covers the largest possible area without overlapping with the circular pond.Calculate the dimensions (length and width) of the rectangular garden you need to remove, and determine the remaining area of the square land after the removal.

Create a 2 separate Classes CIRCLE  with private data members - Radius and Area, and RECTANGLE with private data members - Length, Breadth and Area. In each of these classes declare and define the member methods putData() [ To display Area ] and computeArea()[ To Compute Area ] are the private and getData() as public member method to read Radius or Length and Breadth based on the choice('C' or 'R'). As a programmer, Write a java program and subsequent pseudocode, to find the area of circle/rectangle using class CIRCLE/RECTANGLE which have following details:getData() to read either radius or lenght and breadth based on the choice 'C' or 'R'Calculate the area using computeArea()Display the Area using putData().Note :Radius is always +Ve, otherwise print "Invalid Radius"Length and Breadth are +Ve, otherwise print "Invalid Length(if Length is -VE) or Invalid Breadth(if Breadth is -VE) or "Invalid Length and Breadth"Input : First line Read the type ('C'  or 'R')Second line onwards read Radius(if Type = 'C') or Length and Breadth (if Type = 'R')Output :  print Area

Main MethodCreate a main method in the same class:This method will be the entry point for your program and allow user interaction.Inside the main method:Create a Scanner object to read user input.Prompt the user to enter the circle's radius using System.out.print.Use the Scanner object's nextDouble method to read the user-entered radius and store it in a variable.Create a Circle object.Call the setRadius method of the Circle object, passing the user-entered radius as an argument.Call the calculateArea method of the Circle object and store the returned area value in a variable.Print the calculated area to the console using System.out.printlnSubmit the following: Screenshot of source code and output, and Java source code file.

Create an abstract class named shape that contains two integers and an empty method named printarea().Provide three classes named rectangle, triangle and circle such that each one of the classes extends the class Shape. Each of the inherited class from shape class should provide the implementation for the method printarea(). Get the input and calculate the area of rectangle, circle and triangle.In the Main class, create the objects for the three inherited classes and invoke the methods and display the  area values of the different shapes.2(length of rectangle)3(breadth)5(triangle breadth)6(Triangle height)4(circle radius)

Write a C++ program to implement a class called Borewell that has  member variables for radius of the well. Include member function outside the class to calculate the borewell area and circumference.Create object and call the member function and print the results.Sample Input3Sample Output28.2618.84

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.