Practicing on Dynamic Memory Allocation with Course Class You will work on the Course Class which is given in Course.h f

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Practicing on Dynamic Memory Allocation with Course Class You will work on the Course Class which is given in Course.h f

Post by answerhappygod »

Practicing on Dynamic Memory Allocation with Course Class
You will work on the Course Class which is given in Course.h
file.
This class has a courseName , a capacity and a section to
declare the course info. Students can register for the course using
their names. Course class stores all students' names in a dynamic
array. When a course instance is created, a dynamic array is
allocated with the size of course capacity. When a student is added
to the course or a student drops the course, the value of the
numberOfStudents has updated. In other words, numberOfStudents
shows the current fullness of the course; capacity shows the
available maximum number of students for the course. The capacity
value should be represented the dynamic array size in all
cases.
Constructor: The course instance is created
using 3 parameter values. The number of students enrolled in the
course should be set to zero, a dynamic array is allocated with the
size of course capacity for future student enrollments. The
constructor ends with printing the confirmation message (see
example ) to the console.
Example:
Data Structures-B has been created!
Destructor: It prevents the memory leak. The
destructor ends with printing the confirmation message (see
example) to the console.
Example:
Data Structures-B has been deleted!
Copy Constructor: It creates a copy course
instance of a given course instance in the parameter list using
deep copy. The copy construction ends with printing the
confirmation message (see example ) to the console.
Example:
New Data Structures-B has been created by copy constructor!
Add the name to the course's student list as a last element of
the array by checking if there is available room in the array for
the student. The method ends with printing the confirmation message
(see example ) to the console if the student is added, otherwise'
printing a warning message as shown below.
Example:
Peter Jones was added to Data Structures-A successfully
or
The course Data Structures-A has reached maximum capacity! You
need to increase the capacity!!
If there is a student whose name is the same as the parameter in
the student array, the name is deleted from the array by moving the
last student to the dropped student's place. By doing this, we have
a more efficient drop procedure compared to the shift processing in
the array. The method ends by printing the confirmation message
(see the unit test ) to the console.
Example:
Student: Ahmad Nas dropped the course Computer Network-C
or
Student: Kaan Pease was not found!
If capacity is greater than the NumberOfStudents ,the size of
the dynamic array shrinks to the value of the NumberOfStudents ,
this would affect the dynamic array. After completing the process,
it prints the confirmation message (see example), otherwise prints
a message like "No need to shrink !! Capacity is equal to the
number of students for Database Systems"
Example after shrinking the size:
Capacity of Data Structures-A is now equal to number of
students
Increase the course capacity by the value of parameter inc. It
should affect the dynamic array as well. The method ends by
printing the confirmation message (see the example ) to the
console.
Example: Capacity of Data Structures-A has been increased by
3
It prints the course info to the ostream object as shown in the
example
Or
It assigns the given course instance to another course instance
using deep copy. The copy assignment operation ends with printing
the confirmation message (see the example ) to the console.
Example:
The content of Database Systems-C was copied to Special Topics-X
using operator=
It updates the name of the course and prints the confirmation
message to the console (see example)
Example: Course name Database Systems-C has been changed to
Computer Network-C
It updates the section of the course and prints the confirmation
message to the console (see example)
Example: Course section Database Systems-B has been changed to
C
It returns the student name located at the index in the
array
The operator adds the students of the course object argument to
the current course object. To do this, it updates the current
object capacity to the total capacity of the current and argument
objects. The new capacity value affects the dynamic array of the
current object. You need to update the numberOfstudent member of
the current object as well. The operator returns the current
object’s reference.
main.cpp:
#include <iostream>
#include<fstream>
#include "course.h"
int main(int argc, char * argv[])
{
// Check if number of command-line arguments is 2 or not. If
not, print a error message

// Create a ofstream object named outFile for the filename
given in the command-line
// check if the file is able to open or not


// Create Data Structure course object
Course courseDStA("Data Structures", "A", 3);
// add three students
courseDStA.addStudent("Peter Jones");
courseDStA.addStudent("Brian Smith");
courseDStA.addStudent("Anne Kennedy");
// print course information with its student list
std::cout << courseDStA << std::endl;
// shrink student list
courseDStA.shrinkCapacity();
// Create Database Systems
Course courseDStB("Data Structures", "B", 10);
// add two students
courseDStB.addStudent("Ahmad Nas");
courseDStB.addStudent("Steve Smith");
// print course info
std::cout << courseDStB << std::endl;
// merge two sections
courseDStA += courseDStB;
// print courses info
std::cout << courseDStA << std::endl;
std::cout << courseDStB << std::endl;
// shrink student list
courseDStA.shrinkCapacity();
std::cout << courseDStA << std::endl;
// add a student to database course
courseDStA.addStudent("Khan Tran");
// increase capacity
courseDStA.increaseCapacity(3);
courseDStA.addStudent("Khan Tran");
std::cout << courseDStA << std::endl;
// drop student
courseDStA.dropStudent("Kaan Pease");
std::cout << courseDStA<< std::endl;
// create course using copy constructor
Course courseDB(courseDStB);
// change its name
courseDB.setCourseName("Database Systems");
courseDB.setSection("C");
std::cout << courseDB << std::endl;
// add student
courseDB.addStudent("Ali Wattson");
std::cout << courseDB << std::endl;
std::cout << courseDStB << std::endl;
// create copy object
Course courseX("Special Topics", "X", 0);
courseX = courseDB;
courseX.setCourseName("Computer Network");
std::cout << courseX << std::endl;
// drop a student
courseX.dropStudent("Ahmad Nas");
std::cout << courseX << std::endl;
std::cout << courseDB << std::endl;
// drop all students from Computer Network
courseX.dropStudent("Steve Smith");
std::cout << courseX << std::endl;
courseX.dropStudent("Ali Wattson");
std::cout << courseX << std::endl;
// write all course objects info to the file which was given
as command-line argument
outFile << courseDStA << courseDStB <<
courseDB << courseX;
outFile.close();
return 0;
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply