Objectives: The C++ Programming Skills That Should Be Acquired in This Lab
Objectives: The C++ Programming Skills That Should Be Acquired in This Lab
Constructor
Objectives
A constructor will have exact same name as the class and it does not have any
return type at all, not even void. Constructors can be very useful for setting initial
values for certain member variables.
Declaration
class temporary
{
private:
int x;
float y;
public:
// Constructor
temporary()
{ x=5;
y=5.5;
}
... .. ...
};
int main()
{
Temporary t1;
}
Types of Constructors
Lab Task
1. Create a class called time that has separate int member data for hours,
minutes, and seconds. One constructor should initialize this data to 0, and
another should initialize it to fixed values. Another member function should
display it, in 11:59:59 format. The final member function should add two
objects of type time passed as arguments. A main() program should create
two initialized time objects (should they be const?) and one that isn’t
initialized. Then it should add the two initialized values together, leaving the
result in the third time variable. Finally it should display the value of this third
variable. Make appropriate member functions const
2. Create a class named Pizza that stores information of pizzas. It should
contain the following:
Private instance variables to store the size of the pizza (either small, medium,
or large), the number of cheese toppings, the number of pepperoni toppings,
and the number of ham toppings.
Constructor(s) that set all of the instance variables.
Public methods to get the instance variables.
A public method named calcCost( ) that returns a double that is the cost of the
pizza.
Pizza cost is determined by:
Small: $10 + $2 per topping
Medium: $12 + $2 per topping
Large: $14 + $2 per topping
public method named getDescription( ) that returns a String containing the
pizza size,quantity of each topping.
Create a method calcTotal() that returns the cost of your pizzas.
Write test code to order 3 or 4 pizzas and output their description with total
cost of them. For example, a large pizza with one cheese, one pepperoni and
two ham toppings should cost a total of $22.