Write a complete, simple, working C++ code that answers: This is some code that
ID: 3736959 • Letter: W
Question
Write a complete, simple, working C++ code that answers:
This is some code that might help, but did not work. Unfortunately, all i have is what's in the picture.
#include <iostream>
using namespace std;
class Tree {
public:
int age;
double height;
Tree(int age1, double height1) {
age=age1;
height=height1;
}
void setage(int age2) {
age=age2;
}
int getage() {
return age;
}
void setheight(int height2) {
height=height2;
}
int getheight() {
return height;
}
void display() {
cout<<"Age=" << age<<endl;
cout<<"Height=" << height<<endl;
}
void grow() {
cout<<"Growth rate=" << (height/age)<<endl;
}
};
class CoconutTree : public Tree {
private:
int nutNumber;
int leafHeight;
public:
CoconutTree() {
nutNumber=8;
leafHeight=9;
}
void setnutNumber(int nutNumber2) {
nutNumber=nutNumber2;
}
int getnutNumber() {
return nutNumber;
}
void setleafHeight(int leafHeight2) {
leafHeight=leafHeight2;
}
int getleafHeight() {
return leafHeight;
}
void display() {
cout<<"Age=" << age<<endl;
cout<<"Height=" << height<<endl;
cout<<"Number of Nuts= " << nutNumber<<endl;
cout<<"Height of leaf= " << leafHeight<<endl;
}
void bendGrowth(){
cout<<"Bend Growth= " << (nutNumber/leafHeight)<<endl;
}
};
class Willow : public Tree {
private:
int numBranch;
int flexbility;
public:
Willow() {
numBranch=6;
flexbility=7;
}
void setnumBranch(int numBranch2) {
numBranch=numBranch2;
}
int getnumBranch() {
return numBranch;
}
void setflexbility(int flexbility2) {
flexbility=flexbility2;
}
int getflexbility() {
return flexbility;
}
void display() {
cout<<"Age=" << age<<endl;
cout<<"Height=" << height<<endl;
cout<<"Number of Branches= " << numBranch<<endl;
cout<<"Flexibility= " << flexibility<<endl;
}
void swing() {
cout<<"Swing=" << (numBranch/flexibility)<<endl;
}
};
void main() {
CoconutTree coco(6,4);
coco.dispaly();
};
Explanation / Answer
Hii, I have debugged your code. There were some important statements missing and existed some spelling mistakes. The parameterized constructors of the child class should accept all parameters including those for super class constructor and then pass those values (super class attributes) to the super class constructor. Check the completed code below, comments are included wherever code has been modified.
//code.cpp
#include <iostream>
using namespace std;
class Tree {
public:
int age;
int height;
//default constructor
Tree(){
//default values
age=1;
height=1;
}
//parameterized constructor
Tree(int age1, double height1) {
age=age1;
height=height1;
}
//getters and setters
void setage(int age2) {
age=age2;
}
int getage() {
return age;
}
void setheight(int height2) {
height=height2;
}
int getheight() {
return height;
}
//method to display tree details
void display() {
cout<<"Age=" << age<<endl;
cout<<"Height=" << height<<endl;
}
//method to display the growth rate
void grow() {
cout<<"Growth rate=" << (double)height/age<<endl;
}
};
class CoconutTree : public Tree {
private:
int nutNumber;
int leafHeight;
public:
//default constructor
CoconutTree() {
nutNumber=8;
leafHeight=9;
}
//parameterized constructor, will pass age and height to super class constructor
CoconutTree(int _age,int _height,int _nutNumber,int _leafHeight):Tree(_age,_height){
nutNumber=_nutNumber;
leafHeight=_leafHeight;
}
void setnutNumber(int nutNumber2) {
nutNumber=nutNumber2;
}
int getnutNumber() {
return nutNumber;
}
void setleafHeight(int leafHeight2) {
leafHeight=leafHeight2;
}
int getleafHeight() {
return leafHeight;
}
//method to display coconut tree details
void display() {
cout<<"Age=" << age<<endl;
cout<<"Height=" << height<<endl;
cout<<"Number of Nuts= " << nutNumber<<endl;
cout<<"Height of leaf= " << leafHeight<<endl;
}
//method to display the bend growth
void bendGrowth(){
cout<<"Bend Growth= " << (double)nutNumber/leafHeight<<endl;
}
};
class Willow : public Tree {
private:
int numBranch;
int flexibility;
public:
//default constructor
Willow() {
numBranch=6;
flexibility=7;
}
//parameterized constructor, will pass age and height to super class constructor
Willow(int _age, int _height, int _numBranch,int _flexibility):Tree(_age,_height){
numBranch=_numBranch;
flexibility=_flexibility;
}
void setnumBranch(int numBranch2) {
numBranch=numBranch2;
}
int getnumBranch() {
return numBranch;
}
void setflexibility(int _flexibility) {
flexibility=_flexibility;
}
int getflexibility() {
return flexibility;
}
//method to display willow tree details
void display() {
cout<<"Age=" << age<<endl;
cout<<"Height=" << height<<endl;
cout<<"Number of Branches= " << numBranch<<endl;
cout<<"Flexibility= " << flexibility<<endl;
}
//method to display the swing details
void swing() {
cout<<"Swing=" << (double)numBranch/flexibility<<endl;
}
};
int main() {
//creating a tree with age : 10 and height 25
Tree commonTree(10,25);
//creating a coconut tree with age: 2, height: 5, nut number: 6 and leaf growth: 4
CoconutTree coconutTree(2,5,6,4);
//creating a willow tree with age: 2, height: 3, num branch: 4 and flexibility: 5
Willow willowTree(2,3,4,5);
/* displaying all details */
cout<<"Tree"<<endl;
commonTree.display();
commonTree.grow();
cout<<" Coconut Tree"<<endl;
coconutTree.display();
coconutTree.grow();
coconutTree.bendGrowth();
cout<<" Willow Tree"<<endl;
willowTree.display();
willowTree.grow();
willowTree.swing();
return 0;
}
//OUTPUT
Tree
Age=10
Height=25
Growth rate=2.5
Coconut Tree
Age=2
Height=5
Number of Nuts= 6
Height of leaf= 4
Growth rate=2.5
Bend Growth= 1.5
Willow Tree
Age=2
Height=3
Number of Branches= 4
Flexibility= 5
Growth rate=1.5
Swing=0.8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.