Writing a Class

Write a class My_String which is similar to the class vector but it has character (char) type data.

Your class should contain at least: overloaded operator [] to get access to string elements a copy constructor overloaded assignment operator (=) destructor overloaded string concatenation operator (+) to concatenate two strings.

I am getting a segmentation fault, any ideas on what I can do to improve my code would be greatly appreciated! Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "std_lib_facilities_4.h"
#include <stdio.h>
#include <string.h>
using namespace std;

class My_string
{
private:
char *name;
char *ptr;// 0 to infinite
  
  

public:
// required constructors
My_string(){
  
name[0]='m';
}
void input()
{
//   gets(name);
}
  
void output()
{
  
   cout<<name;
  
}
My_string(char len)
{
cout << "Normal constructor allocating ptr" << endl;
// allocate memory for the pointer;
ptr = new char;
*ptr = len;
}

My_string(const My_string &obj)
{
cout << "Copy constructor allocating ptr." << endl;
ptr = new char;
*ptr = *obj.ptr; // copy the value
}

My_string(char *f){


}
void operator[](const My_string& m)
{
   cout<<"enter the string";
   strcpy(name,m.name);
  
  
//   gets(names);
  
  
  
}
My_string operator+(const My_string& b2)
{
My_string box;
strcat(name,b2.name);


return box;
}

  
void operator=(const My_string& s2)
{

int i;
i=strcmp(name,s2.name);
if (i>0)
{
cout<<"string not match";
}
else
if(i==0)
{
   cout<<"string is match";
}
}
// method to display distance
  
  
};
int main()
{
My_string s1,s2,s3;

cout << "First name: ";
s1.input();
cout << "Second name :";
s2.input();
s3=s1+s2;
s1[s2];
s1 = s2;
cout << "First STRING :";

return 0;
}
Your class should only contain one pointer. Namely, delete line 9.

You seem to not understand the purpose of operator[], as you are abusing it for a completely unrelated task.
Last edited on
Ok, I've gotten rid of the first pointer and re-declared everything but am still receiving a segfault error..
Please show your current code; I have no idea of the exact changes you made.
Last edited on
I just renamed everything that was declared under the pointer as name, to ptr, because name was not declared anymore
Look at your default constructor. You're trying to write to the first element of the array pointed to by name, but you haven't actually allocated any memory for the array, or initialised name to point to any memory.
@MikeyBoy: that variable no longer exists; the OP has not posted their latest code yet.
@LB the user said that they'd replaced all uses of name with ptr, so it should be pretty straightforward to apply what I wrote to their new code.

Of course, we can't be sure until they've posted their new code, but it doesn't hurt to fix an error in what we have seen.
Last edited on
Topic archived. No new replies allowed.