0% found this document useful (0 votes)
252 views

Objectives: The C++ Programming Skills That Should Be Acquired in This Lab

Lab Task 1 involves creating a Time class with hours, minutes, seconds data members and different constructors to initialize them, a display function in 11:59:59 format, and a function to add two Time objects

Uploaded by

student ww
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
252 views

Objectives: The C++ Programming Skills That Should Be Acquired in This Lab

Lab Task 1 involves creating a Time class with hours, minutes, seconds data members and different constructors to initialize them, a display function in 11:59:59 format, and a function to add two Time objects

Uploaded by

student ww
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab 6

Constructor

Objectives

The C++ programming skills that should be acquired in this lab:


 
 To understand the concept of Constructor.
 To understand how to declare Constructor.
 Types of Constructor and Implementation in C++.
Constructor

A class constructor is a special member function of a class that is executed


whenever we create new objects of that class.

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

1. Default Constructors: Default constructor is the constructor which doesn’t


take any argument. It has no parameters.
Output:

Enter length and breadth respectively: 6


7
Area: 42
Default Area when value is not taken from user
Area: 10

2. Parameterized Constructors/ Constructor Overloading:


Constructor can be overloaded in a similar way as function overloading.
Overloaded constructors have the same name (name of the class) but different
number of arguments. Depending upon the number and type of arguments
passed, specific constructor is called. Since, there are multiple constructors
present, argument to the constructor should also be passed while creating an
object.
Output:

Default Area when no argument is passed.


Area: 10
Area when (2,1) is passed as argument.
Area:2

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.

You might also like