Here is my code. My teacher won't let us use string type, so I'm using pointers instead.
When I'm debugging the program it says that the issue is in Author
class destructor.
Thats the message I'm getting:
SIGTRAP (Trace/breakpoint trap)
It's occuring on delete[] name
line.
I think it won't remember name
field when I enter it, because it prints everything right instead of name which is just some random stuff.
#include <iostream>
#include <cstring>
using namespace std;
class Author{
private:
char *name;
char *surname;
public:
Author();
Author(char *, char *);
Author(Author&);
char* get_name();
char* get_surname();
Author& set_name(char *new_name);
Author& set_surname(char *new_surname);
void print_fields();
void print();
~Author();
};
Author::Author() {
this->name = new char[5];
this->surname = new char[5];
strcpy(name, "NONE");
strcpy(surname, "NONE");
}
Author::Author(char *name, char *surname) {
this->name = new char[strlen(name) + 1];
this->surname = new char[strlen(surname) + 1];
strcpy(this->name, name);
strcpy(this->surname, surname);
}
Author::Author(Author &author) {
this->name = new char[strlen(author.name)+1];
this->surname = new char[strlen(author.surname)+1];
strcpy(this->name, author.name);
strcpy(this->surname, author.surname);
}
char* Author::get_name() {
return this->name;
}
char* Author::get_surname() {
return this->surname;
}
Author& Author::set_name(char *new_name) {
delete name;
this->name = new char[strlen(new_name) + 1];
strcpy(this->name, new_name);
return *this;
}
Author& Author::set_surname(char *new_surname) {
delete surname;
this->surname = new char[strlen(new_surname) + 1];
strcpy(this->surname, new_surname);
return *this;
}
void Author::print_fields() {
cout << this->name << " " << this->surname;
}
void Author::print() {
cout << "Author " << this->surname << endl;
}
Author::~Author() {
delete[] name;
delete[] surname;
}
class Book {
private:
char *title;
short code;
Author author;
short year;
short number_of_pages;
public:
Book();
Book(char *, short, const Author&, short, short);
Book(Book&);
char* get_title();
short get_code();
Author get_author();
short get_year();
short get_number_of_pages();
Book& set_title(char *new_title);
Book& set_code(short new_code);
Book& set_author(const Author& new_author);
Book& set_year(short new_year);
Book& set_number_of_pages(short new_number_of_pages);
void print_fields();
void print();
~Book();
};
Book::Book() {
this->title = new char[5];
strcpy(this->title, "NONE");
this->code = -1;
this->author = Author();
this->year = 0;
this->number_of_pages = 0;
}
Book::Book(char *title, short code, const Author& author, short year, short number_of_pages) {
this->title = new char[strlen(title) + 1];
strcpy(this->title, title);
this->code = code;
this->author = author;
this->year = year;
this->number_of_pages = number_of_pages;
}
Book::Book(Book &book): author(book.author) {
this->title = new char[strlen(book.title) + 1];
strcpy(this->title, book.title);
this->code = book.code;
this->year = book.year;
this->number_of_pages = book.number_of_pages;
}
char *Book::get_title() {
return this->title;
}
short Book::get_code() {
return this->code;
}
Author Book::get_author() {
return this->author;
}
short Book::get_year() {
return this->year;
}
short Book::get_number_of_pages() {
return this->number_of_pages;
}
Book& Book::set_title(char *new_title) {
delete[] title;
this->title = new char[strlen(new_title) + 1];
strcpy(this->title, new_title);
return *this;
}
Book& Book::set_code(short new_code) {
this->code = new_code;
return *this;
}
Book& Book::set_author(const Author& new_author) {
this->author = new_author;
return *this;
}
Book& Book::set_year(short new_year) {
this->year = new_year;
return *this;
}
Book& Book::set_number_of_pages(short new_number_of_pages) {
this->number_of_pages = new_number_of_pages;
return *this;
}
void Book::print_fields() {
cout << this->title << " ";
cout << this->code << " [";
this->author.print_fields();
cout << "] ";
cout << this->year << " " << this->number_of_pages << endl;
}
void Book::print() {
cout << "Book " << this->title << endl;
}
Book::~Book() {
delete[] title;
this->code = 0;
this->year = 0;
this->number_of_pages = 0;
}
int main() {
Book book1;
char *title = new char[255];
char *name = new char[255];
char *surname = new char[255];
short code;
short year, number_of_pages;
cout << "Enter author\'s name:";
cin >> name;
cout << "Enter surname:";
cin >> surname;
Author author((char*) name, (char*) surname);
cout << "Enter book\'s information:";
cin >> title >> code >> year >> number_of_pages;
Book book2((char*) title, code, author, year, number_of_pages);
Book book3 = book2;
book1.print_fields();
book2.print_fields();
book3.print();
return 0;
}
I made char *
fields const
and Author(const Author&)
. The problem is still there although now it remembers the name
field correctly.
After the program runs i get:
Process finished with exit code -1073740940 (0xC0000374)
and now it rewrites the name
field in book1
with the name
of book2
.
This is how my console window looks like after the program ends:
Enter author's name:John
Enter surname:Brown
Enter book's information:Title 1 1999 200
NONE -1 [Title NONE] 0 0
Title 1 [John Brown] 1999 200
Book Title
Process finished with exit code -1073740940 (0xC0000374)