1. When is it essential that a class have to define a destructor in C++ class an
ID: 662873 • Letter: 1
Question
1. When is it essential that a class have to define a destructor in C++ class and why?
2. Why should data members of a class be private?
3.
Given an efficient circular array-based queue Q capable of holding 10 objects. Show the final contents of the array myArray (index from 0 to 9) of the array after the following code is executed (you can list the values for myArray[0], myArray[1], ..., myArray[9]):
Queue Q;
for (int k = 1; k ? 7; k++)
Q.enqueue(k);
for (int k = 1; k ? 7; k++)
Q.enqueue(Q.dequeue());
4.
Assume that Stack is the stack class implemented by a static array, with data type is set to int and stack capacity is 5. Give the contents of the myTop and myArray data members of s after the code segment is executed, or indicate why an error occurs.
Stack s;
for (int i = 0; i < 2; i++)
s.push(3*i);
int i = s.top();
s.pop();
5. USE VISUAL STUDIO AS COMPILER AND SHOW OUTPUT
Declare and define a class in C++ syntax to model the given item: A Person, with the following features in a single text or .h file.
Define the private portion of the class Person for people consisting of an integer for ID and a char pointer to a dynamic array for name.
Define functions below to the Person class and implement these functions:
Person(); // default constructor for a person named "Anonymous" and ID is 123456789
Person(char name[], int id); // constructor to initialize the name and ID
int get_id() const; // accessor to retrieve student's ID
void set_name(char new_name[]); // mutator to set or change student's name
overload output operator << to print the Person infomation in the format: Person's name (Person's ID), e.g. Anonymous (987654321)
Add the "big three" to the class and implement the overloading assignment operator = ONLY.
Upload your file here
Explanation / Answer
1.
When memory allocated dynamically is no longer needed.
2.
To prevent objects of other classes from accessing them.
3.
The queue after the first loop is:
_, 1, 2, 3, 4, 5, 6, 7, _, _
The queue after the second loop(final queue) is:
3, 4, 5, 6, 7, _, _, _, 1, 2
4.
The processing of the stack is as follows(I have started with top = 0):
myArray: 0(top element), myTop = 1
myArray: 0, 3(top element), myTop = 2
myArray: 0(top element), myTop = 1
The final values are:
myArray: 0
myTop = 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.