8. Consider the detinition of the following class class produetType public //Lin
ID: 3910554 • Letter: 8
Question
8. Consider the detinition of the following class class produetType public //Line 1 //Line 2 //Line3 /Line /Line 5 productType) productType (int, double, double) productType (atring, int, double, doubl)Line 6 productType(string, string, string Int, doubie, double) //Line7 void set (atring, string, atring, int double, double) /Line 8 /Line 9 void print eonati void setQuantitiesInStock (int x) void updateQuantitiesInStock(int x) int getQuantitiesinstock) const: //Line 10o /Line 11 //Line 12 void setPrice (double x) double getPrice) conat void setDiacount (double d) double getDiscount ) consti //Line 13 /Line 14 /Line 15 //Line 16 //Line 17 //Line 18 //Lin?19 //Line 20 /Line 21 /Line 22 //Line 2? /Line 24 private string productHane: utring id string manufactureri int quantitiesInstock double price: doubl) disount? )r a. Give the line number containing the eanstructor that is executed in each of the following declarations. iproductType productl; iL productType produot2("Microwave", "a3562", "GeneralPool, 35, 175.00, 0.1) ii productType product3"D1290" 25 375.00. 0.05), v productype product(0, 8.50, 0.2) b, write the definition ofthe constructor in Line 4 so that the instance variables are initialized to " ". "", "" 0.0·0, and o. 0" respectively. c. Write the definition of the constructor in Line 5 so that the string instance variables are initialized to", and the other instance variables are initialized according to the parameters. Instance variables quantitiesinStock,price, and discount must be nonnegative d write the definition ofthe constructor 11 Line 6 so that the instance variables pro uota e and anuf otur ?are nitialized to the empty strlng nstance variable id is iti lized according to the first parameter and the rema ning stance variables are initallzed according to the last three parameters. Instance varlables quantitiesinstook price and discount must be nonnegative. e. Write the definition of the constructor in Line 7 so that the instance variables are initialized according to the parameters. Instance variables quantitiesInStock, price, and discount must be nonnegative.Explanation / Answer
Answer:
a.
i) productType product1; (creating product1 object of class productType) : The constructor in Line4 will be executed. Because, the default constructor is called when we create an object for the class.
ii) productType product2("Microwave","M3562","GeneralPool",35,175.00,0.1); (creating product2 object of class productType) : The constructor in Line7 will be executed. Because it is the parameterized constructor with the parameters of type string, string, string, int, double, double respectively.
iii) productType product3("D1290",25,375.00,0.05); (creating product3 object of class productType) : The constructor in Line6 will be executed. Because it is the parameterized constructor with the parameters of type string, int, double, double respectively.
iv) productType product4(10,8.50,0.2); (creating product4 object of class productType) : The constructor in Line5 will be executed. Because it is the parameterized constructor with the parameters of type int, double, double respectively.
b. Definition of constructor in Line4
productType(): productName(""), id(""), manufacturer(""), quantitiesInStock(0), price(0.0), discount(0.0)
{
}
c. Definition of constructor in Line5
The object is productType product2(25,100.54,12.58);
productType(int q,double p,double d)
{
cout<<"Enter product name:";
cin>>productName;
cout<<"Enter product id:";
cin>>id;
cout<<"Enter product manufacturer:";
cin>>manufacturer;
quantitiesInStock=q;
price=p;
discount=d;
}
d. Definition of constructor in Line6
The object is productType product3("xyz",25,100.54,12.58);
productType(string s1,int k,double p,double d)
{
productName="";
id=s1;
manufacturer="";
quantitiesInStock=k;
price=p;
discount=d;
}
e. Definition of constructor in Line7
The object is productType product4("xyz","M123","ABC",25,100.54,12.58);
productType(string s1,string s2,string s3,int q,double p,double d)
{
productName=s1;
id=s2;
manufacturer=s3;
quantitiesInStock=q;
price=p;
discount=d;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.