Overloaded constructor of String

Hi guys,
I have to implement the following class:
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
class MyString
{
private:
	char *str; 	// Pointer to the char array that holds the string
	int strLength; 	// Variable to store the length of the string

public:
	// Default constructor to initialize the string to empty string
	MyString();

	// Overloaded constructor
	MyString(const char *);

	// Copy constructor
	MyString(const MyString&);

	// Overloaded assignment operator
	const MyString& operator = (const MyString&);

	// Display the string on screen
	void display ();

	// Returns the length (# of characters excluding the null terminator) of the string
	int getLength ();

	// Destructor
	~MyString();

	// Overload the relational operators to allow comparison of two MyString objects
	bool operator == (const MyString&) const;
	bool operator != (const MyString&) const;
	bool operator <= (const MyString&) const;
	bool operator < (const MyString&) const;
	bool operator >= (const MyString&) const;
	bool operator > (const MyString&) const;

	// Concatenating two MyString objects. Returns the new MyString
	MyString operator + (const MyString&) const;
	
	// Concatenates another MyString with the current MyString
	const MyString& operator += (const MyString&);
};

can anyone please tell me how can I define overloaded constructor at line 12.

You can define it as you want. And nobody knows what you want.:) It is your class. Whay do you ask the forum?!

I can only surmise that you mean the following

1
2
3
4
5
6
MyString::MyString(const char *s)
{
   strLength = std::strlen( s );
   str = new char[strLength + 1];
   std::strcpy( str, s );
}


Last edited on
Topic archived. No new replies allowed.