Completely lost on how to start this here (Primarily for the Constructors)

So I am doing a C++ Project for one of my classes and I am completely lost on what this is asking for (More so on how to get the program off the ground as the language used is very weird to me in relation to the Constructors)

Link to the requirements it wants
https://cdn.discordapp.com/attachments/635875520038436919/915029775859482654/unknown.png

I have the prototypes in a mystring.h file (paired with mystring.cpp)
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
#ifndef MYSTRING_H 
#define MYSTRING_H

#include <iostream>     /* cout, endl */
class MyString {
public:
	//Constructors
	MyString();
	MyString(const char*);
	//Destructor
	~MyString(); 
	 
	int getString(); 
	bool upper(); 
	bool lower();
	bool reverse(); 
	int clear();
	int set();
	int length();
	int substring();
	int cat();
	int count();

	//Length Attribute 
	int Length;
};


But I am more or less lost on how to get the actual functions made as implementation is relatively easy for me, it's just the functions themselves.

(AKA, I am lost on how to make at least the starts of these functions in mystring.cpp, and if I can get at least some of the major ones down (like however the heck he wants the constructors and destructors to work), I should be able to get most of the rest done.)

AKA I have almost 0 clue on how to do the constructors for this, after that, the rest should be more or less painless.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
class mystring should have a char*, Ill call it 'data' for now you can rename it.

then, you ctor might look like:

MyString::MyString(const char* in)
: Length{strlen(in)} // add a comma and fill in other attributes that you want, if any
{
  //do some work here that is beyond what you can do with just simple copies or derivation in the init list
   data = new char[something]; //something is maybe Length+1, or maybe something bigger, whatever
   strcpy(data,in);
}


the default ctor (no params) probably won't do anything because you would set data and length to nullptr and 0 where they are declared instead. but you will have to provide the empty function one way or another, or you won't be able to make objects using it. I think there is a way to get the standard empty one to be made for you, if that is allowed?
Last edited on
As strcpy() returns the destination pointer, then possibly something like: [Note that Length has to be defined before data.]

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <cstring>

struct MyString {
	MyString() {}
	MyString(const char* in);

	size_t Length {};
	char* data {};
};

MyString::MyString(const char* in) : Length {strlen(in)}, data {strcpy(new char[Length + 1]{}, in)} {}

Last edited on
"You may not use the C++ string or any of the C-string functions or regular expressions...
Do everything manually (ie. with loops)"

So roll out your own homegrown strlen, strcpy etc.
> like however the heck he wants the constructors and destructors to work
you have a dynamic array that would store the characters
the constructor will allocate the memory and the destructor will release it
don't forget the copy constructor and assignment operator

> implementation is relatively easy for me,
> lost on how to get the actual functions made
¿is it easy or you have no idea?

> unknown.png
an image of text, lovely
As a starter, possibly:

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
#include <iostream>

class MyString {
public:
	MyString(const char* in = "");
	~MyString();

	MyString(const MyString&) = delete;		// To be provided if needed
	MyString& operator=(const MyString&) = delete;	// To be provided if needed

	size_t length() const;
	void set(const char* in);
	char* getString() const;

private:
	static size_t len(const char* s);

	size_t Length {};
	char* data {};
};

MyString::MyString(const char* in) {
	set(in);
}

MyString::~MyString() {
	delete[] data;
}

char* MyString::getString() const {
	return data;
}

size_t MyString::length() const {
	return len(data);
}

size_t MyString::len(const char* s) {
	const char* sc {s};

	for (; *sc; ++sc);
	return sc - s;
}

void MyString::set(const char* in)
{
	delete[] data;
	Length = len(in);
	data = new char[Length + 1];

	for (auto d1 {data}; *d1++ = *in++; );
}

int main()
{
	MyString s0;

	std::cout << s0.length() << "  " << s0.getString() << '\n';

	MyString s1("qwerty");

	std::cout << s1.length() << "  " << s1.getString() << '\n';
}



0
6  qwerty

Last edited on
Topic archived. No new replies allowed.