Homework

Classes

 

1    class SaleItem

2    {

3    public:

4           double getprice();

5           void setprice(double);

6           void setName(string);

7           string getName();

8           double TotalItemsPrice(int);

9    private:

10         double price;

11         string name;

12  };

1)      Given the above class definition,  assume the following  declaration in the main program:

 

                                                SaleItem chocolate, candy;

                         double pr;

 

              which of the following is legal in the main program?  If not, why?

 

a)    chocolate.price = .50;

b)    chocolate.setName(kitKat”);

c)    pr = candy.getprice();

d)    chocolate = candy;

e)    cout << candy.getName();

2)      Write the function definitions for the above SaleItem.  The set and get functions set the name and price and return the name and price.  The function TotalItemsPrice takes a parameter that represents the number of items that the user is buying, and returns the total price (price * number of items)