Object Oriented Programming.docx
Object Oriented Programming.docx
LAB-09
Total Marks (30)
● Rectangle that inherits publicly from Shape. Add private member variables width and
height (both integers), a constructor to initialize them and call the base class constructor
for color, and a function calculateArea() that returns the area. Override the display()
function to also show the dimensions.
● Circle that inherits publicly from Shape. Add a private member variable radius (an
integer), a constructor to initialize it and call the base class constructor for color, and a
function calculateArea() that returns the area (you can use a simplified formula like
3.14 * radius * radius). Override the display() function to also show the radius.
Write a main() function that creates objects of both Rectangle and Circle, sets their properties,
and calls their display() and calculateArea() functions.
Write a main() function that creates objects of both Car and Motorcycle, sets their properties in
the constructors, and calls their displayModel() and displayDetails() functions.
● SalariedEmployee that inherits publicly from Employee. Add a private member variable
monthlySalary (a double), a constructor to initialize it and call the base class constructor,
and a function calculateAnnualSalary() that returns the annual salary. Override the
displayBasicInfo() to also show the monthly salary.
● HourlyEmployee that inherits publicly from Employee. Add private member variables
hourlyRate (a double) and hoursWorked (a double), a constructor to initialize them and
call the base class constructor, and a function calculateWeeklyPay() that returns the
weekly pay. Override the displayBasicInfo() to also show the hourly rate and hours
worked.
Write a main() function that creates objects of both SalariedEmployee and HourlyEmployee,
sets their properties, and calls their displayBasicInfo() and their respective pay calculation
functions.
C++
class Base {
public:
int pub = 1;
protected:
int prot = 2;
private:
int priv = 3;
};
C++
class PublicDerived : public Base {
public:
void show() { cout << pub << " " << prot << endl; /* Can we access priv
here? Why? */ }
};
In the show() function of each derived class, can you access pub, prot, and priv from the Base
class? Explain why or why not for each case. Also, in the main() function, try to access pub
from objects of PublicDerived, ProtectedDerived, and PrivateDerived. Explain the
accessibility in each scenario.