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

1. What is the error in the following declaration of a constructor for the class Rectangle?
int Rectangle (int length, int height);
Constructors do not have return type. The correct way to declare one is:
Rectangle (int length, int height);
2. Change the definition of the following constructor to use initialization instead of assignment.
Rectangle :: Rectangle (int len, int wid)
{
length = len; height = wid;
}
Initialization is performed when using initialization list, so the answer is:
Rectangle :: Rectangle (int len, int wid) : length(len),width(wid) { }
3. Assume we have the following class definition. Write the definition of the two member functions.
class Fun
{
private: int x;
pubic: Fun (int);
Fun (const Fun&); };

class Fun
{
private:
int x; public:
Fun(int x) : x(x) {};
Fun(const Fun& fn) : x(fn.x) {};
};
4. A point in planar Cartesian coordinates is normally defined with two integer values (x and y). Define a class named Point with two data members. Define a print function that returns the coordinates of a Point object. Define functions to tell the user if a point is on the left side, right side, above, or below another point. Define a function to find the distance between two points as shown below:
#include <iostream>
class Point
{
public: int x; int y;
Point(int x, int y) : x(x), y(y) {};
void print() const
{
std::cout << "Point (" << x << ", " << y << ")" << std::endl;
}
void left_or_right(const Point& other) const
{
if (x > other.x)
std::cout << "Your point on the right side and ";
else if (x < other.x)
std::cout << "Your point on the left side and ";
else
std::cout << "The points have the same x and ";
if (y > other.y)
std::cout << "your point is above";

else if (y < other.y)
std::cout << "your point is below";
else
std::cout << "your point has the same y";
std::cout << std::endl;
}
double distance(const Point& second) const
{
return sqrt(pow(second.x - x, 2) + pow(second.y - y,
2));
}
};
int main()
{
Point myPoint(1, 2);
Point other(3, 3);
myPoint.print();
other.print();
myPoint.left_or_right(other);
std::cout << "Distance is " << myPoint.distance(other);
return EXIT_SUCCESS;
}
5.Define a class named Triangle as follows:
a.Data members are firstSide, secondSide, and thirdSide.
b.Use a function that asserts that the sum of any two sides to be greater than the third one.
c.Accessor member functions are getSides, getPerimeter, and getArea. To find the perimeter and area of a triangle, use the following.
perimeter = a + b + c
area = sqrt ((p) (p - a)
(p - b)
(p - c)) // p = perimeter / 2
d.Define a constructor for the class.

#include <iostream> #include <cassert>
class Triangle
{
public:
double firstSide; double secondSide; double thirdSide;
Triangle(double firstSide, double secondSide, double thirdSide) : firstSide(firstSide), secondSide(secondSide),
thirdSide(thirdSide)
{
assert(firstSide + secondSide > thirdSide
&&firstSide + thirdSide > secondSide
&&secondSide + thirdSide > firstSide);
};
double getPerimeter() const
{
return firstSide + secondSide + thirdSide;
}
double getArea() const
{
const double p = getPerimeter() / 2.;
return sqrt(p * (p - firstSide) * (p - secondSide) * (p - thirdSide));
}
void getSides() const
{
std::cout << '(' << firstSide << ", " << secondSide << ", " << thirdSide << ')' << std::endl;
}
};
int main()
{
Triangle tr(7, 8, 9);
tr.getSides();
std::cout << tr.getPerimeter() <<std::endl; std::cout << tr.getArea() << std::endl;
return EXIT_SUCCESS;
}
6.Define a class named Employee as follows:
a.Data members are name, age, serviceYear, salary.
b.Define a parameter constructor and a destructor.
c.Accessor member functions are getName, getAge, getServiceYear, and getSalary

#include <iostream>
class Employee
{
private:
char* name; int age;
int serviceYear; int salary;
public:
Employee(const char* name, int age, int serviceYear, int salary) : age(age), salary(salary), serviceYear(serviceYear)
{
if (name == nullptr) name = "Name";
this->name = new char[strlen(name) + 1]; std::move(name, name + strlen(name), this->name);
this->name[strlen(name)] = '\0';
}
~Employee()
{
if (this->name) delete[] name;
}
int getAge() const
{
return age;
}
int getSalary() const
{
return salary;
}
int getServiceYear() const
{
return serviceYear;
}
char* getName() const
{
return this->name;
}
};
int main()
{
Employee em("Roman", 2, 5, 12000);
std::cout << em.getName();
}