Write a complete C program that applies encapsulation principles and defines, im
ID: 3724887 • Letter: W
Question
Write a complete C program that applies encapsulation principles and defines, implements, and tests a Bonsai class with following members. Private properties (All properties must be private to enfonce the encapsulation o smell: string o height: double age: int Constructors No-argument constructor: Initialize all properties to default values Standard constructor: Initialize all properties to user specified values o o Methods For each private property, create one get() method and one set() method to provide o display0: no return -Print out the name and value of every property with proper o makeO: no return - use an output statement to describe how to make a Bonsai. The reading and writing access to this property. prompt. procedure usually includes following steps: ) pick the plant species, 2) decide the size, 3) select the pot, 4 trim and bond, etc. All constructors and methods must be public to allow access from outside of the claass. Inside main0 function, create two Bonsai objects, one from the no-argument constructor, one fromm the standard constructor. Three actual parameters should be read from the user with proper prompts before calling the standard constructor and creating the 2nd object. Then call every method, including get and set methods from every object. Preferably, first call the get) methods with cout statements, then call the set0 methods to change property values. At the end call the display0 method to print the updated property values with proper prompt textExplanation / Answer
#include <iostream>
using namespace std;
class Bonsai
{
private:
string smell;
double height;
int age;
public:
Bonsai()// default constructor
{
smell = "";
height = 0.0;
age = 0;
}
Bonsai(string smell,double height,int age)//argument constructor
{
this->smell = smell;
this->height = height;
this->age = age;
}
void setSmell(string smell)
{
this->smell = smell;
}
string getSmell()
{
return smell;
}
void setHeight(double height)
{
this->height = height;
}
double getHeight()
{
return height;
}
void setAge(int age)
{
this->age = age;
}
int getAge()
{
return age;
}
void display()
{
cout<<" Bonsai : Smell :"<<smell<<" Height : "<<height <<" Age : "<<age;
}
void make()
{
cout<<" 1.Pick the plant species.";
cout<<" 2.Decide the size ";
cout<<" 3.Select the pot";
cout<<" 4.Trim and bond";
}
};
int main() {
string smell;
double height;
int age;
Bonsai b1;
cout<<" Enter the type of smell of bonsai : ";
cin>>smell;
b1.setSmell(smell);
cout<<" Enter the height of bonsai plant in inches : ";
cin>>height;
b1.setHeight(height);
cout<<" Enter the age of bonsai plant : ";
cin>>age;
b1.setAge(age);
b1.display();
Bonsai b2("sweet",4.5,3);
b2.display();
return 0;
}
Output:
Enter the type of smell of bonsai :pleasant
Enter the height of bonsai plant in inches :2.7
Enter the age of bonsai plant :2
Bonsai : Smell :pleasant Height : 2.7 Age : 2
Bonsai : Smell :sweet Height : 4.5 Age : 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.