OOP C++(constant char pointer)

Pages: 12
The best e-learning is https://www.learncpp.com/
Like I said, the code for copy constructor is very similar to the other constructor:

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
#include <iostream>
#include <cctype>
using namespace std;

class Book {
private:
	char* title {};
	char* authors {};
	int publishingYear {};

public:
	Book() {}

	Book(const char* title_, const char* authors_, int publishingYear_) : publishingYear(publishingYear_)
	{
		if (title_)
			title = strcpy(new char[strlen(title_) + 1], title_);

		if (authors_)
			authors = strcpy(new char[strlen(authors_) + 1], authors_);
	}

	~Book()
	{
		delete[] title;
		delete[] authors;
	}

	Book(const Book& book) : publishingYear(book.publishingYear)
	{
		if (book.title)
			title = strcpy(new char[strlen(book.title) + 1], book.title);

		if (book.authors)
			authors = strcpy(new char[strlen(book.authors) + 1], book.authors);
	}

	Book& operator=(const Book&) = delete;	// NEEDS TO BE DEFINED

	friend class Printer;
};

class Printer
{
public:
	static void printBook(const Book& book)
	{
		cout << book.title << '\n';

		for (char* str = book.authors; *str; )
			if (*str == ',')
				for (cout << '\n', ++str; *str && isspace(*str); ++str);
			else
				cout << *str++;

		cout << '\n' << book.publishingYear << '\n';
	}
};

int main()
{
	Book book1("Giai tich 1", "Nguyen Dinh Huy, Nguyen Thi Xuan Anh", 2000);

	Printer::printBook(book1);

	cout << '\n';

	Book book2(book1);

	Printer::printBook(book2);
}

there is errors with the output , i cant find any bug here. Can you show me? Thanks for the advice about e-book
input:
Book book1("Giai tich 1","Nguyen Dinh Huy",2000);
Book book2 = book1;
book2.printBook();


output:
UH\x89\xe5AWAVAUATSH\x83\xec(L\x8b%X\x7f!
(null)
2000
Last edited on
What compiler IDE do you use?
If you run the code from seeplus on cpp.sh you need to add #include <cstring> and you get the following output:
Giai tich 1
Nguyen Dinh Huy
Nguyen Thi Xuan Anh
2000

Giai tich 1
Nguyen Dinh Huy
Nguyen Thi Xuan Anh
2000
I'm confused by what that 'output' is. It looks like an ASCII representation of the type of binary data that is found in the instruction section of an executable file (AWAVAUATSH). That doesn't match what the program prints at all.
Last edited on
I dont know, when i submit the code online and the compiler of the test throw that result, and of course it is a fault, but i dont know yet
Do you have a compiler like Microsoft Visual Studio, or Dev-C++ or Code::Blocks? If so, try using it to compile and run the program seeplus gave you. If you don't have one, I suggest you download one before you do more programming.

Personally, I wouldn't trust online compilers farther than I could throw them...but they can be better than nothing.
you need to add #include <cstring>


Yeah. I forgot as it compiles OK without it for VS2019.
Last edited on
Using an on-line compiler see https://wandbox.org/permlink/aKMKRInxIvRf0nKJ
I can see that all the other compilers can run the code. I dont know why my teacher's one didnt. All I know is my teacher's compiler run on Linux . Anyways thanks guys
Topic archived. No new replies allowed.
Pages: 12