6)Create a class named IceCreamCone with fields for flavor, number of scoops, type of cone, and price. Unless arguments are supplied, flavor defaults to “Vanilla”, number of scoops defaults to 1, and cone type defaults to “Sugar”. The method calculates the price based on Rs.75 per scoop, with an additional Rs.40 for a waffle cone. Write a C++ program to calculate the price.
#include<iostream>
#include<string>
using namespace std;
class IceCreamCone
{
private :
string flavor;
int numScoops;
string coneType;
double price;
public:
IceCreamCone(string ="Vanilla",int =1,string ="sugar");
void calculate();
void print();
};
IceCreamCone::IceCreamCone(string flavor,int numScoops,string coneType)
{
this->flavor=flavor;
this->numScoops=numScoops;
this->coneType=coneType;
calculate();
}
void IceCreamCone::calculate()
{
const int CENTS_PER_SCOOP=70;
const int CENTS_PER_WAFFLE=40;
if(coneType=="waffle")
price=numScoops*CENTS_PER_SCOOP+CENTS_PER_WAFFLE;
else
price=numScoops*CENTS_PER_SCOOP;
}
void IceCreamCone::print()
{
cout<<"\nIceCreamCone object details";
cout<<"\nFlavor : "<<flavor;
cout<<"\nNumber of scoops : "<<numScoops;
cout<<"\nType of Cone : "<<coneType;
cout<<"\nCost : "<<price;
}
int main()
{
IceCreamCone icecream1;
icecream1.print();
IceCreamCone icecream2("Strawberry",5,"waffle");
icecream2.print();
}
Comments
Leave a comment