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

Object Oriented Programming.docx

The document outlines a programming lab focused on Object Oriented Programming, consisting of four questions. It includes tasks to create class hierarchies for shapes, vehicles, and employees, detailing constructors, member variables, and functions for each class. Additionally, it discusses access control in inheritance, requiring explanations of accessibility for derived classes from a base class.

Uploaded by

hydrahammad4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Object Oriented Programming.docx

The document outlines a programming lab focused on Object Oriented Programming, consisting of four questions. It includes tasks to create class hierarchies for shapes, vehicles, and employees, detailing constructors, member variables, and functions for each class. Additionally, it discusses access control in inheritance, requiring explanations of accessibility for derived classes from a base class.

Uploaded by

hydrahammad4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Object Oriented Programming

LAB-09
Total Marks (30)

Question 1: The Basic Shape Hierarchy (7 Marks)

Create a base class named Shape with the following:

●​ Protected Member Variables:


o​ string color: To store the color of the shape.
●​ Public Member Functions:
o​ A default constructor that initializes color to "unknown".
o​ A parameterized constructor that takes a string to initialize color.
o​ void setColor(const string& newColor): Sets the color of the shape.
o​ string getColor() const: Returns the color of the shape.
o​ void display(): Prints a generic message like "This is a shape with color:
[color]".

Now, create two derived classes:

●​ 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.

Question 2: The Vehicle Family (8 Marks)

Create a base class named Vehicle with:

●​ Protected Member Variables:


o​ string model: To store the model name.
●​ Public Member Functions:
o​ A constructor that takes a string to initialize the model.
o​ void displayModel(): Prints the model name.

Now, create two derived classes:


●​ Car that inherits publicly from Vehicle. Add a private member variable numberOfDoors
(an integer), a constructor to initialize it and call the base class constructor for the model,
and a function displayDetails() that prints the model and the number of doors.
●​ Motorcycle that inherits publicly from Vehicle. Add a private member variable
hasSidecar (a boolean), a constructor to initialize it and call the base class constructor
for the model, and a function displayDetails() that prints the model and whether it has
a sidecar.

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.

Question 3: Employee Hierarchy (10 Marks)

Create a base class named Employee with:

●​ Protected Member Variables:


o​ string name: To store the employee's name.
o​ int employeeID: To store the employee's ID.
●​ Public Member Functions:
o​ A constructor that takes a name and ID to initialize the member variables.
o​ void displayBasicInfo(): Prints the name and employee ID.

Now, create two derived classes:

●​ 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.

Question 4: Understanding Access with Different Inheritance (5 Marks)

Consider the following base class:

C++
class Base {
public:
int pub = 1;
protected:
int prot = 2;
private:
int priv = 3;
};

Now, consider the following derived classes:

C++
class PublicDerived : public Base {
public:
void show() { cout << pub << " " << prot << endl; /* Can we access priv
here? Why? */ }
};

class ProtectedDerived : protected Base {


public:
void show() {cout << pub << " " << prot << endl; }
};

class PrivateDerived : private Base {


public:
void show() {cout << pub << " " << prot << endl; }
};

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.

You might also like