Question: Questions 5 to 8 will be using the SegmentList and Segment classes shown above. The goal is to use SegmentList, as an alternative to List,

Questions 5 to 8 will be using the SegmentList and Segment classes shown above. The goal is to use SegmentList, as an alternative to List, to create a list of connected points, as seen in Figure 2. The SegmentList default constructor initializes the first segment to the nullptr. The SegmentList parametrized constructor should initialize the first segment. The addNeighbor method is responsible for adding new segments; it should find the last segment, and make its neighbor point to the newly added segment. The draw method should start at the first segment, and draw each of its neighbor segments, with a line connecting all neighbors, as seen in Figure 2. The destructor must release all memory used by the class.
5. Which code snippet correctly implements the parametrized SegmentList constructor?
A) SegmentList(float x, float y){ first ->Segment(x, y); }
B) SegmentList(float x, float y){ first = Segment(x, y); }
c) SegmentList(float x, float y){ first = new Segment(x,y); }
D) SegmentList(float x, float y){ first ->neighbor = new Segment (x, y); }
6. Which code snippet correctly implements the addNeighbor method?
A) void addNeighbor(float x, float y){ first --> neighbor = new Segment(x, y); }
B) void addNeighbor(float x, float y){ while (first -> neighbor != nullptr){ first = first ->neighbor; } first -> neighbor = new Segment(x, y); }
C) void addNeighbor(float x, float y){ if (first == nullptr){ first = new Segment(x, y); } Segment* temp = first; while (temp-> neighbor != nullptr){
temp = temp->neighbor; } temp-> neighbor = new Segment(x, y); }
D) void addNeighbor(float x, float y){ if (first == nullptr){ first = new Segment(x, y); } else {
Segment* temp = first; while (temp->neighbor != nullptr){ temp = temp->neighbor; } temp->neighbor = new Segment(x, y); }}
7. Which code snippet correctly implements the draw method?
A) void draw(){ first->draw(); }
B) void draw(){ while (first != nullptr){ first-> draw(); first = first ->neighbor; }}
C) void draw(){ Segment* temp = first; while (temp != nullptr){ temp = temp->neighbor; } temp->draw(); }
D) void draw(){ Segment* temp = first; while (temp != nullptr){ temp->draw(); temp = temp->neighbor; }}
8. Which code snippet correctly implements the destructor?
A) ~ SegmentList(){ delete first; }
B) ~ SegmentList(){ delete first; delete first -> neighbor; }
C) ~ SegmentList(){ while (first != nullptr){ delete first -> neighbor; } delete first; }
D) ~ SegmentList(){ Segment* temp = first; while (temp != nullptr){ delete temp->neighbor; } delete first; }
Questions 5 to 8 will be using the SegmentList

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!