0% found this document useful (0 votes)
32 views6 pages

OOP - Midterm - BSCS - Fall2024 - Solution

uol opp midtream paper
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views6 pages

OOP - Midterm - BSCS - Fall2024 - Solution

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

The University of Lahore

Department of Computer Science& IT


Midterm Examination
Fall-2024
Course Title: Object Oriented Programming Course Code: CS02203||11 Credit Hours: 4
Course Instructors: Ms. Mariya Bibi, Mr. Ali Raza, Mr. Majid Program Name: BSCS
Hussain, Ms. Mishal Muneer, Mr. Ahmad
Zia, Mr. Sana Ullah, Mr. H Ali Younas, Mr.
Noman Ali, Mr. Abdul Hannan
Time Allowed: 60 Minutes Maximum Marks: 20
Date: 26-11-2024 Course Mentor: Dr. Ghulam Farooque
Student’s Name:
Signature:
Reg. No: Section:
Important Instructions / Guidelines:
 Attempt the objective part on the question paper before moving to the subjective part.
 Solve the subjective part on the answer sheet provided separately.

Time Allowed: 15 Minutes

Objective Part [Marks = 5]


Question # 1: Write the output of the following code. Assume that all the necessary header files are included. [Marks = 2.5*2]

A: length = 4 and width = 5


