Question

c++ question 1. C++ string Class What does the output look like after executing the following...

c++ question

1. C++ string Class

What does the output look like after executing the following statements?

std::string numstr = "12";
numstr += "3";
std::cout << numstr << '\n';

A. None of these.

B. The snippet has syntax errors.

C. 15

D.123

//

2. Let S be a class that allows integers to be stored in its objects like an array. For example, if obj is an object of S, one can write statements like

obj[0] = 100;

or

int x = obj[0];

What is required in the class specification?

A. A public member function int operator[](int);

B. None of these

C. A public member function int[] S(int);
D. A public member function  int &operator[](int);

//

3. Given a class Coll and some variables as follows:

class Coll {};

Coll obj;

char *cPtr = nullptr;

Coll *ptr = &obj;

Which casting is allowed by C++?

A. cPtr = reinterpret_cast<char *>(ptr);

B. cPtr = dynamic_cast<char *>(ptr);

C. None of these.

D.cPtr = static_cast<char *>(ptr);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.

std::string numstr = "12"; // Initially the string is 12
numstr += "3"; // String concatenate with one more character that is 3
std::cout << numstr << '\n'; // print 123

Option d

2.

Let S be a class that allows integers to be stored in its objects like an array. For example, if obj is an object of S, one can write statements like

A public member function  int &operator[](int);

Option d

3.

class Coll {};

Coll obj;

char *cPtr = nullptr;

Coll *ptr = &obj;

class Coll and some variables as follows cPtr = dynamic_cast<char *>(ptr);

Option b

Add a comment
Know the answer?
Add Answer to:
c++ question 1. C++ string Class What does the output look like after executing the following...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The following program contains the definition of a class called List, a class called Date and...

    The following program contains the definition of a class called List, a class called Date and a main program. Create a template out of the List class so that it can contain not just integers which is how it is now, but any data type, including user defined data types, such as objects of Date class. The main program is given so that you will know what to test your template with. It first creates a List of integers and...

  • C++ 1. The copy constructor is used for one the following scenarios: I. Initialize one object...

    C++ 1. The copy constructor is used for one the following scenarios: I. Initialize one object from another of the same type. II. Copy an object to pass it as argument to a function. III. Copy an object to return it from a function. Read the following program and answer questions (20 points) #include <iostream> using namespace std; class Line public: int getLength( void); Line( int len // simple constructor Line( const Line &obj) 1/ copy constructor Line (); //...

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

  • 2. (50 marks) A string in C++ is simply an array of characters with the null...

    2. (50 marks) A string in C++ is simply an array of characters with the null character(\0) used to mark the end of the string. C++ provides a set of string handling function in <string.h> as well as I/O functions in <iostream>. With the addition of the STL (Standard Template Library), C++ now provides a string class. But for this assignment, you are to develop your own string class. This will give you a chance to develop and work with...

  • For Question 14 - 15, please refer to the following code snippet. 1 class Fitness 2...

    For Question 14 - 15, please refer to the following code snippet. 1 class Fitness 2 { 3 int Jumping; int Running; 12 }; 8 class Round ISAShape: public class Fitness 9 { 10 11 int Sleeping; int Eating; 13 }; 14. The following statements are TRUE based on the code snippet above, EXCEPT (A) class RoundISAShape is inheriting from class Fitness (B) class RoundISAShape is the base class for class Fitness (C) class Round ISAShape is considered as the...

  • #include <iostream> #include <string> #include <cstring> using namespace std; class Node{       private:       ...

    #include <iostream> #include <string> #include <cstring> using namespace std; class Node{       private:        int data;        Node* nextNodePtr;           public:        Node(){}               void setData(int d){            data = d;        }               int getData(){            return data;        }               void setNextNodePtr(Node* nodePtr){                nextNodePtr = nodePtr;        }                ...

  • HELP PLEASE ANSWER 3(c). Consider the following function object: #include <string> class CustomCompare public: bool operator...

    HELP PLEASE ANSWER 3(c). Consider the following function object: #include <string> class CustomCompare public: bool operator (const atd:istringk 1hs, const std::string& rhs) if (1hs. length) 0 && rhs.lengthO 0) return false; int 1 lhs. length )-1; intr rhs. length)-1; return (ro&&10) I Write one C++ statement that defines a variable myCustomPQ, where myCustomPQ is a priority queue storing strings and using CustomCompare.

  • Consider the following definition of a C++ class that acts like an array with bounds-checking (i.e....

    Consider the following definition of a C++ class that acts like an array with bounds-checking (i.e. ensuring that a valid array index is used each time an array element is referenced). class c_array { public: c_array(int s){ size = s; a = new int[size]; } int &operator[](int i); int get_size() { return size; } private: int size; int *a; }; 1- Write the header for the c-array class in a different header file (c_array.h). 2- Complete the definition of the...

  • C++ When running my tests for my char constructor the assertion is coming back false and...

    C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...

  • c. [3 marks] Given the following definition of the class MyClass, class MyClass { String s...

    c. [3 marks] Given the following definition of the class MyClass, class MyClass { String s int z Myclass(int y) { z = y } } fill in the blanks (labelled i., ii. and iii.) in the definition of the method main, of the class StringProcessing, with i. a declaration of a variable named obj of type MyClass, initialised with a MyClass object so that its instance variable z has a value of 20, ii. an assignment of the instance...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT