Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

PL5 / ПЗ5_ООП

.pdf
Скачиваний:
0
Добавлен:
20.12.2024
Размер:
246.13 Кб
Скачать

Міністерство освіти і науки України

Харківський національний університет радіоелектроніки

Кафедра системотехніки Звіт з практичної роботи № 5

З ОСНОВ ПРОЕКТУВАННЯ ТА РЕАЛІЗАЦІЇ ПРОСТИХ КЛАСІВ

Виконали: студенти групи

Перевірили:

КНТ-23-1

доц. Вишняк М. Ю.

Зайцев М. Ю.

 

Бойко А. Ю.

 

Кравченко Р. С.

 

Локтіонов Р. В.

 

Літвін В. О.

 

Харків 2024

05 Pract

Relationships among Classes

PRG-1. Design a class named Student with two data members: name and gpa. Then define a class named Course whose data member capacity holds the number of students in the course and an array of students created in heap memory. The Student class must have a member function to print information about the student object. The Course class must have information about enrolling students in the course and must print information about all students enrolled.

Solving:

#include <iostream>

class Student

{

private:

std::string name; double gpa;

public:

Student() = default;

Student(std::string const& name, double gpa) : name(name), gpa(gpa) { }

//Getter methods

std::string getName() const{ return this->name; } double getGPA() const { return this->gpa; }

//Setter methods

void setName(std::string const& newName) { this->name = newName; } void setGPA(double newGPA) { this->gpa = newGPA; }

//Methods

void printInformation() const

{

std::cout << "Name: " << name << ", GPA: " << gpa << std::endl;

}

};

class Course

{

private:

size_t capacity; size_t length; Student** students;

public:

Course() = delete;

Course(size_t capacity) : capacity(capacity), length(0) 007B

this->students = new Student*[this->capacity];

}

~Course()

{

for (size_t i = 0; i < this->length; i++)

delete this->students[i]; delete[] students;

}

bool enroll(Student* student)

{

if (length >= capacity) return false;

this->students[length] = student; ++length;

return true;

}

void printAllStudents() const

{

for (size_t i = 0; i < length; ++i) students[i]->printInformation();

}

};

PRG-2. Design a class named Course with two data members: name and units. Then design a class named Student with three data members: name, gpa, and a list of courses taken. The list must be implemented as an array in heap memory. Create constructors, destructor, and all necessary member functions for the operation of the Course and Student class. Test both classes in an application program.

Solving:

#include <iostream>

class Course

{

private:

std::string name; int units;

public:

Course(const std::string& name, int units) : name(name), units(units) { }

const std::string& getName() const { return name; } int getUnits() const { return units; }

};

class Student

{

private:

std::string name; double gpa; Course** courses; int numCourses; int capacity;

public:

Student() = default;

Student(const std::string& name, double gpa, int capacity)

: name(name), gpa(gpa), numCourses(0), capacity(capacity)

{

courses = new Course * [capacity];

}

~Student()

{

for (int i = 0; i < numCourses; ++i) delete courses[i];

delete[] courses;

}

bool addCourse(const std::string& courseName, int units)

{

if (numCourses < capacity)

{

courses[numCourses] = new Course(courseName, units); ++numCourses;

return true;

}

else

{

std::cout << "Cannot add more courses. Student's course list is full." << std::endl;

return false;

}

}

void displayCourses() const

{

std::cout << "Courses taken by " << name << ":" << std::endl; for (int i = 0; i < numCourses; ++i)

std::cout << "Course: " << courses[i]->getName() << ", Units: " << courses[i]->getUnits() << std::endl;

}

};

int main()

{

Student* student = new Student("Alice", 3.7, 3); // Capacity for 3 courses

// Add courses student->addCourse("Math", 4); student->addCourse("Physics", 3); student->addCourse("English", 3);

student->displayCourses();

delete student;

return 0;

}

PRG-3. Define a class named Point that represents a point with coordinates x and y. Then write member functions that use the Point class to find the distances between two points. Use the dependency relationship as shown below:

Solution:

#include <iostream>

class Point

{

private: double x; double y;

public:

Point(double x, double y) : x(x), y(y) {}

double getX() const { return x; } double getY() const { return y; }

double distanceTo(Point const& other) const

{

double dx = x - other.x; double dy = y - other.y;

return std::sqrt(dx * dx + dy * dy);

}

};

int main()

{

Point p1(1., 2.); Point p2(4., 6.);

double distance = p1.distanceTo(p2);

std::cout << "Distance between (" << p1.getX() << ", " << p1.getY() << ") and (" << p2.getX() << ", " << p2.getY() << ") is " << distance << std::endl;

return 0;

}