Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ 1. What kind of a container is an object of type list from the Standard Temp

ID: 3606937 • Letter: C

Question

c++

1.

What kind of a container is an object of type list from the Standard Template Library?

Sequential

Associative

2.

In which of the following ways can you write an operator overload? (Select all that apply)

in a class member function

in a nonmember function

in the main function

in a friend function

3.

What is printed by these statements?

int p1 = 3;

int &p2 = p1;

p2 = p1;

cout << p1 << endl;

The address in the stack of p1

An error occurs

The address in the stack of both p1 and p2

3

4.

Suppose cursor is a pointer that points to a penguin node in a linked list using a penguin class that includes a member function get_link(). You wish to move cursor to point to the next node (to the next penguin) in the list. Which statement do you use?

cursor++;

cursor = cursor->get_link( );

cursor = get_link();

cursor.get_link();

5.

Suppose cursor points to a penguin node in a linked list using a penguin class, which includes functions called get_data() and get_link(). What Boolean expression should be true when that penguin node is the ONLY node in the list?

cursor->get_data( ) == 0.0

cursor->get_data( ) == NULL

cursor->get_link( ) == NULL

cursor == NULL

Explanation / Answer

Answer 1:

Sequential containers are those in which there is sequential ordering of elements, whereas in associative containers, it is just a collection with no ordering.

"list" of the standard template library denotes a doubly linked list. A linked list has the sequence of elements defined. So it is a sequential container.

Answer 2:

An operator overload can be written as a class member function, as a non-member function (which will only be able to access the public members), and as a friend function. It cannot be written inside the main function since functions can not be defined within other functions. Examples of each are given below:

struct OverloadedOperator
{
    int a, b;
};

OverloadedOperator operator+(OverloadedOperator s1, OverloadedOperator s2) // non-member function
{
    OverloadedOperator s;
    s.a = s1.a + s2.a;
    s.b = s1.b + s2.b;
    return s;
}

class FriendOverloadedOperator
{
    int a,b;
public:
    friend FriendOverloadedOperator operator+(FriendOverloadedOperator,FriendOverloadedOperator);
};

FriendOverloadedOperator operator+(FriendOverloadedOperator c1, FriendOverloadedOperator c2) // friend overloaded operator
{
    FriendOverloadedOperator s;
    s.a = c1.a + c2.a;
    s.b = c1.b + c2.b;
    return s;
}

class MemberFunctionOverloadedOperator
{
    int a, b;
public:
    MemberFunctionOverloadedOperator operator+(MemberFunctionOverloadedOperator d2) //overloaded operator as member function
    {
        MemberFunctionOverloadedOperator d;
        d.a = a + d2.a;
        d.b = b + d2.b;
    }
};

Answer 3:

The output will be 3. Explanations are added for each line below:

int p1 = 3; // Create a variable on stack p1, and store value 3

int &p2 = p1; // p2 is created as an alias of p1, i.e. it refers to the same memory location on stack

p2 = p1; // value of p1 is stored at p2, i.e. contents of p2 are replaced by value 3

Since p2 and p1 refer to the same memory location, assigning a value to p2 is equivalent to assigning a value to p1. Therefore, storing value 3 in p2 is equivalent to storing 3 in p1.

Answer 4:

The get_link() function gives the next element in the list. To point to the next node in the list, we need to reassign cursor to point to the next location. So the answer is:

cursor = cursor->get_link( );

Answer 5:

When there is only one element in the list, the next element will be NULL. It can carry any valid data. The next node in the list is fetched by the get_link() member function. Therefore, if there is only one element in the list and cursor points to it, the following boolean expression will be true:

cursor->get_link( ) == NULL

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote