C++ | Constructors | Question 17

Output?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<iostream>
#include<string.h>
using namespace std;
  
class String
{
    char *str;
public:
     String(const char *s);
     void change(int index, char c) { str[index] = c; }
     char *get() { return str; }
};
  
String::String(const char *s)
{
    int l = strlen(s);
    str = new char[l+1];
    strcpy(str, s);
}
  
int main()
{
   String s1("geeksQuiz");
   String s2 = s1;
   s1.change(0, 'G');
   cout << s1.get() << " ";
   cout << s2.get();
}

chevron_right


(A) GeeksQuiz
geeksQuiz
(B) GeeksQuiz
GeeksQuiz
(C) geeksQuiz
geeksQuiz
(D) geeksQuiz
GeeksQuiz


Answer: (B)

Explanation: Since there is no copy constructor, the compiler creates a copy constructor. The compiler created copy constructor does shallow copy in line ” String s2 = s1;”

So str pointers of both s1 and s2 point to the same location.



There must be a user defined copy constructor in classes with pointers ot dynamic memory allocation.

Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.