C++ Problems: Any help is appriciated. Write an entire class called Toy that sto
ID: 3884666 • Letter: C
Question
C++ Problems:
Any help is appriciated.
Write an entire class called Toy that stores an instance variable named description for the description of the toy as a string. Write the corresponding getter and setter methods (getDescription, setDescription), and a constructor that takes a string parameter for the description of the toy Include both the class declaration and method definitionsI Be sure to use appropriate visibility modiniers Assume all includes and using namespace std are already in the file. Do not try to make header files. Answer: (penalty regime: 05.10, %) 1 class Toy string description; 4 6 Toy::ToyO0/ default constructor 7 Toy::Toy(string d) description d; 10 12 string Toy: :getDescription 14 return description; 16 void Toy: :setDescription(string description)t 17 toyDescription description; 18Explanation / Answer
#include<iostream>
#include<string.h>
using namespace std;
class Toy
{
private:
string description; //class varible for description of toy
public:
Toy(); //default constructor
Toy(string des); //parameterised constructor
string getDescription(); //getter
void setDescription(string des); //setter
};
Toy::Toy()
{
description="NULL";
}
Toy::Toy(string des)
{
description=des;
}
string Toy::getDescription()
{
return description;
}
void Toy:: setDescription(string des)
{
description=des;
}
int main()
{
string des,result;
Toy T,T1("Doll");
cout << "Default Constructor: ";
result = T.getDescription();
cout<<result<<endl;
cout << "Constructor with parameter: ";
result = T1.getDescription();
cout<<result<<endl;
return 0;
}
Output:
Default Constructor: NULL
Constructor with parameter: Doll
https://drive.google.com/open?id=0B482UDpVXbMvbldCS3dKNGY3TFU
sharinging image for the output
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.