
PL4 / ПЗ4_ООП
.pdfМіністерство освіти і науки України
Харківський національний університет радіоелектроніки
Кафедра системотехніки Звіт з практичної роботи № 4
З ОСНОВ ПРОЕКТУВАННЯ ТА РЕАЛІЗАЦІЇ ПРОСТИХ КЛАСІВ
Виконали: студенти групи |
Перевірили: |
КНТ-23-1 |
доц. Вишняк М. Ю. |
Зайцев М. Ю. |
|
Бойко А. Ю. |
|
Кравченко Р. С. |
|
Локтіонов Р. В. |
|
Літвін В. О. |
|
Харків 2024

Inheritance
1. Assume we have defined the following two classes. Write the definition of the set and print functions for both classes using delegation.
class First
{
private: int a;
public:
void set(int a); void print() const;
};
// Declaration of the class Second class Second : public First
{
private: int b;
public:
void set(int a, int b); void print() const;
};
Solving:
#include <iostream>
class First
{
private:
int a; public:
void set(int a) { this->a = a; }
void print() const { std::cout<<"First: " << this->a << std::endl;
};
};
// Declaration of the class Second class Second : public First
{
private:
int b; public:
void set(int a, int b) { First::set(a), this->b = b; };
void print() const { First::print(), std::cout << "Second: " << b << std::endl; };
};

2. Assume we have a class First defined as follows. If class Second is a public derivative from class First, what is the accessibility of members one, two, and set in class Second? class First
{
private:
int one; protected:
int two; public:
void set(int one, int two);
}
Solving:
•One is a private member of First. It remains private and is not accessible directly in Second.
•Two is a protected member of First. It remains protected in Second and is accessible within Second and its derived classes.
•Set is a public member function of First. It remains public in Second and can be accessed from outside the class.
3.Design a class name Square that defines a square geometric shape. The class must have a data member named side that defines the length of each side. Then define two member functions, getPeri and getArea, to find the perimeter and area of the square shape. Now define a class Cube that defines a cubic shape and inherits from the Square class. The class Cube needs no new data members, but it needs the member functions getArea and getVolume. Provide the appropriate constructors and destructors for both classes.
Solving:
class Square
{
protected:
double side;
public:

Square() = default;
explicit Square(const double s) : side(s) {}
virtual ~Square() = default;
double getSide() const { return this->side; } void setSide(double s) { this->side = s; }
virtual double getPeri() const { return this->side * 4; }
virtual double getArea() const { return std::pow(this->side, 2); }
};
class Cube : public Square
{
public:
//Inherit all constructors from 'Square' using Square::Square;
~Cube() override = default;
double getPeri() const override { return this->side * 6; }
double getArea() const override { return 6 * Square::getArea(); } double getVolume() const{ return std::pow(this->side, 3); }
};
4. Design a class named Rectangle with two private data members: length and width. Define constructors and a destructor for the class and write member functions to find the perimeter and area of a rectangle. Then define a class named Cuboid (representing a box) that inherits from the class Rectangle with an extra data member: height. Then write constructors and a destructor for the Cuboid class, and write member functions to find the surface and volume of the Cuboid objects.
Solving:
class Rectangle
{
protected:
double length; double width;
public:
Rectangle() = default;
explicit Rectangle(double l, double w) : length(l), width(w) {} virtual ~Rectangle() = default;
virtual double getPeri() const { return 2 * (this->length + this- >width); }
virtual double getArea() const { return this->length * this->width;
}
};
class Cuboid : public Rectangle

