Object Oriented Programming Lab-03 (GetterSetter, Constructors, MemberFunctions)
Object Oriented Programming Lab-03 (GetterSetter, Constructors, MemberFunctions)
1
Lab Tasks
Task 1:
Write a class named Car that has the following member variables:
• year. An int that holds the car’s model year.
• make. A string that holds the make of the car.
• speed. An int that holds the car’s current speed.
• Constructor.
The constructor should accept the car’s year and make as arguments
and assign these values to the object’s year and make member variables. The constructor
should initialize the speed member variable to 0.
• Accessors.
Appropriate accessor functions should be created to allow values to be
retrieved from an object’s year, make, and speed member variables.
• accelerate.
The accelerate function should add 5 to the speed member variable
each time it is called.
• brake.
The brake function should subtract 5 from the speed member variable each
time it is called.
Demonstrate the class in a program that creates a Car object, and then calls the accelerate
function five times. After each call to the accelerate function, get the current speed of the
car and display it. Then, call the brake function five times. After each call to the brake
function, get the current speed of the car and display it..
2
Task 2: Write a code for a class named Date with three data members day, month and year
which must incorporate the following given main. Write each type of (all possible) constructors
implemented in following main function and getter/setter methods.
int main()
Date D1;
D1.DisplayDate();
D2.DisplayDate();
D3.DisplayDate();
Date Today(1,11,2019); // Constructor with the values of day, Month and year.
Today.DisplayDate();
return 0;
3
Task 3:
Design an Inventory class that can hold information for an item in a retail store’s inventory.
The class should have the following private member variables.