You are required to write a program to build a class JewelleryItem. A jewellery Item has a type (string) , price (float),
You are required to write a program to build a class JewelleryItem. A jewellery Item has a type (string) , price (float),
material (string), daysToGo (int).
These attributes represent the type i.e ‘ear ring’, necklace, finger ring etc. The material can be gold,
silver, plastic etc. The daysToGo attribute represents the number of days it will remain fit for use.
1. You are required to provide the following parameterized constructors
JewelleryItem(string type, float price, string material, int daysToGo)
Jewlleryitem(string type, float price, int daysToGo) // the material will get “gold” as default
JewelleryItem(string type, float price) //the material defaults to gold and daysToGo defaults to
100.
2. Now provide getters for each of the attributes above but setters for only price, daysToGo
3. Provide a function/method makeUse(). This function represents the use of this item for one day
which reduces the daysToGo by 1. This function returns true if the daysToGo is greater than 1 and
false otherwise. This means that the item can be used if the daysToGo has not become zero.
4. Provide another function bool polish(int goodForDays). This function represents the scenario that
the jewelry item has been polished and can be used for goodForDays more days. But this function
will work only for the items whose material is gold or silver. E.g if j.polish(100) is called, then if the
item j is made of gold or silver than the daysToGo is increased by 100 days.
5. Provide function print() that prints all the data members.
6. Test your program by writing main and calling different functions.
#include<iostream>
#include<string>
using namespace std;
//declaring a class
class JewelleryItem{
//decalring the variables as private
private:
string type;
float price;
string material;
int daysToGo;
//defining methods as public
public:
//constructor with 4 parameters
JewelleryItem(string type, float price, string material, int daysToGo){
//setting the instance variables of current object with passed values
this->type=type;
this->price=price;
this->material=material;
this->daysToGo=daysToGo;
}
//constructor with 3 parameters
JewelleryItem(string type, float price, int daysToGo){
//setting the instance variables of current object with passed values
this->type=type;
this->price=price;
this->material="gold"; //setting to gold by default if not specified
this->daysToGo=daysToGo;
}
//constructor with 2 parameters
JewelleryItem(string type, float price){
//setting the instance variables of current object with passed values
this->type=type;
this->price=price;
this->material="gold";
this->daysToGo=100; //setting to 100 if not specified
}
//getter for type
string getType(){
return this->type;
}
//getter for price
float getPrice(){
return this->price;
}
//getter for material
string getMaterial(){
return this->material;
}
//getter for daysToGo
int getDaysToGo(){
return this->daysToGo;
}
//setter for price
void setPrice(float price){
this->price=price;
}
//setter for daysToGo
void setDaysToGo(int daysToGo){
this->daysToGo=daysToGo;
}
//makeUse() method returning boolean value
bool makeUse(){
this->daysToGo= this->daysToGo - 1; //reducing daysToGo by 1
if(this->daysToGo>0){
return true; //returning true if daysToGo is greater than 1
}
return false; //in else case returning false
}
//method to increase lifetime
bool polish(int goodForDays){
if(this->material=="gold" || this->material=="silver"){
this->daysToGo= this->daysToGo + goodForDays; //increasing daysToGo by goodForDays value
return true; //return true if days has been updated
}
return false; //in else case return false
}
//printing all the data members
void print(){
cout << "Type is: " << this->type << endl;
cout << "Price is: " << this->price << endl;
cout << "Material is: " << this->material << endl;
cout << "Lifetime is: " << this->daysToGo << endl;
}
};
int main()
{
JewelleryItem item1("Bracelet",5000,"gold",365);
item1.makeUse(); //decreasing lifetime by a day
item1.polish(365); //increasing lifetime by 365 days
item1.print(); //printing all the details
return 0;
}
Step by step
Solved in 2 steps with 1 images