OOP - Midterm - BSCS - Fall2024 - Solution
OOP - Midterm - BSCS - Fall2024 - Solution
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
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.
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;
}
};
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;
}
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;
}
// 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
return 0;
}
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.
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) {}
// 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);
return 0;
}
Page 6 of 3