Write a C++ program Create a base class Mountain with protected data members: mountain name and number of trees.Define a constructor to read the data members. Create a class Coast from Mountain with a static data member to represent the total number of dams. Define a constructor to read the dam details such as dam name, total length, and width of the dam. Define member function “Display” to print the mountain details and the surface area of a dam.surface area of a dam=length*widthCreate a class Dam_detials from Coast with a private data member: depth. Create a parametrized constructor to initialize the value of depth. Define a member function “ Display” to calculate the capacity of your dam.Member function (Display) in Dam_details overrides the member function (Display) in the Coast classCapacity=Volume/1000Volume=Surface area*depthCreate required objects, base pointers and access the member functions. Identify the type of inheritance given.Sample Input5 (Read the depth )Mountain1 (Read the mountain name)20 (Read number of trees)Dam1 (Read dam name)30 (Read length)50(Read width)Sample OutputMountain1 20 treesDam1 area 1500Volume 6000Capacity 6.0Total dam 1
Question
Write a C++ program Create a base class Mountain with protected data members: mountain name and number of trees.Define a constructor to read the data members. Create a class Coast from Mountain with a static data member to represent the total number of dams. Define a constructor to read the dam details such as dam name, total length, and width of the dam. Define member function “Display” to print the mountain details and the surface area of a dam.surface area of a dam=lengthwidthCreate a class Dam_detials from Coast with a private data member: depth. Create a parametrized constructor to initialize the value of depth. Define a member function “ Display” to calculate the capacity of your dam.Member function (Display) in Dam_details overrides the member function (Display) in the Coast classCapacity=Volume/1000Volume=Surface areadepthCreate required objects, base pointers and access the member functions. Identify the type of inheritance given.Sample Input5 (Read the depth )Mountain1 (Read the mountain name)20 (Read number of trees)Dam1 (Read dam name)30 (Read length)50(Read width)Sample OutputMountain1 20 treesDam1 area 1500Volume 6000Capacity 6.0Total dam 1
Solution
Here is the C++ program based on your requirements:
#include<iostream>
using namespace std;
class Mountain {
protected:
string mountain_name;
int num_trees;
public:
Mountain(string m, int n) {
mountain_name = m;
num_trees = n;
}
};
class Coast : public Mountain {
protected:
static int total_dams;
string dam_name;
int length, width;
public:
Coast(string m, int n, string d, int l, int w) : Mountain(m, n) {
dam_name = d;
length = l;
width = w;
total_dams++;
}
void Display() {
cout << mountain_name << " " << num_trees << " trees" << endl;
cout << dam_name << " area " << length * width << endl;
}
};
int Coast::total_dams = 0;
class Dam_details : public Coast {
private:
int depth;
public:
Dam_details(string m, int n, string d, int l, int w, int dp) : Coast(m, n, d, l, w) {
depth = dp;
}
void Display() {
Coast::Display();
int volume = length * width * depth;
cout << "Volume " << volume << endl;
cout << "Capacity " << (float)volume / 1000 << endl;
cout << "Total dam " << total_dams << endl;
}
};
int main() {
Dam_details dam("Mountain1", 20, "Dam1", 30, 50, 5);
dam.Display();
return 0;
}
This program first defines a base class Mountain with protected data members mountain_name and num_trees. Then it defines a derived class Coast with an additional static data member total_dams and other data members. The Display function in Coast class prints the mountain details and the surface area of a dam. Then it defines another derived class Dam_details with an additional private data member depth. The Display function in Dam_details class overrides the one in Coast class and calculates the capacity of the dam. In the main function, it creates an object of Dam_details class and calls the Display function.
Similar Questions
Expected outputMountain1 10 treesDam1 area 1800 Volume 3600Capacity 3.6Total dam 1Your Program OutputEnter mountain name: Enter number of trees: Enter dam name: Enter length of dam: Enter width of dam: Enter depth of dam: 2 0 trees area 0Volume 0Capacity 0Total dam 1
Input2Mountain110Dam12090Expected outputMountain1 10 treesDam1 area 1800 Volume 3600Capacity 3.6Total dam 1Your Program Output2 0 trees area -1253137408Volume -1473544192Capacity -1.47354e+06Total dam
Create a class Building with data members building number, name, area (in square feet),cost/square feet, location, total cost, and year of construction. Declare the cost/square feet asthe static data member. Define a parametric constructor to initialize the data members. Makeuse of this pointer. Create an array of objects and display details of N buildings along with the total cost.
#include <stdio.h>#include <stdlib.h>// Structure for a binary tree nodestruct Node { int data; struct Node* left; struct Node* right;};… int value; scanf("%d", &value); root = insert(root, value); } int maxLevel = -1; printRightView(root, 0, &maxLevel); printf("\n"); return 0;}
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
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.