#define _CRT_SECURE_NO_DEPRECATE
#include<cstring>
#include <iostream>
#include <new>
#include<string>
using namespace std;
class Printed_edition
{
protected:
static int N;
char name[25];
int page;
char type[25];
int i;
Printed_edition* temp;
public:
Printed_edition(void)
{
//N++;
cout << "Counstructor without parameters!base!" << endl;
page = 0;
temp = this;
}
Printed_edition(const char* Name, const char* Type, int Value)
{
SetName(Name);
SetType(Type);
page = Value;
}
virtual ~Printed_edition(void)
{
cout << "Destructor" << endl;
}
virtual void show()
{
N = 0;
for (i = 0; i < N; i++)
{
cout << temp->GetName() << endl;
}
}
void SetName(const char Value[])
{
strcpy_s(name, Value);
}
void SetType(const char Value[])
{
strcpy_s(type, Value);
}
void SetPage(int Value)
{
page = Value;
}
char* GetName()
{
return name;
}
char* GetType()
{
return type;
}
int GetPage()
{
return page;
}
};
class magazine : virtual public Printed_edition
{
public:
magazine()
{
cout << "Counstructo without parameters" << endl;
page = 0;
}
magazine(const char* Name,const char* Type, int Page)
{
strcpy_s(name, Name);
strcpy_s(type, Type);
page = Page;
}
magazine(const magazine& a)
{
cout << "Copying Constuctor" << endl;
char *name = new char[strlen(a.name) + 1];
strcpy(name, a.name);
char *type = new char[strlen(a.type) + 1];
strcpy(type, a.type);
page = a.page;
}
virtual ~magazine()
{
cout << "Destructor" << endl;
}
};
class book :virtual public Printed_edition
{
private:
char genre[25];
public:
book()
{
cout << "Constructor without parameters" << endl;
page = 0;
}
book(const char* Name, const char* Type, int Page, const char* Genre)
{
strcpy_s(name, Name);
strcpy_s(type, Type);
page = Page;
strcpy_s(genre, Genre);
}
book(const book& a)
{
cout << "Copying Constructor" << endl;
char *name = new char[strlen(a.name) + 1];
strcpy(name, a.name);
page = a.page;
char *type = new char[strlen(a.type) + 1];
strcpy(type, a.type);
char *genre = new char[strlen(a.genre) + 1];
strcpy(genre, a.genre);
}
~book()
{
cout << "Destructor" << endl;
}
void show()
{
for (i = 0; i < N; i++)
{
cout << temp->GetName() << endl;
}
}
void SetGenre(const char GENRE[])
{
strcpy_s(genre, GENRE);
}
char* GetGenre()
{
return genre;
}
};
class learning_book :virtual public Printed_edition, book
{
private:
char lessontype[25];
public:
learning_book()
{
cout << "Constructor wp" << endl;
page = 0;
}
learning_book(const char* Name, const char* Type, int Page, const char* Lessontype)
{
cout << "Constructor with parameters" << endl;
strcpy_s(name, Name);
strcpy_s(type, Type);
page = Page;
SetLesson(Lessontype);
}
learning_book(const learning_book& a)
{
cout << "Copying Constructor" << endl;
char *name = new char[strlen(a.name) + 1];
strcpy(name, a.name);
page = a.page;
char *type = new char[strlen(a.type) + 1];
strcpy(type, a.type);
char *lessontype = new char[strlen(a.lessontype) + 1];
strcpy(lessontype, a.lessontype);
}
~learning_book()
{
cout << "Destructor" << endl;
}
/*void show()
{
if (i = 0; i < N; i++)
{
cout << temp->GetName() << endl;
}
}*/
void SetLesson(const char* LT)
{
strcpy_s(lessontype, LT);
}
char *GetLessonType()
{
return lessontype;
}
};
int main()
{
//N = 0;
magazine b1("Tall", "magazine", 12);
system("pause");
}
Asked
Active
Viewed 333 times
1
1 Answers
1
Или исправить
static int N;
на
inline static int N;
или дописать за пределами класса определение
int Printed_edition::N;
(можно с инициализатором :)).
Ответ только на заданный вопрос, все остальные неприятности (а они есть, и еще какие!) проигнорированы.
Harry
- 221,325
bookвы выделяете память дляname, сохраняете указатель в локальной переменной, записываете память... на выходе локальная переменная потеряна. Итог - утечка памяти, в копии и близко нетnameиз исходного объекта. Таких мест куда больше чем одно... – Harry Apr 14 '19 at 14:05