class Rectangle { length = 7 and width = 3
private: The area of the passed rectangle is: 21
int length; Rectangle with length = 7 and width = 3 is
int width; destroyed
public: Rectangle with length = 4 and width = 5 is
Rectangle(int l, int w) : length(l), width(w) { destroyed
cout << " length = " << length << " and width = " <<
width << endl;
}
int calculateArea() {
return length * width;
}
void displayAreaOfAnotherRectangle( Rectangle& other) {
cout << "The area of the passed rectangle is: " <<
other.calculateArea() << endl;
}
~Rectangle() {
cout << "Rectangle with length = " << length << " and
width = " << width << " is destroyed" << endl; }
};

int main() {
Rectangle rect1(4, 5);
Page 1 of 3
Rectangle rect2(7, 3);
rect1.displayAreaOfAnotherRectangle(rect2);
return 0;
}
2. employee class
class Employee{ Parametrized constructor
int id; Copy Constructor
string name; Employee id is 1
Employee name is Mr. Khan
public:
Employee(){
cout<<"employee class"<<endl;
}
Employee(int i, string n){
id = i;
name = n;
cout<<”Parametrized constructor\n”;
}
Employee(Employee &e){
id = e.id;
name = e.name;
cout<<"Copy Constructor\n";
}
void Display(employee e){
cout<<"Employee id is "<<e.id<<endl;
cout<<"Employee name is "<<e.name<<endl;

}
};
main(){
Employee e;
Employee e2(1, "Mr. Khan");
e.Display(e2);
}

Page 2 of 3
Object Oriented Programming -Mid term: F-24 Time:45 minutes

Subjective Part Total Marks: 15

Question # 2: Write a C++ code of the following [8 Marks]

Assume a creative agency that contains the following classes. Create a base class Employee that contains
some data members to store the common employee details such as empID, empName, and empRole. This class should
also have a function displayInfo() to display the employee's basic details.

Then, create two derived classes:

1. GraphicDesigner: This class will inherit from Employee and add a data member designSoftware to store the
primary design software they use (e.g., Photoshop, Illustrator, etc.). The GraphicDesigner class should also have a
displayInfo() function that overrides the base class's function to include the software they specialize in, as well as
a method estimateProjectCost() to estimate the cost of a design project based on hours worked and hourly rate .
2. Copywriter: This class will also inherit from Employee and add a data member writingStyle to store the writing
style or type of content they specialize in (e.g., creative, technical, SEO-focused). The Copywriter class should
override the displayInfo() function to include their writing style and have a method estimateProjectCost() that
calculates the project cost based on the number of words written and a per-word rate .

Both derived classes will implement their own version of the estimateProjectCost() function, allowing them to
calculate project costs based on their specific work type (design for the GraphicDesigner and writing for the
Copywriter).

At the end create a main() function to instantiate objects of both GraphicDesigner and Copywriter, and display
their information, including the estimated project cost for a given project.

Note: You can assume any value for per_word_rate and hourly_rate

Code:

#include <iostream>
#include <string>
using namespace std;

// Base class
class Employee {
protected:
int empID;
string empName;
string empRole;

public:
Employee(int id, string name, string role)
: empID(id), empName(name), empRole(role) {}

void displayInfo() {
cout << "Employee ID: " << empID << endl;
cout << "Employee Name: " << empName << endl;
cout << "Employee Role: " << empRole << endl;
}

};

// Derived class: GraphicDesigner


class GraphicDesigner : public Employee {
Page 3 of 3
private:
string designSoftware;

public:
GraphicDesigner(int id, string name, string role, string software)
: Employee(id, name, role), designSoftware(software) {}

void displayInfo() {
Employee::displayInfo();
cout << "Design Software: " << designSoftware << endl;
}

double estimateProjectCost(int hoursWorked, double hourlyRate) {


return hoursWorked * hourlyRate;
}
};

// Derived class: Copywriter


class Copywriter : public Employee {
private:
string writingStyle;

public:
Copywriter(int id, string name, string role, string style)
: Employee(id, name, role), writingStyle(style) {}

void displayInfo() {
Employee::displayInfo();
cout << "Writing Style: " << writingStyle << endl;
}

double estimateProjectCost(int wordsWritten, double perWordRate) {


return wordsWritten * perWordRate;
}
};

// Main function
int main() {
// Create a GraphicDesigner object
GraphicDesigner gd(101, "Alice", "Graphic Designer", "Photoshop");
cout << "Graphic Designer Details:" << endl;
gd.displayInfo();
cout << "Estimated Project Cost: $"
<< gd.estimateProjectCost(10, 50.0) << endl; // 10 hours, $50/hour

cout << endl;

// Create a Copywriter object


Copywriter cw(102, "Bob", "Copywriter", "SEO-focused");
cout << "Copywriter Details:" << endl;
cw.displayInfo();
cout << "Estimated Project Cost: $"
<< cw.estimateProjectCost(1000, 0.10) << endl; // 1000 words, $0.10/word

return 0;
}

Question # 3: Write a C++ code of the following [7 Marks]

Page 4 of 3
Create a class named Product with the following private data members: product_name, price, and quantity. And
implements the following methods in the class.

1. Implement a copy constructor() that initializes the product_name and price of the new object using the
original object but sets quantity to zero.
2. setter() function to set the quantity of new object.
3. compare() function to compare the quantity of the original object and new object and display the details of
the object having lower quantity.

In the main() function do the following.

1. Create an original Product object named as saltedLays and initialize it with values.
2. Use the copy constructor to create a new Product object named as maslaLays based on the original object.
3. Set the quantity for the new object using the setter() function.
4. Call the compare() function to compare the quantity of saltedLays and masalaLays and display the details of the
5.
object having lower values.

Code:
#include <iostream>
#include <string>
using namespace std;

class Product {
private:
string product_name;
double price;
int quantity;

public:
// Constructor
Product(string name, double pr, int qty)
: product_name(name), price(pr), quantity(qty) {}

// Copy constructor
Product(const Product& original)
: product_name(original.product_name), price(original.price),
quantity(0) {}

// Setter for quantity


void setQuantity(int qty) {
quantity = qty;
}

// Function to display product details


void displayDetails() const {
cout << "Product Name: " << product_name << endl;
cout << "Price: $" << price << endl;
cout << "Quantity: " << quantity << endl;
}

// Compare function
static void compare(const Product& p1, const Product& p2) {
if (p1.quantity < p2.quantity) {
cout << "Product with lower quantity:" << endl;
p1.displayDetails();
} else if (p2.quantity < p1.quantity) {
cout << "Product with lower quantity:" << endl;
p2.displayDetails();
} else {
Page 5 of 3
cout << "Both products have the same quantity." << endl;
p1.displayDetails();
p2.displayDetails();
}
}
};

int main() {
// Create the original Product object
Product saltedLays("Salted Lays", 2.5, 50);

// Create a new Product object using the copy constructor


Product masalaLays = saltedLays;

// Set the quantity for the new Product object


masalaLays.setQuantity(30);

// Compare the quantities of the two products


cout << "Comparing products:" << endl;
Product::compare(saltedLays, masalaLays);

return 0;
}

Page 6 of 3

You might also like