Which of the following is correct using the constructor?
A. string s1;
B. string s2 (s1);
C. string s3{7};
D. string s4{'a'};
E. string s5{7, 'a'};
Pick an option to check yourself, or open the answer below.
Show answer & explanation
Correct answer: A. A, B, D, E only
Explanation
string s1; (default), string s2(s1); (copy), string s4{'a'}; and string s5{7, 'a'}; (initializer lists of chars) are listed as valid. Following the official key (after Stroustrup's examples), string s3{7}; is treated as an error, since there is no string(int) constructor.
More Programming Languages and Computer Graphics questions
- Match List I with List II
- Which of the following is wrong about the data types?
- If a constructor 'Date' is declared explicitly and has to be defined outside the class, which of the Q following is correct?
- Given a vector with Cartesian components (x, y), if scaling is done with the matrix [0.5 0; 0 1.5], which of the following are true? 1. Dec…
- Match the List-I and List-II d.To gain access to IV. to use explicit differing instances for casting derived classes through base we have C…
- In what sequence the initialization, testing and execution of body is done while using do-while loop? I. Commenting II. Execution of the bo…