{
protected:
double height;
public:
Cuboid() = default;
Cuboid(double l, double w, double h) : Rectangle(w, l), height(h) {} ~Cuboid() override = default;
double getVolume() const { return this->width * this->length * this- >height; }
double getSurfaceArea() const
{
return 2 * (this->height * this->width + this->height * this->length + this->length * this->width);
}
};
5. Create a simple employee class that supports two classifications of employees, salaried and hourly. All employees have four data members: name, employee number, birth date, and date hired. Salaried employees have a monthly salary and an annual bonus, which is stated as a percentage in the range of 0 to 10 percent. Hourly employees have an hourly wage and an overtime rate which ranges from 50 to 100 percent.
Solving:
#include <iostream>
class Employee
{
protected: std::string name;
int employeeNumber; std::string birthDate; std::string dateHired;
public:
Employee(const std::string& name, int employeeNumber, const std::string& birthDate, const std::string& dateHired)
: name(name), employeeNumber(employeeNumber), birthDate(birthDate), dateHired(dateHired) {}
virtual ~Employee() = default;
virtual double calculatePay() const = 0;
virtual void displayInfo() const
{
std::cout << "Name: " << name << std::endl;
std::cout << "Employee Number: " << employeeNumber << std::endl;

std::cout << "Birth Date: " << birthDate << std::endl; std::cout << "Date Hired: " << dateHired << std::endl;
}
};
class SalariedEmployee : public Employee
{
private:
double monthlySalary; double annualBonusPercent;
public:
SalariedEmployee(const std::string& name, int employeeNumber, const std::string& birthDate, const std::string& dateHired,
double monthlySalary, double annualBonusPercent)
: Employee(name, employeeNumber, birthDate, dateHired), monthlySalary(monthlySalary), annualBonusPercent(annualBonusPercent) {}
~SalariedEmployee() override = default;
double calculatePay() const override { return monthlySalary * (1 + annualBonusPercent / 100.); }
void displayInfo() const override
{
std::cout << "Salaried Employee" << std::endl; Employee::displayInfo();
std::cout << "Monthly Salary: $" << monthlySalary << std::endl; std::cout << "Annual Bonus Percent: " << annualBonusPercent << "%"
<< std::endl;
}
};
class HourlyEmployee : public Employee
{
private:
double hourlyWage;
double overtimeRatePercent;
public:
HourlyEmployee(const std::string& name, int employeeNumber, const std::string& birthDate, const std::string& dateHired,
double hourlyWage, double overtimeRatePercent)
: Employee(name, employeeNumber, birthDate, dateHired), hourlyWage(hourlyWage), overtimeRatePercent(overtimeRatePercent) {}
~HourlyEmployee() override = default;
double calculatePay() const override
{
const int hoursPerWeek = 40;
double overtimePay = hoursPerWeek * (1 + overtimeRatePercent / 100) * hourlyWage;
double totalpay = hoursPerWeek * hourlyWage;
return totalpay + overtimePay;
}
void displayInfo() const override
{
std::cout << "Hourly Employee" << std::endl; Employee::displayInfo();

std::cout << "Hourly Wage: $" << hourlyWage << std::endl; std::cout << "Overtime Rate Percent: " << overtimeRatePercent <<
"%" << std::endl;
}
};
int main() {
SalariedEmployee salariedEmployee("John Doe", 1001, "1990-01-01", "2020-01-01", 5000, 5);
HourlyEmployee hourlyEmployee("Jane Smith", 1002, "1995-05-15", "2020- 01-01", 20, 75);
salariedEmployee.displayInfo();
std::cout << "Monthly Pay: $" << salariedEmployee.calculatePay() << std::endl << std::endl;
hourlyEmployee.displayInfo();
std::cout << "Monthly Pay: $" << hourlyEmployee.calculatePay() * 4 << std::endl; // Assuming a 4-week month
return 0;
}
6.Write a program that simulates the three classes defined as shown below:
a)Use name as the only data member for the Person class.
b)Use name and gpa as data members for the Student class.
c)Use name and salary as data members for the Employee class
Solution:
#include <string>
class Person
{
protected: std::string name;
public:
Person() = default;
explicit Person(const std::string& name) : name(name) { }
std::string getName() const { return name; }
};
class Student : public Person
{
private:

double gpa;
public:
Student() = default;
explicit Student(const std::string& name, double gpa) : Person(name), gpa(gpa) { }
double getGpa() const { return gpa; }
};
class Employee : public Person
{
protected:
double salary;
public:
Employee() = default;
explicit Employee(const std::string& name, double salary) : Person(name), salary(salary) { }
double getSalary() const { return salary; }
};